lib/reek/source/source_code.rb in reek-4.6.1 vs lib/reek/source/source_code.rb in reek-4.6.2

- old
+ new

@@ -1,12 +1,14 @@ # frozen_string_literal: true + require_relative '../cli/silencer' Reek::CLI::Silencer.silently do - require 'parser/ruby23' + require 'parser/ruby24' end require_relative '../tree_dresser' require_relative '../ast/node' +require_relative '../ast/builder' require_relative '../errors/parse_error' # Opt in to new way of representing lambdas Parser::Builders::Default.emit_lambda = true @@ -24,11 +26,11 @@ # Initializer. # # code - Ruby code as String # origin - 'STDIN', 'string' or a filepath as String # parser - the parser to use for generating AST's out of the given source - def initialize(code:, origin:, parser: Parser::Ruby23) + def initialize(code:, origin:, parser: default_parser) @source = code @origin = origin @parser = parser end @@ -80,15 +82,18 @@ # where each node is possibly adorned with our SexpExtensions (see ast/ast_node_class_map # and ast/sexp_extensions for details). # # @return [Anonymous subclass of Reek::AST::Node] the AST presentation # for the given source + # :reek:TooManyStatements: { max_statements: 7 } def syntax_tree @syntax_tree ||= begin + buffer = Parser::Source::Buffer.new(origin, 1) + buffer.source = source begin - ast, comments = parser.parse_with_comments(source, origin) + ast, comments = parser.parse_with_comments(buffer) rescue Racc::ParseError, Parser::SyntaxError => error raise Errors::ParseError, origin: origin, original_exception: error end # See https://whitequark.github.io/parser/Parser/Source/Comment/Associator.html @@ -98,8 +103,17 @@ end private attr_reader :parser, :source + + # :reek:UtilityFunction + def default_parser + Parser::Ruby24.new(AST::Builder.new).tap do |parser| + diagnostics = parser.diagnostics + diagnostics.all_errors_are_fatal = true + diagnostics.ignore_warnings = true + end + end end end end