Class: Reek::Source::SourceCode

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/source/source_code.rb

Overview

A Source object represents a chunk of Ruby source code.

Direct Known Subclasses

SourceFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, desc, parser = Parser::Ruby21) ⇒ SourceCode

Returns a new instance of SourceCode


15
16
17
18
19
# File 'lib/reek/source/source_code.rb', line 15

def initialize(code, desc, parser = Parser::Ruby21)
  @source = code
  @desc = desc
  @parser = parser
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc


13
14
15
# File 'lib/reek/source/source_code.rb', line 13

def desc
  @desc
end

Instance Method Details

#syntax_treeObject


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reek/source/source_code.rb', line 21

def syntax_tree
  @syntax_tree ||=
    begin
      begin
        ast, comments = @parser.parse_with_comments(@source, @desc)
      rescue Racc::ParseError, Parser::SyntaxError => error
        $stderr.puts "#{desc}: #{error.class.name}: #{error}"
      end

      comment_map = Parser::Source::Comment.associate(ast, comments) if ast
      TreeDresser.new.dress(ast, comment_map)
    end
end