-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri_default_parser.rb
40 lines (35 loc) · 1.06 KB
/
uri_default_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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