class Rubocop::Cop::Style::StringLiterals

Checks for uses of double quotes where single quotes would do.

Constants

MSG

Public Instance Methods

autocorrect_action(node) click to toggle source
# File lib/rubocop/cop/style/string_literals.rb, line 29
def autocorrect_action(node)
  replace(node.loc.begin, "'")
  replace(node.loc.end, "'")
end
on_str(node) click to toggle source
# File lib/rubocop/cop/style/string_literals.rb, line 11
def on_str(node)
  # Constants like __FILE__ are handled as strings,
  # but don't respond to begin.
  return unless node.loc.respond_to?(:begin)

  # regex matches IF there is a ' or there is a \\ in the string that
  # is not preceeded/followed by another \\ (e.g. "\\x34") but not
  # "\\\\"
  if node.loc.expression.source !~ /('|([^\\]|\A)\\([^\\]|\Z))/ &&
      node.loc.begin.is?('"')
    add_offence(:convention, node.loc.expression, MSG)
    do_autocorrect(node)
  end
end