Module: RuboCop::Cop::StringHelp

Included in:
StringLiteralsHelp, RuboCop::Cop::Style::CharacterLiteral
Defined in:
lib/rubocop/cop/mixin/string_help.rb

Overview

Classes that include this module just implement functions to determine what is an offense and how to do auto-correction. They get help with adding offenses for the faulty string nodes, and with filtering out nodes.

Constant Summary collapse

ESCAPED_CHAR_REGEXP =

Regex matches IF there is a ' or there is a \ in the string that is not preceded/followed by another \ (e.g. "\x34") but not "\\".

/(?<! \\) \\{2}* \\ (?! \\)/x

Instance Method Summary collapse

Instance Method Details

#inside_interpolation?(node) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/mixin/string_help.rb', line 31

def inside_interpolation?(node)
  # A :begin node inside a :dstr node is an interpolation.
  begin_found = false
  node.each_ancestor.any? do |a|
    begin_found = true if a.type == :begin
    begin_found && a.type == :dstr
  end
end

#on_regexp(node) ⇒ Object



27
28
29
# File 'lib/rubocop/cop/mixin/string_help.rb', line 27

def on_regexp(node)
  ignore_node(node)
end

#on_str(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/mixin/string_help.rb', line 14

def on_str(node)
  # Constants like __FILE__ are handled as strings,
  # but don't respond to begin.
  return unless node.loc.respond_to?(:begin) && node.loc.begin
  return if part_of_ignored_node?(node)

  if offense?(node)
    add_offense(node, :expression) { opposite_style_detected }
  else
    correct_style_detected
  end
end