Sha256: 3a7369c77d5d9093a1850f184ed02e4386f0d2840ca067593205d14b26f2913f

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 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 = begin
            Regexp::Parser.parse(regexp)
          rescue Regexp::Parser::Error
            # Upon encountering an invalid regular expression,
            # we aim to proceed and identify any remaining potential offenses.
          end

          return unless parsed_regexp && 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

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-1.69.2 lib/rubocop/cop/style/exact_regexp_match.rb