Skip to content

Commit

Permalink
feat: no-useless-backreference support v flag (#17408)
Browse files Browse the repository at this point in the history
* feat: `no-useless-backreference` support `v` flag

* test: change test case
  • Loading branch information
ota-meshi committed Jul 25, 2023
1 parent 94954a7 commit a6a3ad4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-useless-backreference.js
Expand Up @@ -95,7 +95,7 @@ module.exports = {
let regExpAST;

try {
regExpAST = parser.parsePattern(pattern, 0, pattern.length, flags.includes("u"));
regExpAST = parser.parsePattern(pattern, 0, pattern.length, { unicode: flags.includes("u"), unicodeSets: flags.includes("v") });
} catch {

// Ignore regular expressions with syntax errors
Expand Down
13 changes: 12 additions & 1 deletion tests/lib/rules/no-useless-backreference.js
Expand Up @@ -142,7 +142,11 @@ ruleTester.run("no-useless-backreference", rule, {
String.raw`new RegExp('\\1(a)\\2', 'ug')`, // \1 would be an error, but \2 is syntax error because of the 'u' flag
String.raw`const flags = 'gus'; RegExp('\\1(a){', flags);`, // \1 would be an error, but the rule is aware of the 'u' flag so this is a syntax error
String.raw`RegExp('\\1(a)\\k<foo>', 'u')`, // \1 would be an error, but \k<foo> produces syntax error because of the u flag
String.raw`new RegExp('\\k<foo>(?<foo>a)\\k<bar>')` // \k<foo> would be an error, but \k<bar> produces syntax error because group <bar> doesn't exist
String.raw`new RegExp('\\k<foo>(?<foo>a)\\k<bar>')`, // \k<foo> would be an error, but \k<bar> produces syntax error because group <bar> doesn't exist

// ES2024
String.raw`new RegExp('([[A--B]])\\1', 'v')`,
String.raw`new RegExp('[[]\\1](a)', 'v')` // SyntaxError
],

invalid: [
Expand Down Expand Up @@ -508,6 +512,13 @@ ruleTester.run("no-useless-backreference", rule, {
{
code: String.raw`const r = RegExp, p = '\\1', s = '(a)'; new r(p + s);`,
errors: [{ messageId: "forward", data: { bref: String.raw`\1`, group: String.raw`(a)` }, type: "NewExpression" }]
},


// ES2024
{
code: String.raw`new RegExp('\\1([[A--B]])', 'v')`,
errors: [{ messageId: "forward", data: { bref: String.raw`\1`, group: String.raw`([[A--B]])` }, type: "NewExpression" }]
}
]
});

0 comments on commit a6a3ad4

Please sign in to comment.