-
-
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
Use regexp_parser
to improve Style/RedundantRegexp...
cops
#8625
Use regexp_parser
to improve Style/RedundantRegexp...
cops
#8625
Conversation
As mentioned in rubocop#8593 by @marcandre
ebc9252
to
c474274
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.
Good stuff 😄. I just had a quick look, here are some comments
end | ||
|
||
def each_single_element_character_class(node) | ||
Regexp::Parser.parse(pattern_source(node)).each_expression do |expr| |
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 use node.parsed_tree
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.
Unfortunately parsed_tree
doesn't support patterns containing an interpolation. @marcandre I see you added that method - do you recall why you return nil
for patterns containing interpolations? As it stands, this diff causes a bunch of spec failures:
diff --git a/lib/rubocop/cop/style/redundant_regexp_character_class.rb b/lib/rubocop/cop/style/redundant_regexp_character_class.rb
index 214a84743..7a3251994 100644
--- a/lib/rubocop/cop/style/redundant_regexp_character_class.rb
+++ b/lib/rubocop/cop/style/redundant_regexp_character_class.rb
@@ -54,7 +54,8 @@ module RuboCop
end
def each_single_element_character_class(node)
- Regexp::Parser.parse(pattern_source(node)).each_expression do |expr|
+ # Regexp::Parser.parse(pattern_source(node)).each_expression do |expr|
+ node.parsed_tree.each_expression do |expr|
next if expr.type != :set || expr.expressions.size != 1
next if expr.negative? || %i[posixclass set].include?(expr.expressions.first.type)
mostly of the form NoMethodError: undefined method
each_expression' for nil:NilClass`.
N.B. I introduced pattern_source
(which replaces interpolation and comments with spaces) to remove (as far as the cops are concerned) the "effect" of the interpolation, whilst preserving the indentation/width of the interpolation in the pattern
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.
Perhaps we could incorporate the "interpolation-blanking" into parsed_tree
, or some other mechanism of supporting interpolation?
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 like the idea of blanking the interpolations, makes sense and could be quite useful.
Maybe parsed_tree
could take a keyword argument :interpolation
with value :ignore
or :blank
?
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've had a go at this - please let me know what you think @marcandre!
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.
That's awesome.
Ideally, the changes to parsed_tree
would be in a different PR, and with at least a test.
It's not clear to me that replacing the interpolations with spaces has no effect, I thought you were going to simply delete them...
I also don't know why the treatment of the comments.
CHANGELOG.md
Outdated
@@ -44,6 +44,7 @@ | |||
* [#8517](https://github.com/rubocop-hq/rubocop/pull/8517): Make `Style/HashTransformKeys` and `Style/HashTransformValues` aware of `to_h` with block. ([@eugeneius][]) | |||
* [#8529](https://github.com/rubocop-hq/rubocop/pull/8529): Mark `Lint/FrozenStringLiteralComment` as `Safe`, but with unsafe auto-correction. ([@marcandre][]) | |||
* [#8602](https://github.com/rubocop-hq/rubocop/pull/8602): Fix usage of `to_enum(:scan, regexp)` to work on TruffleRuby. ([@jaimerave][]) | |||
* [#8625](https://github.com/rubocop-hq/rubocop/pull/8625): Improve `Style/RedundantRegexpCharacterClass` and `Style/RedundantRegexpEscape` by using `regexp_parser` gem. ([@owst][]) |
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.
You'll have to move this under master
.
…_in_redundant_regexp_cops
…_parser_in_redundant_regexp_cops
Thanks for the review @marcandre - I've pushed a conflict resolution (and move of the CHANGELOG entry) and have hopefully adequately responded to your comments |
The changes look good, but your branch has to be rebased on top of the current |
…_in_redundant_regexp_cops
…_parser_in_redundant_regexp_cops
…_regexp_parser_in_redundant_regexp_cops
@owst Can you rebase and squash, please? |
@bbatsov sorry, I need to deal with @marcandre's comment here #8625 (comment) - I'll need to pull out another PR for some of this change. For the record, the comment-in-extended-regexp-blanking is (currently) required because
|
@owst we can add an option to |
Hi @jaynetics, thanks for the suggestion! I was looking to do just that, I'll try and pull something together over the weekend and open a PR 👍 |
…nto use_regexp_parser_in_redundant_regexp_cops
…ster' into use_regexp_parser_in_redundant_regexp_cops
…_in_redundant_regexp_cops
…_parser_in_redundant_regexp_cops
…_regexp_parser_in_redundant_regexp_cops
…ed_tree' into use_regexp_parser_in_redundant_regexp_cops
…ed_tree' into use_regexp_parser_in_redundant_regexp_cops
…xp_parsed_tree' into use_regexp_parser_in_redundant_regexp_cops
…_in_redundant_regexp_cops
…nd_blank_interpolations_in_regexp_parsed_tree
…ments_and_blank_interpolations_in_regexp_parsed_tree
…_parser_in_redundant_regexp_cops
…ed_tree' into use_regexp_parser_in_redundant_regexp_cops
…xp_parsed_tree' into use_regexp_parser_in_redundant_regexp_cops
…in_regexp_parsed_tree' into use_regexp_parser_in_redundant_regexp_cops
…nd_blank_interpolations_in_regexp_parsed_tree
…_in_redundant_regexp_cops
…_parser_in_redundant_regexp_cops
…nd_blank_interpolations_in_regexp_parsed_tree
…ments_and_blank_interpolations_in_regexp_parsed_tree
…ed_tree' into use_regexp_parser_in_redundant_regexp_cops
…xp_parsed_tree' into use_regexp_parser_in_redundant_regexp_cops
…_in_redundant_regexp_cops
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.
LGTM
@bbatsov / @marcandre - anything more for me to do on this, or can it be merged now? |
🚀 |
As mentioned in #8593 by @marcandre
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 RuboCop for itself, and generates the documentation.