Sha256: 9bfe432acb63703573f3c89082cbac5fd8545232e9ae877bec001829833a5541

Contents?: true

Size: 1.16 KB

Versions: 131

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # 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.
    module StringHelp
      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

      def on_regexp(node)
        ignore_node(node)
      end

      private

      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
    end
  end
end

Version data entries

131 entries across 131 versions & 12 rubygems

Version Path
rubocop-1.71.0 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.70.0 lib/rubocop/cop/mixin/string_help.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/string_help.rb
rubocop-1.69.2 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.69.1 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.69.0 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.68.0 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.67.0 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.66.1 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.66.0 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.65.1 lib/rubocop/cop/mixin/string_help.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/string_help.rb
rubocop-1.65.0 lib/rubocop/cop/mixin/string_help.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/string_help.rb
rubocop-1.64.1 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.63.4 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.63.3 lib/rubocop/cop/mixin/string_help.rb
rubocop-1.63.2 lib/rubocop/cop/mixin/string_help.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/string_help.rb
rubocop-1.63.1 lib/rubocop/cop/mixin/string_help.rb