Skip to content

Add regexp/no-misleading-backreference #751

@ota-meshi

Description

@ota-meshi

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 🤔

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions