Sha256: 752d048574914fa02044f5f371726edc1f715b3c60c31e7048b820345501fcc2

Contents?: true

Size: 1.06 KB

Versions: 86

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # 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)
          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, "#{double_colon}URI::DEFAULT_PARSER")
            end
          end
        end
      end
    end
  end
end

Version data entries

86 entries across 86 versions & 5 rubygems

Version Path
rubocop-performance-1.23.1 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.23.0 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.22.1 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.22.0 lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.21.1 lib/rubocop/cop/performance/uri_default_parser.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.0/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.21.0 lib/rubocop/cop/performance/uri_default_parser.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.20.2/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
rubocop-performance-1.20.2 lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/uri_default_parser.rb