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 add suggestions #16544

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
77 changes: 71 additions & 6 deletions lib/rules/prefer-named-capture-group.js
Expand Up @@ -23,6 +23,61 @@ const regexpp = require("regexpp");

const parser = new regexpp.RegExpParser();

/**
* Creates fixer suggestions for the regex, if statically determinable.
* @param {number} groupStart Starting index of the regex group.
* @param {string} pattern The regular expression pattern to be checked.
* @param {string} rawText Source text of the regexNode.
* @param {ASTNode} regexNode AST node which contains the regular expression.
* @returns {Array<SuggestionResult>} Fixer suggestions for the regex, if statically determinable.
*/
function suggestIfPossible(groupStart, pattern, rawText, regexNode) {
switch (regexNode.type) {
case "Literal":
if (typeof regexNode.value === "string" && rawText.includes("\\")) {
return null;
}
break;
case "TemplateLiteral":
if (regexNode.expressions.length || rawText.slice(1, -1) !== pattern) {
return null;
}
break;
default:
return null;
}

const start = regexNode.range[0] + groupStart + 2;

return [
{
fix(fixer) {
const existingTemps = pattern.match(/temp\d+/gu) || [];
const highestTempCount = existingTemps.reduce(
(previous, next) =>
Math.max(previous, Number(next.slice("temp".length))),
0
);

return fixer.insertTextBeforeRange(
[start, start],
`?<temp${highestTempCount + 1}>`
);
},
messageId: "addGroupName"
},
{
fix(fixer) {
return fixer.insertTextBeforeRange(
[start, start],
"?:"
);
},
messageId: "addNonCapture"
}
];
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -38,23 +93,29 @@ module.exports = {
url: "https://eslint.org/docs/rules/prefer-named-capture-group"
},

hasSuggestions: true,

schema: [],

messages: {
addGroupName: "Add name to capture group.",
addNonCapture: "Convert group to non-capturing.",
required: "Capture group '{{group}}' should be converted to a named or non-capturing group."
}
},

create(context) {
const sourceCode = context.getSourceCode();

/**
* Function to check regular expression.
* @param {string} pattern The regular expression pattern to be check.
* @param {ASTNode} node AST node which contains regular expression.
* @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.
* @returns {void}
*/
function checkRegex(pattern, node, uFlag) {
function checkRegex(pattern, node, regexNode, uFlag) {
let ast;

try {
Expand All @@ -68,12 +129,16 @@ module.exports = {
regexpp.visitRegExpAST(ast, {
onCapturingGroupEnter(group) {
if (!group.name) {
const rawText = sourceCode.getText(regexNode);
const suggest = suggestIfPossible(group.start, pattern, rawText, regexNode);

context.report({
node,
messageId: "required",
data: {
group: group.raw
}
},
suggest
});
}
}
Expand All @@ -83,7 +148,7 @@ module.exports = {
return {
Literal(node) {
if (node.regex) {
checkRegex(node.regex.pattern, node, node.regex.flags.includes("u"));
checkRegex(node.regex.pattern, node, node, node.regex.flags.includes("u"));
}
},
Program() {
Expand All @@ -101,7 +166,7 @@ module.exports = {
const flags = getStringIfConstant(node.arguments[1]);

if (regex) {
checkRegex(regex, node, flags && flags.includes("u"));
checkRegex(regex, node, node.arguments[0], flags && flags.includes("u"));
}
}
}
Expand Down