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 regexp/no-misleading-backreference #751

Open
ota-meshi opened this issue Jun 28, 2024 · 2 comments
Open

Add regexp/no-misleading-backreference #751

ota-meshi opened this issue Jun 28, 2024 · 2 comments
Labels
enhancement New feature or request new rule

Comments

@ota-meshi
Copy link
Owner

ota-meshi commented Jun 28, 2024

Motivation

ES2025 now allows duplicate named capturing groups.
But using numbers to backreference them seems very hard to read and misleading, so I'd like to add a rule to disallow that.

Description

The new rule will flag as an error if a numbered backreference refers to a duplicate named capturing group.

Examples

/* ✓ GOOD */
/^(?:(?<foo>a)|(?<foo>b))\k<foo>$/u.test('aa'); // true
/^(?:(?<foo>a)|(?<foo>b))\k<foo>$/u.test('bb'); // true

/* ✗ BAD */
/^(?:(?<foo>a)|(?<foo>b))\1$/u.test('aa'); // true
/^(?:(?<foo>a)|(?<foo>b))\1$/u.test('bb'); // false

Maybe there's a better rule name 🤔

@ota-meshi ota-meshi added enhancement New feature or request new rule labels Jun 28, 2024
@fisker
Copy link

fisker commented Jun 29, 2024

How about prefer-named-backreference with option to report only if backreference
is duplicate names.

@ota-meshi
Copy link
Owner Author

Hmm... I think the purpose is a bit different from prefer-named-backreference, which always recommends named backreferences, because we don't know whether users should change backreferences or rename named capturing groups in response to the new rule report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new rule
Projects
None yet
Development

No branches or pull requests

2 participants