Class: Rubocop::Cop::PercentLiterals

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/percent_literals.rb

Constant Summary collapse

ERROR_MESSAGE =
'The use of %s is discouraged.'
BAD_LITERALS =
{
  on_tstring_beg: '%q',
  on_symbeg: '%s',
  on_backtick: '%x'
}

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/percent_literals.rb', line 13

def inspect(file, source, tokens, sexp)
  tokens.each_index do |ix|
    t = tokens[ix]
    bad_token = BAD_LITERALS[t.type]
    if bad_token && t.text.downcase.start_with?(bad_token)
      add_offence(:convention, t.pos.lineno,
                  sprintf(ERROR_MESSAGE, t.text[0, 2]))
    end
  end
end