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
Rule proposal: Use named capture groups for regex #11381
Rule proposal: Use named capture groups for regex #11381
Comments
I'm in favor of this in general, although I think I would characterize this as a suggestion rather than a problem (i.e., the rule doesn't report a known/very likely actual bug, but rather can help avoid a type of bug that might or might not occur in a project). |
@platinumazure Yeah, I was not sure about that. It could potentially catch a problem, but you're right, the main intention is readability. Changed. |
Anyone wants to champion this rule? It has enough up-votes to be accepted, just need a champion. |
✋ |
Sometimes, people use parentheses to indicate grouping in a regex without using it as a capture group, e.g. const regex = /(ab|cd|ef)+/; Would this report an error? It could be replaced with (This comment is cross-posted from #11392 (comment) for more visibility.) |
IMHO, yes, but if you think it's a big problem, there could be an option to opt-out of that. |
* New: add rule "require-named-capture-group" (fixes #11381) * Chore: fix linting errors * Update: change rule name * Update: use `ReferenceTracker` * Update: ignore regexp syntax errors * Update: detect "unicode" flag * Update: report group instead of AST node * Docs: improve words * Chore: enable early return * Update: improve the message * Chore: simplify message Co-Authored-By: g-plane <g-plane@hotmail.com>
Please describe what the rule should do:
Enforce using named capture groups for regexes instead numbered capture groups. Named capture groups make regexes more readable and they can also prevent possible off-by-one errors.
What category of rule is this? (place an "X" next to just one item)
[ ] Warns about a potential error (problem)
[x] Suggests an alternate way of doing something (suggestion)
[ ] Enforces code style (layout)
[ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about:
For reference, the first example would look like this with named capture groups:
Why should this rule be included in ESLint (instead of a plugin)?
It's a native JS feature and ESLint already has some other regex-related rules.
Are you willing to submit a pull request to implement this rule?
No
The text was updated successfully, but these errors were encountered: