class Rubocop::CLI
def self.parse(file)
def self.parse(file) parser = Parser::CurrentRuby.new # On JRuby and Rubinius, there's a risk that we hang in # tokenize() if we don't set the all errors as fatal flag. parser.diagnostics.all_errors_are_fatal = RUBY_ENGINE != 'ruby' parser.diagnostics.ignore_warnings = false diagnostics = [] parser.diagnostics.consumer = lambda do |diagnostic| diagnostics << diagnostic end source_buffer = Parser::Source::Buffer.new(file, 1) yield source_buffer begin ast, comments, tokens = parser.tokenize(source_buffer) rescue Parser::SyntaxError # rubocop:disable HandleExceptions # All errors are in diagnostics. No need to handle exception. end if tokens tokens = tokens.map do |t| type, details = *t text, range = *details Rubocop::Cop::Token.new(range, type, text) end end syntax_offences = diagnostics.map do |d| Cop::Offence.new(d.level, d.location, "#{d.message}", 'Syntax') end source = source_buffer.source.split($RS) [ast, comments, tokens, source_buffer, source, syntax_offences] end