Ameba::Source - ameba 1.7.0

# class Ameba::Source

- Ameba::Source
- Reference
- Object

##
Overview

An entity that represents a Crystal source file. Has path, lines of code and issues reported by rules.

##
Included Modules

- Ameba::InlineComments
- Ameba::Reportable

##
Defined in:

ameba/source.cr
ameba/source/corrector.cr
ameba/source/rewriter.cr
ameba/spec/support.cr

##
Constructors

- .new(code : String = "", path : String = "", project_path : String = path)

  Creates a new source by `#code` and `#path`.

- .new(code : String, *args, normalize = true, **opts)

##
Instance Method Summary

- #ast : Crystal::ASTNode

  Returns AST nodes constructed by `Crystal::Parser`.

- #code : String

  Crystal code (content of a source file).

- #correct!

  Corrects any correctable issues and updates `#code`.

- #ecr?

  Returns `true` if the source is an ECR template, `false` otherwise.

- #fullpath : String

  Absolute path to the source file.

- #lines : Array(String)

  Returns lines of code split by new line character.

- #path : String

  Path to the source file.

- #pos(location : Crystal::Location, end end_pos = false) : Int32

  Converts an AST location to a string position.

- #project_path : String

  Path to the source file relative to the project root.

- #spec?

  Returns `true` if the source is a spec file, `false` otherwise.

### Instance methods inherited from module `Ameba::Reportable`

add_issue(rule, location : Crystal::Location | Nil, end_location : Crystal::Location | Nil, message : String, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, location : Crystal::Location | Nil, end_location : Crystal::Location | Nil, message : String, status : Issue::Status | Nil = nil, &block : Source::Corrector -> ) : Issue
add_issue(rule, location : Tuple(Crystal::Location, Crystal::Location), message : String, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, token : Crystal::Token, message : String, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, location : Tuple(Int32, Int32), message : String, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, location : Tuple(Int32, Int32), end_location : Tuple(Int32, Int32), message : String, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, node : Crystal::ASTNode, message : String, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil, *, prefer_name_location = false) : Issue
add_issue(rule, location : Tuple(Int32, Int32), message : String, status : Issue::Status | Nil = nil, &block : Source::Corrector -> ) : Issue
add_issue(rule, token : Crystal::Token, message : String, status : Issue::Status | Nil = nil, &block : Source::Corrector -> ) : Issue
add_issue(rule, location : Tuple(Int32, Int32), end_location : Tuple(Int32, Int32), message : String, status : Issue::Status | Nil = nil, &block : Source::Corrector -> ) : Issue
add_issue(rule, node : Crystal::ASTNode, message : String, status : Issue::Status | Nil = nil, *, prefer_name_location = false, &block : Source::Corrector -> ) : Issue add_issue, issues issues, valid? valid?

### Instance methods inherited from module `Ameba::AST::Util`

abort?(node) abort?, control_exp_code(node : Crystal::ControlExpression, code_lines) control_exp_code, dynamic_literal?(node) : Bool dynamic_literal?, each_inline_directive(source, &block : Crystal::Token, String, Array(String) -> _) each_inline_directive, exit?(node) exit?, flow_command?(node, in_loop) flow_command?, flow_expression?(node, in_loop = false) flow_expression?, has_arguments?(node) : Bool has_arguments?, has_block?(node) : Bool has_block?, has_short_block?(node, code_lines) has_short_block?, heredoc?(node : Crystal::ASTNode, source : Source) heredoc?, literal?(node) : Bool literal?, loop?(node) loop?, name_end_location(node) name_end_location, name_location(node) name_location, name_location_or(token : Crystal::Token, name, *, adjust_location_column_number = nil)
name_location_or(node : Crystal::ASTNode, *, adjust_location_column_number = nil) name_location_or, name_size(node) name_size, node_source(node, code_lines) node_source, nodoc?(node) nodoc?, operator_method?(node) operator_method?, operator_method_name?(name : String) operator_method_name?, path_name(node : Crystal::Path, *, include_global = true) : String path_name, path_named?(node, *names : String) : Bool path_named?, raise?(node) raise?, setter_method?(node) setter_method?, setter_method_name?(name : String) setter_method_name?, short_block?(node, code_lines) short_block?, source_between(loc, end_loc, code_lines) : String | Nil source_between, static_literal?(node) : Bool static_literal?, suffix?(node) suffix?, takes_arguments?(node) : Bool takes_arguments?

### Instance methods inherited from module `Ameba::InlineComments`

comment?(line_number : Int32) comment?, location_disabled?(location : Crystal::Location | Nil, rule) location_disabled?, parse_inline_directive(line) parse_inline_directive

##
Constructor Detail

def self.new(code : String = "", path : String = "", project_path : String = path) #

Creates a new source by `#code` and `#path`.

For example:

    path = "src/source.cr"
    Ameba::Source.new(File.read(path), path)

def self.new(code : String, *args, normalize = true, **opts) #

##
Instance Method Detail

def ast : Crystal::ASTNode #

Returns AST nodes constructed by `Crystal::Parser`.

    source = Ameba::Source.new(code, path)
    source.ast

def code : String #

Crystal code (content of a source file).

def correct! #

Corrects any correctable issues and updates `#code`. Returns `false` if no issues were corrected.

def ecr? #

Returns `true` if the source is an ECR template, `false` otherwise.

def fullpath : String #

Absolute path to the source file.

def lines : Array(String) #

Returns lines of code split by new line character. Since `#code` is immutable and can't be changed, this method caches lines in an instance variable, so calling it a second time will not perform a split, but will return lines instantly.

    source = Ameba::Source.new("a = 1\nb = 2", path)
    source.lines # => ["a = 1", "b = 2"]

def path : String #

Path to the source file.

def pos(location : Crystal::Location, end end_pos = false) : Int32 #

Converts an AST location to a string position.

def project_path : String #

Path to the source file relative to the project root.

def spec? #

Returns `true` if the source is a spec file, `false` otherwise.