Module: RuboCop::Cop::ParserDiagnostic

Included in:
Lint::AmbiguousOperator, Lint::AmbiguousRegexpLiteral, Lint::InvalidCharacterLiteral, Lint::UselessElseWithoutRescue
Defined in:
lib/rubocop/cop/mixin/parser_diagnostic.rb

Overview

Common functionality for cops which processes Parser's diagnostics. This mixin requires its user class to define #relevant_diagnostic?.

def relevant_diagnostic?(diagnostic)
  diagnostic.reason == :my_interested_diagnostic_type
end

If you want to use an alternative offense message rather than the one in Parser's diagnostic, define #alternative_message.

def alternative_message(diagnostic)
  'My custom message'
end

Instance Method Summary collapse

Instance Method Details

#investigate(processed_source) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/mixin/parser_diagnostic.rb', line 19

def investigate(processed_source)
  processed_source.diagnostics.each do |d|
    next unless relevant_diagnostic?(d)

    message = if respond_to?(:alternative_message, true)
                alternative_message(d)
              else
                d.message.capitalize
              end

    add_offense(nil, d.location, message, d.level)
  end
end