Sha256: 566f372013bcdec5b018ddca352557c0a8ea6e8741c9c6d2423f5cbbf2cf46a9

Contents?: true

Size: 1.76 KB

Versions: 17

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for exact regexp match inside Regexp literals.
      #
      # @example
      #
      #   # bad
      #   string =~ /\Astring\z/
      #   string === /\Astring\z/
      #   string.match(/\Astring\z/)
      #   string.match?(/\Astring\z/)
      #
      #   # good
      #   string == 'string'
      #
      #   # bad
      #   string !~ /\Astring\z/
      #
      #   # good
      #   string != 'string'
      #
      class ExactRegexpMatch < Base
        extend AutoCorrector

        MSG = 'Use `%<prefer>s`.'
        RESTRICT_ON_SEND = %i[=~ === !~ match match?].freeze

        # @!method exact_regexp_match(node)
        def_node_matcher :exact_regexp_match, <<~PATTERN
          (call
            _ {:=~ :=== :!~ :match :match?}
            (regexp
              (str $_)
              (regopt)))
        PATTERN

        def on_send(node)
          return unless (receiver = node.receiver)
          return unless (regexp = exact_regexp_match(node))

          parsed_regexp = Regexp::Parser.parse(regexp)
          return unless exact_match_pattern?(parsed_regexp)

          prefer = "#{receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"

          add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
            corrector.replace(node, prefer)
          end
        end
        alias on_csend on_send

        private

        def exact_match_pattern?(parsed_regexp)
          tokens = parsed_regexp.map(&:token)
          return false unless tokens[0] == :bos && tokens[1] == :literal && tokens[2] == :eos

          !parsed_regexp[1].quantifier
        end

        def new_method(node)
          node.method?(:!~) ? '!=' : '=='
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.69.1 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.69.0 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.68.0 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.67.0 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.66.1 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.66.0 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.65.1 lib/rubocop/cop/style/exact_regexp_match.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.65.0 lib/rubocop/cop/style/exact_regexp_match.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.64.1 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.63.4 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.63.3 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.63.2 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.63.1 lib/rubocop/cop/style/exact_regexp_match.rb
rubocop-1.63.0 lib/rubocop/cop/style/exact_regexp_match.rb