|
| 1 | +import type { Expression } from "estree" |
| 2 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 3 | +import type { Node as RegExpNode, LookaroundAssertion } from "regexpp/ast" |
| 4 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 5 | + |
| 6 | +/* istanbul ignore file */ |
| 7 | +/** |
| 8 | + * Finds the path from the given `regexpp` AST node to the root node. |
| 9 | + * @param {regexpp.Node} node Node. |
| 10 | + * @returns {regexpp.Node[]} Array that starts with the given node and ends with the root node. |
| 11 | + */ |
| 12 | +function getPathToRoot(node: RegExpNode) { |
| 13 | + const path = [] |
| 14 | + let current = node |
| 15 | + |
| 16 | + while (current) { |
| 17 | + path.push(current) |
| 18 | + if (!current.parent) { |
| 19 | + break |
| 20 | + } |
| 21 | + current = current.parent |
| 22 | + } |
| 23 | + |
| 24 | + return path |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Determines whether the given `regexpp` AST node is a lookaround node. |
| 29 | + * @param {regexpp.Node} node Node. |
| 30 | + * @returns {boolean} `true` if it is a lookaround node. |
| 31 | + */ |
| 32 | +function isLookaround(node: RegExpNode): node is LookaroundAssertion { |
| 33 | + return ( |
| 34 | + node.type === "Assertion" && |
| 35 | + (node.kind === "lookahead" || node.kind === "lookbehind") |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Determines whether the given `regexpp` AST node is a negative lookaround node. |
| 41 | + * @param {regexpp.Node} node Node. |
| 42 | + * @returns {boolean} `true` if it is a negative lookaround node. |
| 43 | + */ |
| 44 | +function isNegativeLookaround(node: RegExpNode) { |
| 45 | + return isLookaround(node) && node.negate |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Get last element |
| 50 | + */ |
| 51 | +function last<T>(arr: T[]): T { |
| 52 | + return arr[arr.length - 1] |
| 53 | +} |
| 54 | + |
| 55 | +export default createRule("no-useless-backreference", { |
| 56 | + meta: { |
| 57 | + docs: { |
| 58 | + description: |
| 59 | + "disallow useless backreferences in regular expressions", |
| 60 | + recommended: false, |
| 61 | + }, |
| 62 | + schema: [], |
| 63 | + messages: { |
| 64 | + nested: |
| 65 | + "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' from within that group.", |
| 66 | + forward: |
| 67 | + "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears later in the pattern.", |
| 68 | + backward: |
| 69 | + "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which appears before in the same lookbehind.", |
| 70 | + disjunctive: |
| 71 | + "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in another alternative.", |
| 72 | + intoNegativeLookaround: |
| 73 | + "Backreference '{{ bref }}' will be ignored. It references group '{{ group }}' which is in a negative lookaround.", |
| 74 | + }, |
| 75 | + type: "suggestion", // "problem", |
| 76 | + }, |
| 77 | + create(context) { |
| 78 | + /** |
| 79 | + * Create visitor |
| 80 | + * @param node |
| 81 | + */ |
| 82 | + function createVisitor(node: Expression): RegExpVisitor.Handlers { |
| 83 | + return { |
| 84 | + onBackreferenceEnter(bref) { |
| 85 | + const group = bref.resolved, |
| 86 | + brefPath = getPathToRoot(bref), |
| 87 | + groupPath = getPathToRoot(group) |
| 88 | + let messageId = null |
| 89 | + |
| 90 | + if (brefPath.includes(group)) { |
| 91 | + // group is bref's ancestor => bref is nested ('nested reference') => group hasn't matched yet when bref starts to match. |
| 92 | + messageId = "nested" |
| 93 | + } else { |
| 94 | + // Start from the root to find the lowest common ancestor. |
| 95 | + let i = brefPath.length - 1, |
| 96 | + j = groupPath.length - 1 |
| 97 | + |
| 98 | + do { |
| 99 | + i-- |
| 100 | + j-- |
| 101 | + } while (brefPath[i] === groupPath[j]) |
| 102 | + |
| 103 | + const indexOfLowestCommonAncestor = j + 1, |
| 104 | + groupCut = groupPath.slice( |
| 105 | + 0, |
| 106 | + indexOfLowestCommonAncestor, |
| 107 | + ), |
| 108 | + commonPath = groupPath.slice( |
| 109 | + indexOfLowestCommonAncestor, |
| 110 | + ), |
| 111 | + lowestCommonLookaround = commonPath.find( |
| 112 | + isLookaround, |
| 113 | + ), |
| 114 | + isMatchingBackward = |
| 115 | + lowestCommonLookaround && |
| 116 | + lowestCommonLookaround.kind === "lookbehind" |
| 117 | + |
| 118 | + if (!isMatchingBackward && bref.end <= group.start) { |
| 119 | + // bref is left, group is right ('forward reference') => group hasn't matched yet when bref starts to match. |
| 120 | + messageId = "forward" |
| 121 | + } else if ( |
| 122 | + isMatchingBackward && |
| 123 | + group.end <= bref.start |
| 124 | + ) { |
| 125 | + // the opposite of the previous when the regex is matching backward in a lookbehind context. |
| 126 | + messageId = "backward" |
| 127 | + } else if (last(groupCut).type === "Alternative") { |
| 128 | + // group's and bref's ancestor nodes below the lowest common ancestor are sibling alternatives => they're disjunctive. |
| 129 | + messageId = "disjunctive" |
| 130 | + } else if (groupCut.some(isNegativeLookaround)) { |
| 131 | + // group is in a negative lookaround which isn't bref's ancestor => group has already failed when bref starts to match. |
| 132 | + messageId = "intoNegativeLookaround" |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (messageId) { |
| 137 | + context.report({ |
| 138 | + node, |
| 139 | + messageId, |
| 140 | + data: { |
| 141 | + bref: bref.raw, |
| 142 | + group: group.raw, |
| 143 | + }, |
| 144 | + }) |
| 145 | + } |
| 146 | + }, |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return defineRegexpVisitor(context, { |
| 151 | + createVisitor, |
| 152 | + }) |
| 153 | + }, |
| 154 | +}) |
0 commit comments