-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Support autocorrection on Performance/RedundantMatch
when receiver is a Regexp literal
#297
Conversation
end | ||
end | ||
|
||
private | ||
|
||
def autocorrect(corrector, node) | ||
# Regexp#match can take a second argument, but this cop doesn't | ||
# register an offense in that case |
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.
Can you leave the comment to node.first_argument.regexp_type?
?
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.
Roger.
Actually, I don't fully understand why this comment is necessary here. Why does this comment mention the second argument? String#match
may also take a second argument, so does it need a similar comment?
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 just keeps the status quo for future maintenance. No text updates would be needed.
# @param [RuboCop::AST::SendNode] node | ||
# @return [Boolean] |
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.
In fact, this type comment is unneeded :-)
# @param [RuboCop::AST::SendNode] node | |
# @return [Boolean] |
@@ -111,4 +111,15 @@ def method(str) | |||
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used. | |||
RUBY | |||
end | |||
|
|||
it 'autocorrects Regexp#match to Regexp#=~' do |
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 'autocorrects Regexp#match to Regexp#=~' do | |
it 'registers an offense when receiver is a Regexp literal' do |
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 it's a good to change about minor wording, but doesn't this completely lose the nuance of autocorrect support?
Or should we prefer that the message be ignorant as to whether autocorrect is supported or not?
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 'registers an offense and corrects when receiver is a Regexp literal' do
, WDYT?
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 sounds good and much clear 👍
expect_offense(<<~TEXT) | ||
something if /regex/.match(str) | ||
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used. | ||
TEXT |
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.
nits:
expect_offense(<<~TEXT) | |
something if /regex/.match(str) | |
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used. | |
TEXT | |
expect_offense(<<~RUBY) | |
something if /regex/.match(str) | |
^^^^^^^^^^^^^^^^^^ Use `=~` in places where the `MatchData` returned by `#match` will not be used. | |
RUBY |
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'm wondering if it's not a good idea to describe something that is not Ruby code as being "RUBY".
Modern editors (e.g. Visual Studio Code) recognize this heredoc delimiter as a language name and do syntax highlighting, but this heredoc includes ^^^...
, which is clearly not Ruby code and therefore it fails to parse
then syntax highlighting for the rest of code will be broken.
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 understand what you want to say. However, I think it's better to adapt to it because RUBY
is used by convention.
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.
Okay, certainly it is more important thing.
Thanks for having the courage to point this out 👍
Performance/RedundantMatch
when receiver is a RegexpPerformance/RedundantMatch
when receiver is a Regexp literal
34b31ab
to
dde1158
Compare
This looks good to me. Can you squash your commits into one? |
…is a Regexp literal
69cd540
to
160998d
Compare
Thanks! |
I found the following pattern is treated as an offense but not autocorrected, so tried to support it.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.