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

Cop idea: Referencing named capture with a number #7754

Closed
pocke opened this issue Feb 25, 2020 · 4 comments
Closed

Cop idea: Referencing named capture with a number #7754

pocke opened this issue Feb 25, 2020 · 4 comments
Labels
feature request stale Issues that haven't been active in a while

Comments

@pocke
Copy link
Collaborator

pocke commented Feb 25, 2020

Is your feature request related to a problem? Please describe.

Regexp's named capture can be referenced with a number, but referencing with a name is preferred.

# bad
'str'.match(/(?<X>s)/)[1]
# => "s"

# good
'str'.match(/(?<X>s)/)[:X]
# => "s"

# also good
'str'.match(/(?<X>s)/)['X']
# => "s"

Describe the solution you'd like

Add a cop to detect the problem.

Describe alternatives you've considered

Nothing

Additional context

We will be able to the cop with regexp_parser gem. ref: #7746

@choosen
Copy link

choosen commented Mar 13, 2020

should we detect also following example? :

match_data = 'str'.match /(?<X>s)/
match_data[1]

@pocke
Copy link
Collaborator Author

pocke commented Mar 14, 2020

I think the cop should detect the problem ideally, but I guess it is difficult with the current RuboCop implementation.Because RuboCop's analysis for local variable assigments is not enougth.
I think a simple implementation, which detects no-assignment cases only, is enough for the first step.

tejasbubane added a commit to tejasbubane/ruby-style-guide that referenced this issue Mar 29, 2020
tejasbubane added a commit to tejasbubane/ruby-style-guide that referenced this issue Mar 29, 2020
bbatsov pushed a commit to rubocop/ruby-style-guide that referenced this issue Mar 31, 2020
@stale
Copy link

stale bot commented Sep 11, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale Issues that haven't been active in a while label Sep 11, 2020
@stale
Copy link

stale bot commented Dec 11, 2020

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

@stale stale bot closed this as completed Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request stale Issues that haven't been active in a while
Projects
None yet
Development

No branches or pull requests

2 participants