Class: Rubocop::Cop::StringLiterals

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

Constant Summary collapse

MSG =
"Prefer single-quoted strings when you don't need " +
'string interpolation or special symbols.'

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

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

Constructor Details

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

Instance Method Details

#inspect(source, tokens, ast, comments) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/string_literals.rb', line 9

def inspect(source, tokens, ast, comments)
  on_node(:str, ast, :dstr) do |s|
    text = s.to_a[0]

    if text !~ /['\n\t\r]/ && s.loc.expression.source[0] == '"'
      add_offence(:convention,
                  s.loc.line,
                  MSG)
    end
  end
end