eslint-plugin-regexp icon indicating copy to clipboard operation
eslint-plugin-regexp copied to clipboard

Add `regexp/no-misleading-backreference`

Open ota-meshi opened this issue 1 year ago • 2 comments

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 avatar Jun 28 '24 22:06 ota-meshi

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

fisker avatar Jun 29 '24 01:06 fisker

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.

ota-meshi avatar Jun 29 '24 05:06 ota-meshi