Sha256: 615e46c8c5d847b206591596968b652f1b2d89a717e764f5a541d66c84dae3a5

Contents?: true

Size: 1.12 KB

Versions: 20

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop identifies places where `URI::Parser.new`
      # can be replaced by `URI::DEFAULT_PARSER`.
      #
      # @example
      #   # bad
      #   URI::Parser.new
      #
      #   # good
      #   URI::DEFAULT_PARSER
      #
      class UriDefaultParser < Base
        extend AutoCorrector

        MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
              '`%<double_colon>sURI::Parser.new`.'
        RESTRICT_ON_SEND = %i[new].freeze

        def_node_matcher :uri_parser_new?, <<~PATTERN
          (send
            (const
              (const ${nil? cbase} :URI) :Parser) :new)
        PATTERN

        def on_send(node)
          return unless uri_parser_new?(node) do |captured_value|
            double_colon = captured_value ? '::' : ''
            message = format(MSG, double_colon: double_colon)

            add_offense(node, message: message) do |corrector|
              corrector.replace(node.loc.expression, "#{double_colon}URI::DEFAULT_PARSER")
            end
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/uri_default_parser.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/uri_default_parser.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.13.3 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.13.2 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.13.1 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.13.0 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.12.0 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.11.5 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.11.4 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.11.3 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.11.2 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.11.1 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.11.0 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.10.2 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.10.1 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.10.0 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.9.2 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.9.1 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.9.0 lib/rubocop/cop/performance/uri_default_parser.rb