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

feat: no-useless-backreference support v flag #17408

Merged
merged 2 commits into from Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" }]
}
]
});