Sha256: 29a2485e0a77d1e3feab736a620b4cdaf1b77f454986526c381799da25e918a5

Contents?: true

Size: 1.95 KB

Versions: 85

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies places where `gsub(/a+/, 'a')` and `gsub!(/a+/, 'a')`
      # can be replaced by `squeeze('a')` and `squeeze!('a')`.
      #
      # The `squeeze('a')` method is faster than `gsub(/a+/, 'a')`.
      #
      # @example
      #
      #   # bad
      #   str.gsub(/a+/, 'a')
      #   str.gsub!(/a+/, 'a')
      #
      #   # good
      #   str.squeeze('a')
      #   str.squeeze!('a')
      #
      class Squeeze < Base
        extend AutoCorrector

        MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
        RESTRICT_ON_SEND = %i[gsub gsub!].freeze

        PREFERRED_METHODS = { gsub: :squeeze, gsub!: :squeeze! }.freeze

        def_node_matcher :squeeze_candidate?, <<~PATTERN
          (call
            $!nil? ${:gsub :gsub!}
            (regexp
              (str $#repeating_literal?)
              (regopt))
            (str $_))
        PATTERN

        # rubocop:disable Metrics/AbcSize
        def on_send(node)
          squeeze_candidate?(node) do |receiver, bad_method, regexp_str, replace_str|
            regexp_str = regexp_str[0..-2] # delete '+' from the end
            regexp_str = interpret_string_escapes(regexp_str)
            return unless replace_str == regexp_str

            good_method = PREFERRED_METHODS[bad_method]
            message = format(MSG, current: bad_method, prefer: good_method)

            add_offense(node.loc.selector, message: message) do |corrector|
              string_literal = to_string_literal(replace_str)
              new_code = "#{receiver.source}#{node.loc.dot.source}#{good_method}(#{string_literal})"

              corrector.replace(node, new_code)
            end
          end
        end
        # rubocop:enable Metrics/AbcSize
        alias on_csend on_send

        private

        def repeating_literal?(regex_str)
          regex_str.match?(/\A(?:#{Util::LITERAL_REGEX})\+\z/o)
        end
      end
    end
  end
end

Version data entries

85 entries across 85 versions & 5 rubygems

Version Path
rubocop-performance-1.23.0 lib/rubocop/cop/performance/squeeze.rb
rubocop-performance-1.22.1 lib/rubocop/cop/performance/squeeze.rb
rubocop-performance-1.22.0 lib/rubocop/cop/performance/squeeze.rb
rubocop-performance-1.21.1 lib/rubocop/cop/performance/squeeze.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.0/lib/rubocop/cop/performance/squeeze.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
rubocop-performance-1.21.0 lib/rubocop/cop/performance/squeeze.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.20.2/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
rubocop-performance-1.20.2 lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/squeeze.rb