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?, #ignore_node, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

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

Instance Method Details

#on_str(node) ⇒ Object



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

def on_str(node)
  text, = *node

  # Constants like __FILE__ and __DIR__ are created as strings,
  # but don't respond to begin.
  return unless node.loc.respond_to?(:begin)

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