Sha256: e337edbc2138cf6b39f0af54194feda3156e0421ca3e20ddaec466b97114e106

Contents?: true

Size: 1.19 KB

Versions: 185

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Identifies places where `URI.regexp` is obsolete and should
      # not be used. Instead, use `URI::DEFAULT_PARSER.make_regexp`.
      #
      # @example
      #   # bad
      #   URI.regexp('http://example.com')
      #
      #   # good
      #   URI::DEFAULT_PARSER.make_regexp('http://example.com')
      #
      class UriRegexp < Base
        extend AutoCorrector

        MSG = '`%<current>s` is obsolete and should not be used. Instead, use `%<preferred>s`.'
        URI_CONSTANTS = ['URI', '::URI'].freeze
        RESTRICT_ON_SEND = %i[regexp].freeze

        def on_send(node)
          return unless node.receiver
          return unless URI_CONSTANTS.include?(node.receiver.source)

          argument = node.first_argument ? "(#{node.first_argument.source})" : ''
          preferred_method = "#{node.receiver.source}::DEFAULT_PARSER.make_regexp#{argument}"
          message = format(MSG, current: node.source, preferred: preferred_method)

          add_offense(node.loc.selector, message: message) do |corrector|
            corrector.replace(node, preferred_method)
          end
        end
      end
    end
  end
end

Version data entries

185 entries across 178 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.66.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.66.0 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.65.1 lib/rubocop/cop/lint/uri_regexp.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.65.0 lib/rubocop/cop/lint/uri_regexp.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/uri_regexp.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/uri_regexp.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/uri_regexp.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.64.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.63.4 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.63.3 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.63.2 lib/rubocop/cop/lint/uri_regexp.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.63.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.63.0 lib/rubocop/cop/lint/uri_regexp.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.62.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.62.0 lib/rubocop/cop/lint/uri_regexp.rb