Sha256: 1d0b1bd96ce4b4d0a1b494f06a69f8a6ef8b8888cb231e8e61ec4216d3433091

Contents?: true

Size: 1.2 KB

Versions: 71

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop 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

71 entries across 71 versions & 7 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/uri_regexp.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.29.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.29.0 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.28.2 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.28.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.28.0 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.27.0 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.26.1 lib/rubocop/cop/lint/uri_regexp.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.26.0 lib/rubocop/cop/lint/uri_regexp.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/uri_regexp.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.25.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.25.0 lib/rubocop/cop/lint/uri_regexp.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/uri_regexp.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/uri_regexp.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.24.1 lib/rubocop/cop/lint/uri_regexp.rb
rubocop-1.24.0 lib/rubocop/cop/lint/uri_regexp.rb