Module: RuboCop::Cop::StringLiteralsHelp

Includes:
StringHelp
Included in:
RuboCop::Cop::Style::StringLiterals, RuboCop::Cop::Style::StringLiteralsInInterpolation
Defined in:
lib/rubocop/cop/mixin/string_literals_help.rb

Overview

Common functionality for cops checking single/double quotes.

Constant Summary

Constants included from StringHelp

RuboCop::Cop::StringHelp::ESCAPED_CHAR_REGEXP

Instance Method Summary collapse

Methods included from StringHelp

#inside_interpolation?, #on_regexp, #on_str

Instance Method Details

#autocorrect(node) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/rubocop/cop/mixin/string_literals_help.rb', line 19

def autocorrect(node)
  lambda do |corrector|
    replacement = node.loc.begin.is?('"') ? "'" : '"'
    corrector.replace(node.loc.begin, replacement)
    corrector.replace(node.loc.end, replacement)
  end
end

#wrong_quotes?(node, style) ⇒ Boolean

Returns:

  • (Boolean)


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

def wrong_quotes?(node, style)
  src = node.loc.expression.source
  return false if src.start_with?('%') || src.start_with?('?')
  if style == :single_quotes
    src !~ /'/ && src !~ StringHelp::ESCAPED_CHAR_REGEXP
  else
    src !~ /" | \\ | \#/x
  end
end