|
| 1 | +import type { RegExpVisitor } from "@eslint-community/regexpp/visitor" |
| 2 | +import type { |
| 3 | + CharacterClassElement, |
| 4 | + ClassSetOperand, |
| 5 | + ExpressionCharacterClass, |
| 6 | + Node, |
| 7 | + StringAlternative, |
| 8 | +} from "@eslint-community/regexpp/ast" |
| 9 | +import type { RegExpContext } from "../utils" |
| 10 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 11 | +import { toUnicodeSet } from "regexp-ast-analysis" |
| 12 | + |
| 13 | +type FlatElement = CharacterClassElement | StringAlternative |
| 14 | + |
| 15 | +function getFlatElements( |
| 16 | + node: ClassSetOperand | ExpressionCharacterClass["expression"], |
| 17 | +): readonly FlatElement[] { |
| 18 | + if (node.type === "ClassStringDisjunction") { |
| 19 | + return node.alternatives |
| 20 | + } |
| 21 | + if (node.type === "CharacterClass") { |
| 22 | + const nested: FlatElement[] = [] |
| 23 | + // eslint-disable-next-line func-style -- x |
| 24 | + const addElement = (element: CharacterClassElement) => { |
| 25 | + if (element.type === "ClassStringDisjunction") { |
| 26 | + nested.push(...element.alternatives) |
| 27 | + } else if (element.type === "CharacterClass") { |
| 28 | + if (!element.negate) { |
| 29 | + nested.push(...element.elements) |
| 30 | + } |
| 31 | + nested.push(element) |
| 32 | + } else { |
| 33 | + nested.push(element) |
| 34 | + } |
| 35 | + } |
| 36 | + node.elements.forEach(addElement) |
| 37 | + return nested |
| 38 | + } |
| 39 | + |
| 40 | + return [] |
| 41 | +} |
| 42 | + |
| 43 | +function removeDescendant(root: Node, e: FlatElement): string { |
| 44 | + let { start, end } = e |
| 45 | + |
| 46 | + if (e.type === "StringAlternative") { |
| 47 | + if (e.parent.alternatives.length === 1) { |
| 48 | + // we have to remove the whole string disjunction |
| 49 | + // eslint-disable-next-line no-param-reassign -- x |
| 50 | + e = e.parent |
| 51 | + start = e.start |
| 52 | + end = e.end |
| 53 | + } else { |
| 54 | + // remove one adjacent | symbol |
| 55 | + if (e.parent.alternatives.at(-1) === e) { |
| 56 | + start-- |
| 57 | + } else { |
| 58 | + end++ |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + const before = root.raw.slice(0, start - root.start) |
| 64 | + const after = root.raw.slice(end - root.start) |
| 65 | + return before + after |
| 66 | +} |
| 67 | + |
| 68 | +export default createRule("no-useless-set-operand", { |
| 69 | + meta: { |
| 70 | + docs: { |
| 71 | + description: |
| 72 | + "disallow unnecessary elements in expression character classes", |
| 73 | + category: "Best Practices", |
| 74 | + recommended: true, |
| 75 | + }, |
| 76 | + schema: [], |
| 77 | + messages: { |
| 78 | + intersectionDisjoint: |
| 79 | + "'{{left}}' and '{{right}}' are disjoint, so the result of the intersection is always going to be the empty set.", |
| 80 | + intersectionSubset: |
| 81 | + "'{{sub}}' is a subset of '{{super}}', so the result of the intersection is always going to be '{{sub}}'.", |
| 82 | + intersectionRemove: |
| 83 | + "'{{expr}}' can be removed without changing the result of the intersection.", |
| 84 | + subtractionDisjoint: |
| 85 | + "'{{left}}' and '{{right}}' are disjoint, so the subtraction doesn't do anything.", |
| 86 | + subtractionSubset: |
| 87 | + "'{{left}}' is a subset of '{{right}}', so the result of the subtraction is always going to be the empty set.", |
| 88 | + subtractionRemove: |
| 89 | + "'{{expr}}' can be removed without changing the result of the subtraction.", |
| 90 | + }, |
| 91 | + fixable: "code", |
| 92 | + type: "suggestion", |
| 93 | + }, |
| 94 | + create(context) { |
| 95 | + function createVisitor( |
| 96 | + regexpContext: RegExpContext, |
| 97 | + ): RegExpVisitor.Handlers { |
| 98 | + const { node, flags, getRegexpLocation, fixReplaceNode } = |
| 99 | + regexpContext |
| 100 | + |
| 101 | + if (!flags.unicodeSets) { |
| 102 | + // set operations are only available with the `v` flag |
| 103 | + return {} |
| 104 | + } |
| 105 | + |
| 106 | + function fixRemoveExpression( |
| 107 | + expr: ExpressionCharacterClass["expression"], |
| 108 | + ) { |
| 109 | + if (expr.parent.type === "ExpressionCharacterClass") { |
| 110 | + const cc = expr.parent |
| 111 | + return fixReplaceNode(cc, cc.negate ? "[^]" : "[]") |
| 112 | + } |
| 113 | + return fixReplaceNode(expr, "[]") |
| 114 | + } |
| 115 | + |
| 116 | + return { |
| 117 | + onClassIntersectionEnter(iNode) { |
| 118 | + const leftSet = toUnicodeSet(iNode.left, flags) |
| 119 | + const rightSet = toUnicodeSet(iNode.right, flags) |
| 120 | + |
| 121 | + if (leftSet.isDisjointWith(rightSet)) { |
| 122 | + context.report({ |
| 123 | + node, |
| 124 | + loc: getRegexpLocation(iNode), |
| 125 | + messageId: "intersectionDisjoint", |
| 126 | + data: { |
| 127 | + left: iNode.left.raw, |
| 128 | + right: iNode.right.raw, |
| 129 | + }, |
| 130 | + fix: fixRemoveExpression(iNode), |
| 131 | + }) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + if (leftSet.isSubsetOf(rightSet)) { |
| 136 | + context.report({ |
| 137 | + node, |
| 138 | + loc: getRegexpLocation(iNode), |
| 139 | + messageId: "intersectionSubset", |
| 140 | + data: { |
| 141 | + sub: iNode.left.raw, |
| 142 | + super: iNode.right.raw, |
| 143 | + }, |
| 144 | + fix: fixReplaceNode(iNode, iNode.left.raw), |
| 145 | + }) |
| 146 | + return |
| 147 | + } |
| 148 | + if (rightSet.isSubsetOf(leftSet)) { |
| 149 | + context.report({ |
| 150 | + node, |
| 151 | + loc: getRegexpLocation(iNode), |
| 152 | + messageId: "intersectionSubset", |
| 153 | + data: { |
| 154 | + sub: iNode.right.raw, |
| 155 | + super: iNode.left.raw, |
| 156 | + }, |
| 157 | + fix: fixReplaceNode(iNode, iNode.right.raw), |
| 158 | + }) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + const toRemoveRight = getFlatElements(iNode.right).filter( |
| 163 | + (e) => leftSet.isDisjointWith(toUnicodeSet(e, flags)), |
| 164 | + ) |
| 165 | + const toRemoveLeft = getFlatElements(iNode.left).filter( |
| 166 | + (e) => rightSet.isDisjointWith(toUnicodeSet(e, flags)), |
| 167 | + ) |
| 168 | + for (const e of [...toRemoveRight, ...toRemoveLeft]) { |
| 169 | + context.report({ |
| 170 | + node, |
| 171 | + loc: getRegexpLocation(e), |
| 172 | + messageId: "subtractionRemove", |
| 173 | + data: { |
| 174 | + expr: e.raw, |
| 175 | + }, |
| 176 | + fix: fixReplaceNode( |
| 177 | + iNode, |
| 178 | + removeDescendant(iNode, e), |
| 179 | + ), |
| 180 | + }) |
| 181 | + } |
| 182 | + }, |
| 183 | + onClassSubtractionEnter(sNode) { |
| 184 | + const leftSet = toUnicodeSet(sNode.left, flags) |
| 185 | + const rightSet = toUnicodeSet(sNode.right, flags) |
| 186 | + |
| 187 | + if (leftSet.isDisjointWith(rightSet)) { |
| 188 | + context.report({ |
| 189 | + node, |
| 190 | + loc: getRegexpLocation(sNode), |
| 191 | + messageId: "subtractionDisjoint", |
| 192 | + data: { |
| 193 | + left: sNode.left.raw, |
| 194 | + right: sNode.right.raw, |
| 195 | + }, |
| 196 | + fix: fixReplaceNode(sNode, sNode.left.raw), |
| 197 | + }) |
| 198 | + return |
| 199 | + } |
| 200 | + |
| 201 | + if (leftSet.isSubsetOf(rightSet)) { |
| 202 | + context.report({ |
| 203 | + node, |
| 204 | + loc: getRegexpLocation(sNode), |
| 205 | + messageId: "subtractionSubset", |
| 206 | + data: { |
| 207 | + left: sNode.left.raw, |
| 208 | + right: sNode.right.raw, |
| 209 | + }, |
| 210 | + fix: fixRemoveExpression(sNode), |
| 211 | + }) |
| 212 | + return |
| 213 | + } |
| 214 | + |
| 215 | + const toRemove = getFlatElements(sNode.right).filter((e) => |
| 216 | + leftSet.isDisjointWith(toUnicodeSet(e, flags)), |
| 217 | + ) |
| 218 | + for (const e of toRemove) { |
| 219 | + context.report({ |
| 220 | + node, |
| 221 | + loc: getRegexpLocation(e), |
| 222 | + messageId: "subtractionRemove", |
| 223 | + data: { |
| 224 | + expr: e.raw, |
| 225 | + }, |
| 226 | + fix: fixReplaceNode( |
| 227 | + sNode, |
| 228 | + removeDescendant(sNode, e), |
| 229 | + ), |
| 230 | + }) |
| 231 | + } |
| 232 | + }, |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return defineRegexpVisitor(context, { |
| 237 | + createVisitor, |
| 238 | + }) |
| 239 | + }, |
| 240 | +}) |
0 commit comments