Skip to content

Commit

Permalink
feat: prefer-named-capture-group add suggestions (#16544)
Browse files Browse the repository at this point in the history
* feat: [prefer-named-capture-group] add suggestions

* Add test for proper parenthesis matching

* fix: only suggest when there are no \s

* More granular checking of suggestion viability

* Feedback: return null in more cases, no more indexOf, suggestions: null

* Account for existing temps
  • Loading branch information
JoshuaKGoldberg committed Nov 27, 2022
1 parent ade621d commit e6a865d
Show file tree
Hide file tree
Showing 2 changed files with 423 additions and 25 deletions.
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

0 comments on commit e6a865d

Please sign in to comment.