Sha256: 114dcd42a989c79e5a4ab3d62ec135da6be85069ddaab87d922220588ea3ff10

Contents?: true

Size: 1.29 KB

Versions: 151

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for the instantiation of regexp using redundant `Regexp.new` or `Regexp.compile`.
      # Autocorrect replaces to regexp literal which is the simplest and fastest.
      #
      # @example
      #
      #   # bad
      #   Regexp.new(/regexp/)
      #   Regexp.compile(/regexp/)
      #
      #   # good
      #   /regexp/
      #   Regexp.new('regexp')
      #   Regexp.compile('regexp')
      #
      class RedundantRegexpConstructor < Base
        extend AutoCorrector

        MSG = 'Remove the redundant `Regexp.%<method>s`.'
        RESTRICT_ON_SEND = %i[new compile].freeze

        # @!method redundant_regexp_constructor(node)
        def_node_matcher :redundant_regexp_constructor, <<~PATTERN
          (send
            (const {nil? cbase} :Regexp) {:new :compile}
            (regexp $... (regopt $...)))
        PATTERN

        def on_send(node)
          return unless (regexp, regopt = redundant_regexp_constructor(node))

          add_offense(node, message: format(MSG, method: node.method_name)) do |corrector|
            pattern = regexp.map(&:source).join
            regopt = regopt.join

            corrector.replace(node, "/#{pattern}/#{regopt}")
          end
        end
      end
    end
  end
end

Version data entries

151 entries across 150 versions & 15 rubygems

Version Path
rubocop-1.72.1 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.72.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.71.2 lib/rubocop/cop/style/redundant_regexp_constructor.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.71.1 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.71.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.70.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.69.2 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.69.1 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.69.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.68.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.67.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.66.1 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.66.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.65.1 lib/rubocop/cop/style/redundant_regexp_constructor.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.65.0 lib/rubocop/cop/style/redundant_regexp_constructor.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/redundant_regexp_constructor.rb
rubocop-1.64.1 lib/rubocop/cop/style/redundant_regexp_constructor.rb