Sha256: 5f5ae3cf7f1764dfdace8f6d3470c2ec2b07cbcf5041ada7e72d0f5d68f22410

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

old_verbose, $VERBOSE = $VERBOSE, nil
require 'parser/current'
$VERBOSE = old_verbose
require_relative 'tree_dresser'
require_relative 'ast_node'

module Reek
  module Source
    #
    # A +Source+ object represents a chunk of Ruby source code.
    #
    class SourceCode
      attr_reader :desc

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

      def self.from(source)
        case source
        when File   then new(source.read, source.path)
        when IO     then new(source.readlines.join, 'STDIN')
        when String then new(source, 'string')
        end
      end

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reek-2.1.0 lib/reek/source/source_code.rb