Skip to content

Commit

Permalink
feat: prefer-named-capture-group support v flag (#17409)
Browse files Browse the repository at this point in the history
* feat: `prefer-named-capture-group` support `v` flag

* chore: add comment

* Update lib/rules/prefer-named-capture-group.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

---------

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
ota-meshi and mdjermanovic committed Jul 25, 2023
1 parent 8ca8b50 commit 2a35f3e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
13 changes: 8 additions & 5 deletions lib/rules/prefer-named-capture-group.js
Expand Up @@ -112,14 +112,17 @@ module.exports = {
* @param {string} pattern The regular expression pattern to be checked.
* @param {ASTNode} node AST node which contains the regular expression or a call/new expression.
* @param {ASTNode} regexNode AST node which contains the regular expression.
* @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
* @param {string|null} flags The regular expression flags to be checked.
* @returns {void}
*/
function checkRegex(pattern, node, regexNode, uFlag) {
function checkRegex(pattern, node, regexNode, flags) {
let ast;

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

// ignore regex syntax errors
Expand Down Expand Up @@ -148,7 +151,7 @@ module.exports = {
return {
Literal(node) {
if (node.regex) {
checkRegex(node.regex.pattern, node, node, node.regex.flags.includes("u"));
checkRegex(node.regex.pattern, node, node, node.regex.flags);
}
},
Program(node) {
Expand All @@ -166,7 +169,7 @@ module.exports = {
const flags = getStringIfConstant(refNode.arguments[1]);

if (regex) {
checkRegex(regex, refNode, refNode.arguments[0], flags && flags.includes("u"));
checkRegex(regex, refNode, refNode.arguments[0], flags);
}
}
}
Expand Down
33 changes: 32 additions & 1 deletion tests/lib/rules/prefer-named-capture-group.js
Expand Up @@ -70,7 +70,17 @@ ruleTester.run("prefer-named-capture-group", rule, {
}
`,
env: { es2020: true }
}
},

// ES2024
"new RegExp('(?<c>[[A--B]])', 'v')",

/*
* This testcase checks if the rule understands the v flag correctly.
* Without the v flag, `([\q])` is considered a valid regex and the rule reports,
* but if the v flag is understood correctly the rule does not because of a syntax error.
*/
String.raw`new RegExp('([\\q])', 'v')` // SyntaxError
],

invalid: [
Expand Down Expand Up @@ -591,6 +601,27 @@ ruleTester.run("prefer-named-capture-group", rule, {
}
]
}]
},

// ES2024
{
code: "new RegExp('([[A--B]])', 'v')",
errors: [{
messageId: "required",
type: "NewExpression",
data: { group: "([[A--B]])" },
line: 1,
column: 1,
suggestions: [
{
messageId: "addGroupName",
output: "new RegExp('(?<temp1>[[A--B]])', 'v')"
},
{
messageId: "addNonCapture",
output: "new RegExp('(?:[[A--B]])', 'v')"
}]
}]
}
]
});

0 comments on commit 2a35f3e

Please sign in to comment.