Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to disallow useless backreferences in regular expressions |
| 3 | * @author Milos Djermanovic |
| 4 | */ |
| 5 | |
| 6 | "use strict"; |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | // Requirements |
| 10 | //------------------------------------------------------------------------------ |
| 11 | |
| 12 | const { CALL, CONSTRUCT, ReferenceTracker, getStringIfConstant } = require("eslint-utils"); |
| 13 | const { RegExpParser, visitRegExpAST } = require("regexpp"); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 14 | |
| 15 | //------------------------------------------------------------------------------ |
| 16 | // Helpers |
| 17 | //------------------------------------------------------------------------------ |
| 18 | |
| 19 | const parser = new RegExpParser(); |
| 20 | |
| 21 | /** |
| 22 | * Finds the path from the given `regexpp` AST node to the root node. |
| 23 | * @param {regexpp.Node} node Node. |
| 24 | * @returns {regexpp.Node[]} Array that starts with the given node and ends with the root node. |
| 25 | */ |
| 26 | function getPathToRoot(node) { |
| 27 | const path = []; |
| 28 | let current = node; |
| 29 | |
| 30 | do { |
| 31 | path.push(current); |
| 32 | current = current.parent; |
| 33 | } while (current); |
| 34 | |
| 35 | return path; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Determines whether the given `regexpp` AST node is a lookaround node. |
| 40 | * @param {regexpp.Node} node Node. |
| 41 | * @returns {boolean} `true` if it is a lookaround node. |
| 42 | */ |
| 43 | function isLookaround(node) { |
| 44 | return node.type === "Assertion" && |
| 45 | (node.kind === "lookahead" || node.kind === "lookbehind"); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Determines whether the given `regexpp` AST node is a negative lookaround node. |
| 50 | * @param {regexpp.Node} node Node. |
| 51 | * @returns {boolean} `true` if it is a negative lookaround node. |
| 52 | */ |
| 53 | function isNegativeLookaround(node) { |
| 54 | return isLookaround(node) && node.negate; |
| 55 | } |
| 56 | |
| 57 | //------------------------------------------------------------------------------ |
| 58 | // Rule Definition |
| 59 | //------------------------------------------------------------------------------ |
| 60 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 61 | /** @type {import('../shared/types').Rule} */ |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 62 | module.exports = { |
| 63 | meta: { |
| 64 | type: "problem", |
| 65 | |
| 66 | docs: { |
| 67 | description: "disallow useless backreferences in regular expressions", |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 68 | recommended: true, |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 69 | url: "https://eslint.org/docs/rules/no-useless-backreference" |
| 70 | }, |
| 71 | |
| 72 | schema: [], |
| 73 | |
| 74 | messages: { |
| 75 | nested: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' from within that group.", |
| 76 | forward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears later in the pattern.", |
| 77 | backward: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears before in the same lookbehind.", |
| 78 | disjunctive: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in another alternative.", |
| 79 | intoNegativeLookaround: "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in a negative lookaround." |
| 80 | } |
| 81 | }, |
| 82 | |
| 83 | create(context) { |
| 84 | |
| 85 | /** |
| 86 | * Checks and reports useless backreferences in the given regular expression. |
| 87 | * @param {ASTNode} node Node that represents regular expression. A regex literal or RegExp constructor call. |
| 88 | * @param {string} pattern Regular expression pattern. |
| 89 | * @param {string} flags Regular expression flags. |
| 90 | * @returns {void} |
| 91 | */ |
| 92 | function checkRegex(node, pattern, flags) { |
| 93 | let regExpAST; |
| 94 | |
| 95 | try { |
| 96 | regExpAST = parser.parsePattern(pattern, 0, pattern.length, flags.includes("u")); |
| 97 | } catch { |
| 98 | |
| 99 | // Ignore regular expressions with syntax errors |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | visitRegExpAST(regExpAST, { |
| 104 | onBackreferenceEnter(bref) { |
| 105 | const group = bref.resolved, |
| 106 | brefPath = getPathToRoot(bref), |
| 107 | groupPath = getPathToRoot(group); |
| 108 | let messageId = null; |
| 109 | |
| 110 | if (brefPath.includes(group)) { |
| 111 | |
| 112 | // group is bref's ancestor => bref is nested ('nested reference') => group hasn't matched yet when bref starts to match. |
| 113 | messageId = "nested"; |
| 114 | } else { |
| 115 | |
| 116 | // Start from the root to find the lowest common ancestor. |
| 117 | let i = brefPath.length - 1, |
| 118 | j = groupPath.length - 1; |
| 119 | |
| 120 | do { |
| 121 | i--; |
| 122 | j--; |
| 123 | } while (brefPath[i] === groupPath[j]); |
| 124 | |
| 125 | const indexOfLowestCommonAncestor = j + 1, |
| 126 | groupCut = groupPath.slice(0, indexOfLowestCommonAncestor), |
| 127 | commonPath = groupPath.slice(indexOfLowestCommonAncestor), |
| 128 | lowestCommonLookaround = commonPath.find(isLookaround), |
| 129 | isMatchingBackward = lowestCommonLookaround && lowestCommonLookaround.kind === "lookbehind"; |
| 130 | |
| 131 | if (!isMatchingBackward && bref.end <= group.start) { |
| 132 | |
| 133 | // bref is left, group is right ('forward reference') => group hasn't matched yet when bref starts to match. |
| 134 | messageId = "forward"; |
| 135 | } else if (isMatchingBackward && group.end <= bref.start) { |
| 136 | |
| 137 | // the opposite of the previous when the regex is matching backward in a lookbehind context. |
| 138 | messageId = "backward"; |
Simon Zünd | 52e2020 | 2021-06-16 08:34:28 +0200 | [diff] [blame] | 139 | } else if (groupCut[groupCut.length - 1].type === "Alternative") { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 140 | |
| 141 | // group's and bref's ancestor nodes below the lowest common ancestor are sibling alternatives => they're disjunctive. |
| 142 | messageId = "disjunctive"; |
| 143 | } else if (groupCut.some(isNegativeLookaround)) { |
| 144 | |
| 145 | // group is in a negative lookaround which isn't bref's ancestor => group has already failed when bref starts to match. |
| 146 | messageId = "intoNegativeLookaround"; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (messageId) { |
| 151 | context.report({ |
| 152 | node, |
| 153 | messageId, |
| 154 | data: { |
| 155 | bref: bref.raw, |
| 156 | group: group.raw |
| 157 | } |
| 158 | }); |
| 159 | } |
| 160 | } |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | return { |
| 165 | "Literal[regex]"(node) { |
| 166 | const { pattern, flags } = node.regex; |
| 167 | |
| 168 | checkRegex(node, pattern, flags); |
| 169 | }, |
| 170 | Program() { |
| 171 | const scope = context.getScope(), |
| 172 | tracker = new ReferenceTracker(scope), |
| 173 | traceMap = { |
| 174 | RegExp: { |
| 175 | [CALL]: true, |
| 176 | [CONSTRUCT]: true |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | for (const { node } of tracker.iterateGlobalReferences(traceMap)) { |
| 181 | const [patternNode, flagsNode] = node.arguments, |
| 182 | pattern = getStringIfConstant(patternNode, scope), |
| 183 | flags = getStringIfConstant(flagsNode, scope); |
| 184 | |
| 185 | if (typeof pattern === "string") { |
| 186 | checkRegex(node, pattern, flags || ""); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | }; |
| 191 | } |
| 192 | }; |