Skip to content
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

Merged

Conversation

owst
Copy link
Contributor

@owst owst commented Aug 31, 2020

As mentioned in #8593 by @marcandre


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@owst owst force-pushed the use_regexp_parser_in_redundant_regexp_cops branch from ebc9252 to c474274 Compare August 31, 2020 22:29
Copy link
Contributor

@marcandre marcandre left a 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|
Copy link
Contributor

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...

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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!

Copy link
Contributor

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.

lib/rubocop/cop/style/redundant_regexp_character_class.rb Outdated Show resolved Hide resolved
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][])
Copy link
Collaborator

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.

@owst
Copy link
Contributor Author

owst commented Sep 5, 2020

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

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 8, 2020

The changes look good, but your branch has to be rebased on top of the current master branch due to merge conflicts.

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 11, 2020

@owst Can you rebase and squash, please?

@owst
Copy link
Contributor Author

owst commented Sep 11, 2020

@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 regexp_parser doesn't allow setting Regexp options when parsing a String pattern rather than Regexp directly. Therefore, regexp_parser treats the comment as a literal, which can be flagged by the cops in question e.g.

/
  foo # [x] <- flagged as redundant brackets if comment is not ignored
/x

@jaynetics
Copy link
Contributor

@owst we can add an option to regexp_parser to pass regexp flags / options when parsing a regexp body from a string if that is of help here.

@owst
Copy link
Contributor Author

owst commented Sep 12, 2020

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
…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_regexp_parsed_tree' into use_regexp_parser_in_redundant_regexp_cops
…nd_blank_interpolations_in_regexp_parsed_tree
…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
@owst owst requested a review from marcandre September 30, 2020 22:41
Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@owst
Copy link
Contributor Author

owst commented Oct 4, 2020

@bbatsov / @marcandre - anything more for me to do on this, or can it be merged now?

@bbatsov bbatsov merged commit 269ac4c into rubocop:master Oct 4, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 4, 2020

🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants