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

Add "No mix named and numbered captures" rule #793

Merged
merged 2 commits into from Mar 4, 2020

Conversation

pocke
Copy link
Contributor

@pocke pocke commented Feb 24, 2020

I created Lint/MixedRegexpCaptureTypes cop in rubocop/rubocop#7749.
This pull request adds a mention of the problem to the style guide also.

README.adoc Outdated Show resolved Hide resolved
@pocke pocke force-pushed the no-mix-named-and-numbered-captures branch from f63356e to b04abc7 Compare February 24, 2020 17:11
README.adoc Outdated
@@ -4336,6 +4336,26 @@ Use non-capturing groups when you don't use the captured result.
/(?:first|second)/
----

=== No mix named and numbered captures [[no-mix-named-and-numbered-captures]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think Do not mix... is better wording here. Same for the anchor.

=== No mix named and numbered captures [[no-mix-named-and-numbered-captures]]

Do not mix named captures and numbered captures in a Regexp literal.
Because numbered capture is ignored if they're mixed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might be a good idea to illustrate what does this mean in practice.

README.adoc Outdated
# good
/(?<foo>FOO)(?<bar>BAR)/

# good
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd add some comments explaining examples like this one, as I'm pretty sure many people are not familiar with non-capturing groups.

@pocke
Copy link
Contributor Author

pocke commented Mar 2, 2020

@bbatsov Thanks for reviewing!
I've updated the sample code with comments and debug prints for more information.
3b1a80e

@bbatsov bbatsov merged commit eb09e50 into rubocop:master Mar 4, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 4, 2020

Thanks! Great work! 🙇

@pocke pocke deleted the no-mix-named-and-numbered-captures branch March 5, 2020 01:28
olleolleolle added a commit to github-changelog-generator/github-changelog-generator that referenced this pull request Jun 3, 2020
  - This rule is rubocop/ruby-style-guide#793
  - Perhaps we can investigate what can be done with this regular
    expression, to not need those extra capture groups?
olleolleolle added a commit to github-changelog-generator/github-changelog-generator that referenced this pull request Oct 30, 2020
  - This rule is rubocop/ruby-style-guide#793
  - Perhaps we can investigate what can be done with this regular
    expression, to not need those extra capture groups?
olleolleolle added a commit to github-changelog-generator/github-changelog-generator that referenced this pull request Nov 21, 2020
  - This rule is rubocop/ruby-style-guide#793
  - Perhaps we can investigate what can be done with this regular
    expression, to not need those extra capture groups?
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

3 participants