|
| 1 | +/** |
| 2 | + * @fileoverview Rule to enforce requiring named capture groups in regular expression. |
| 3 | + * @author Pig Fang <https://github.com/g-plane> |
| 4 | + */ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const { |
| 13 | + CALL, |
| 14 | + CONSTRUCT, |
| 15 | + ReferenceTracker, |
| 16 | + getStringIfConstant |
| 17 | +} = require("eslint-utils"); |
| 18 | +const regexpp = require("regexpp"); |
| 19 | + |
| 20 | +//------------------------------------------------------------------------------ |
| 21 | +// Helpers |
| 22 | +//------------------------------------------------------------------------------ |
| 23 | + |
| 24 | +const parser = new regexpp.RegExpParser(); |
| 25 | + |
| 26 | +//------------------------------------------------------------------------------ |
| 27 | +// Rule Definition |
| 28 | +//------------------------------------------------------------------------------ |
| 29 | + |
| 30 | +module.exports = { |
| 31 | + meta: { |
| 32 | + type: "suggestion", |
| 33 | + |
| 34 | + docs: { |
| 35 | + description: "enforce using named capture group in regular expression", |
| 36 | + category: "Best Practices", |
| 37 | + recommended: false, |
| 38 | + url: "https://eslint.org/docs/rules/prefer-named-capture-group" |
| 39 | + }, |
| 40 | + |
| 41 | + schema: [], |
| 42 | + |
| 43 | + messages: { |
| 44 | + required: "Capture group '{{group}}' should be converted to a named or non-capturing group." |
| 45 | + } |
| 46 | + }, |
| 47 | + |
| 48 | + create(context) { |
| 49 | + |
| 50 | + /** |
| 51 | + * Function to check regular expression. |
| 52 | + * |
| 53 | + * @param {string} regex The regular expression to be check. |
| 54 | + * @param {ASTNode} node AST node which contains regular expression. |
| 55 | + * @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not. |
| 56 | + * @returns {void} |
| 57 | + */ |
| 58 | + function checkRegex(regex, node, uFlag) { |
| 59 | + let ast; |
| 60 | + |
| 61 | + try { |
| 62 | + ast = parser.parsePattern(regex, 0, regex.length, uFlag); |
| 63 | + } catch (_) { |
| 64 | + |
| 65 | + // ignore regex syntax errors |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + regexpp.visitRegExpAST(ast, { |
| 70 | + onCapturingGroupEnter(group) { |
| 71 | + if (!group.name) { |
| 72 | + const locNode = node.type === "Literal" ? node : node.arguments[0]; |
| 73 | + |
| 74 | + context.report({ |
| 75 | + node, |
| 76 | + messageId: "required", |
| 77 | + loc: { |
| 78 | + start: { |
| 79 | + line: locNode.loc.start.line, |
| 80 | + column: locNode.loc.start.column + group.start + 1 |
| 81 | + }, |
| 82 | + end: { |
| 83 | + line: locNode.loc.start.line, |
| 84 | + column: locNode.loc.start.column + group.end + 1 |
| 85 | + } |
| 86 | + }, |
| 87 | + data: { |
| 88 | + group: group.raw |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + Literal(node) { |
| 98 | + if (node.regex) { |
| 99 | + checkRegex(node.regex.pattern, node, node.regex.flags.includes("u")); |
| 100 | + } |
| 101 | + }, |
| 102 | + Program() { |
| 103 | + const scope = context.getScope(); |
| 104 | + const tracker = new ReferenceTracker(scope); |
| 105 | + const traceMap = { |
| 106 | + RegExp: { |
| 107 | + [CALL]: true, |
| 108 | + [CONSTRUCT]: true |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + for (const { node } of tracker.iterateGlobalReferences(traceMap)) { |
| 113 | + const regex = getStringIfConstant(node.arguments[0]); |
| 114 | + const flags = getStringIfConstant(node.arguments[1]); |
| 115 | + |
| 116 | + if (regex) { |
| 117 | + checkRegex(regex, node, flags && flags.includes("u")); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + }; |
| 122 | + } |
| 123 | +}; |
0 commit comments