class Ameba::Source

Overview

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

Included Modules

Defined in:

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

Constructors

Instance Method Summary

Instance methods inherited from module Ameba::Spec::Util

normalize_code(code, separator = '\n') normalize_code

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, token : Crystal::Token, message, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, location : Tuple(Int32, Int32), message, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, location : Tuple(Int32, Int32), end_location : Tuple(Int32, Int32), message, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil) : Issue
add_issue(rule, node : Crystal::ASTNode, message, status : Issue::Status | Nil = nil, block : Source::Corrector -> | Nil = nil, *, prefer_name_location = false) : Issue
add_issue(rule, location : Tuple(Int32, Int32), message, status : Issue::Status | Nil = nil, &block : Source::Corrector -> ) : Issue
add_issue(rule, token : Crystal::Token, message, status : Issue::Status | Nil = nil, &block : Source::Corrector -> ) : Issue
add_issue(rule, location : Tuple(Int32, Int32), end_location : Tuple(Int32, Int32), message, status : Issue::Status | Nil = nil, &block : Source::Corrector -> ) : Issue
add_issue(rule, node : Crystal::ASTNode, message, 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?, exit?(node) exit?, flow_command?(node, in_loop) flow_command?, flow_expression?(node, in_loop = false) flow_expression?, literal?(node) : Bool literal?, loop?(node) loop?, name_end_location(node) name_end_location, name_location(node) name_location, name_size(node) name_size, node_source(node, code_lines) node_source, path_named?(node, name) : Bool path_named?, raise?(node) raise?, source_between(loc, end_loc, code_lines) : String | Nil source_between, static_literal?(node) : Bool static_literal?

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 = "", normalize = true) #

[View source]
def self.new(code : String, path : String = "") #

Creates a new source by #code and #path.

For example:

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

[View source]

Instance Method Detail

def ast : Crystal::ASTNode #

Returns AST nodes constructed by Crystal::Parser.

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

[View source]
def code : String #

Crystal code (content of a source file).


[View source]
def correct? #

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


[View source]
def fullpath : String #

[View source]
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 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"]

[View source]
def matches_path?(filepath) #

Returns true if filepath matches the source's path, false otherwise.


[View source]
def path : String #

Path to the source file.


[View source]
def pos(location : Crystal::Location, end end_pos = false) : Int32 #

Converts an AST location to a string position.


[View source]
def spec? #

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


[View source]