module RuboCop::Cop::StringHelp

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

Public Instance Methods

on_regexp(node) click to toggle source
# File lib/rubocop/cop/mixin/string_help.rb, line 26
def on_regexp(node)
  ignore_node(node)
end
on_str(node) click to toggle source
# File lib/rubocop/cop/mixin/string_help.rb, line 10
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) do |corrector|
      opposite_style_detected
      autocorrect(corrector, node) if respond_to?(:autocorrect, true)
    end
  else
    correct_style_detected
  end
end

Private Instance Methods

inside_interpolation?(node) click to toggle source
# File lib/rubocop/cop/mixin/string_help.rb, line 32
def inside_interpolation?(node)
  # A :begin node inside a :dstr, :dsym, or :regexp node is an interpolation.
  node.ancestors
      .drop_while { |a| !a.begin_type? }
      .any? { |a| a.dstr_type? || a.dsym_type? || a.regexp_type? }
end