class Reek::Source::SourceCode
A SourceCode
object represents a chunk of Ruby source code.
Constants
- IO_IDENTIFIER
- STRING_IDENTIFIER
Attributes
Public Class Methods
Source
# File lib/reek/source/source_code.rb, line 56 def self.default_parser Parser::CurrentRuby.new(AST::Builder.new).tap do |parser| diagnostics = parser.diagnostics diagnostics.all_errors_are_fatal = true diagnostics.ignore_warnings = true end end
Source
# File lib/reek/source/source_code.rb, line 45 def self.from(source, origin: nil) case source when self then source else new(source: source, origin: origin) end end
Initializes an instance of SourceCode
given a source. This source can come via several different ways:
-
from Files or Pathnames a la ‘reek lib/reek/`
-
from IO (STDIN) a la ‘echo “class Foo; end” | reek`
-
from String via our rspec matchers a la ‘expect(“class Foo; end”).to reek`
-
from an existing
SourceCode
object. This is passed through unchanged
@param source [SourceCode|File|Pathname|IO|String] the given source @param origin [String|nil]
@return an instance of SourceCode
Source
# File lib/reek/source/source_code.rb, line 28 def initialize(source:, origin: nil, parser: self.class.default_parser) @origin = origin @parser = parser @source = source end
Initializer.
@param source [File|Pathname|IO|String] Ruby source code @param origin [String] Origin of the source code. Will be determined
automatically if left blank.
@param parser the parser to use for generating AST’s out of the given code
Public Instance Methods
Source
# File lib/reek/source/source_code.rb, line 64 def origin @origin ||= case source when File then source.path when IO then IO_IDENTIFIER when Pathname then source.to_s when String then STRING_IDENTIFIER end end
Source
# File lib/reek/source/source_code.rb, line 52 def syntax_tree @syntax_tree ||= parse end
Private Instance Methods
Source
# File lib/reek/source/source_code.rb, line 76 def code @code ||= begin str = case source when File, Pathname then source.read when IO then source.readlines.join when String then source end str = str.dup if str.frozen? str.force_encoding(Encoding::UTF_8) end end
Source
# File lib/reek/source/source_code.rb, line 120 def parse buffer = Parser::Source::Buffer.new(origin, 1) buffer.source = code ast, comments = parser.parse_with_comments(buffer) # See https://whitequark.github.io/parser/Parser/Source/Comment/Associator.html comment_map = Parser::Source::Comment.associate(ast, comments) TreeDresser.new.dress(ast, comment_map) || AST::Node.new(:empty) end
Parses the given code into an AST
and associates the source code comments with it. This AST
is then traversed by a TreeDresser
which adorns the nodes in the AST
with our SexpExtensions. Finally this AST
is returned where each node is an anonymous subclass of Reek::AST::Node
Given this @code:
# comment about C class C def m puts 'nada' end end
this method would return something that looks like
(class (const nil :C) nil (def :m (args) (send nil :puts (str "nada"))))
where each node is possibly adorned with our SexpExtensions (see ast/ast_node_class_map and ast/sexp_extensions for details).
@return Reek::AST::Node
the AST
presentation for the given code