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: prefer-named-capture-group support v flag #17409

Merged
merged 3 commits into from Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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} flags The regular expression flags to be checked.
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
* @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
27 changes: 26 additions & 1 deletion tests/lib/rules/prefer-named-capture-group.js
Expand Up @@ -70,7 +70,11 @@ ruleTester.run("prefer-named-capture-group", rule, {
}
`,
env: { es2020: true }
}
},

// ES2024
"new RegExp('(?<c>[[A--B]])', 'v')",
String.raw`new RegExp('([\\q])', 'v')` // SyntaxError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you're saying here is that the regex intentionally has a syntax error? If so, can you expand the comment and explain why that's a useful valid test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing this PR. I added a comment. Could you please check again?

],

invalid: [
Expand Down Expand Up @@ -591,6 +595,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')"
}]
}]
}
]
});