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.

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
26
27
28
29
30
# File 'lib/rubocop/cop/mixin/string_literals_help.rb', line 19

def autocorrect(node)
  return if node.dstr_type?

  lambda do |corrector|
    str = node.str_content
    if style == :single_quotes
      corrector.replace(node.source_range, to_string_literal(str))
    else
      corrector.replace(node.source_range, str.inspect)
    end
  end
end

#wrong_quotes?(node) ⇒ 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)
  src = node.source
  return false if src.start_with?('%', '?')
  if style == :single_quotes
    !double_quotes_required?(src)
  else
    src !~ /" | \\ | \#(@|\{)/x
  end
end