-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Lint/UriRegexp
cop
#4694
Conversation
b52aa7a
to
cd4dc6c
Compare
lib/rubocop/cop/lint/uri_regexp.rb
Outdated
|
||
def_node_matcher :uri_regexp_with_argument?, <<-PATTERN | ||
(send | ||
(const nil :URI) :regexp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not match ::URI.regexp
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. I missed that viewpoint. I fixed by 90bc2a5.
config/enabled.yml
Outdated
@@ -1403,6 +1403,10 @@ Lint/UnreachableCode: | |||
Description: 'Unreachable code.' | |||
Enabled: true | |||
|
|||
Lint/UriRegexp: | |||
Description: 'Use URI::Parser.new.make_regexp` instead of `URI.regexp`.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess URI::DEFAULT_PARSER.make_regexp
is better.
https://github.com/ruby/ruby/blob/4b9aeef1fa2f32a4555f88c6e83539374a1bd8d1/lib/uri/common.rb#L22
require 'benchmark'
require 'uri'
Benchmark.bm(20) do |x|
puts 'empty arg'
x.report('URI::Parser.new'){ 2000.times{URI::Parser.new.make_regexp}}
x.report('URI::DEFAULT_PARSER'){2000.times{URI::DEFAULT_PARSER.make_regexp}}
puts 'with arg'
x.report('URI::Parser.new'){ 2000.times{URI::Parser.new.make_regexp('http://example.com')}}
x.report('URI::DEFAULT_PARSER'){2000.times{URI::DEFAULT_PARSER.make_regexp('http://example.com')}}
end
user system total real
empty arg
URI::Parser.new 2.010000 0.010000 2.020000 ( 2.016804)
URI::DEFAULT_PARSER 0.000000 0.000000 0.000000 ( 0.000144)
with arg
URI::Parser.new 2.460000 0.000000 2.460000 ( 2.451649)
URI::DEFAULT_PARSER 0.330000 0.000000 0.330000 ( 0.331841)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see! Because it does not generate every time like URI::Parser.new
, the constant URI::DEFAULT_PARSER
is fast. I fixed by 88ff097.
d6df59d
to
88ff097
Compare
lib/rubocop/cop/lint/uri_regexp.rb
Outdated
|
||
def_node_matcher :uri_regexp_with_argument?, <<-PATTERN | ||
(send | ||
(const $_ :URI) :regexp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern matches MyClass::URI.regexp('str')
also.
$ ruby-parse -e ::URI; MyClass::URI
(begin
(const
(cbase) :URI)
(const
(const nil :MyClass) :URI))
Could you change this matcher to use {nil cbase}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure 💦 I fixed by ced09d3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
ced09d3
to
a0e6018
Compare
lib/rubocop/cop/lint/uri_regexp.rb
Outdated
private | ||
|
||
def register_offense(node, top_level: '', arg: '') | ||
format = format(MSG, top_level, arg, top_level, arg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MSG
format string could use named fragments to avoid having to pass duplicate arguments. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for telling me how to write cool. I fixed by 21f5f1b.
b8e25e1
to
21f5f1b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
config/enabled.yml
Outdated
@@ -1403,6 +1403,10 @@ Lint/UnreachableCode: | |||
Description: 'Unreachable code.' | |||
Enabled: true | |||
|
|||
Lint/UriRegexp: | |||
Description: 'Use URI::DEFAULT_PARSER.make_regexp` instead of `URI.regexp`.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this typo.
-URI::DEFAULT_PARSER.make_regexp`
+`URI::DEFAULT_PARSER.make_regexp`
Before I saw this, I wasn't sure why this cop was being introduce. Now, I am just a bit disappointed with Ruby. Why would they change a static method for an instance method? I like that the method has been moved to |
|
||
it 'registers an offense when using `URI.regexp` with argument' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
URI.regexp('http://example.com') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about having the offense highlight more of the expression? It seems like this should highlight URI.regexp
or URI.regexp('http://example.com')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know if that is correct, but since URI
module does not change, this cop focused on only regexp
.
Thanks for your comment. I also replaced |
lib/rubocop/cop/lint/uri_regexp.rb
Outdated
module Cop | ||
module Lint | ||
# This cop identifies places where `URI.regexp` | ||
# can be replaced by `URI::DEFAULT_PARSER.make_regexp`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should also mention that URI.regexp
was deprecated and this is the reason why the cop exists.
Right now it's definitely not clear why this is being flagged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I improved to the offense message that URI.regexp
is obsolete. 00835fb
What do you think about this?
lib/rubocop/cop/lint/uri_regexp.rb
Outdated
# | ||
# @example | ||
# # bad | ||
# URI.regexp("http://example.com") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use single quotes here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed by c5bf278.
06fb0ea
to
2e121ea
Compare
2e121ea
to
00835fb
Compare
Follow up for #4254 (comment).
Feature
This cop identifies places where
URI.regexp
can be replaced byURI::Parser.new.make_regexp
.Target Problem
Suppress the following Ruby's warnings.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).rake spec
) are passing.rake internal_investigation
.and description in grammatically correct, complete sentences.
rake generate_cops_documentation
(required only when you've added a new cop or changed the configuration/documentation of an existing cop).