Module: Rubocop::SourceParser
- Defined in:
- lib/rubocop/source_parser.rb
Overview
SourceParser provides a way to parse Ruby source with Parser gem and also parses comment directives which disable arbitrary cops.
Class Method Summary collapse
Class Method Details
.create_parser ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rubocop/source_parser.rb', line 34 def create_parser 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. The problem is caused by a bug # in Racc that is discussed in issue #93 of the whitequark/parser project # on GitHub. parser.diagnostics.all_errors_are_fatal = RUBY_ENGINE != 'ruby' parser.diagnostics.ignore_warnings = false parser end |
.parse(string, name = '(string)') ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rubocop/source_parser.rb', line 13 def parse(string, name = '(string)') source_buffer = Parser::Source::Buffer.new(name, 1) source_buffer.source = string parser = create_parser diagnostics = [] parser.diagnostics.consumer = lambda do |diagnostic| diagnostics << diagnostic end 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 tokens = tokens.map { |t| Token.from_parser_token(t) } if tokens ProcessedSource.new(source_buffer, ast, comments, tokens, diagnostics) end |
.parse_file(path) ⇒ Object
9 10 11 |
# File 'lib/rubocop/source_parser.rb', line 9 def parse_file(path) parse(File.read(path), path) end |