|
| 1 | +import type { RegExpVisitor } from "@eslint-community/regexpp/visitor" |
| 2 | +import type { |
| 3 | + Alternative, |
| 4 | + Character, |
| 5 | + CharacterClass, |
| 6 | + CharacterSet, |
| 7 | + ExpressionCharacterClass, |
| 8 | + LookaroundAssertion, |
| 9 | + Node, |
| 10 | +} from "@eslint-community/regexpp/ast" |
| 11 | +import type { RegExpContext } from "../utils" |
| 12 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 13 | +import { hasStrings } from "regexp-ast-analysis" |
| 14 | + |
| 15 | +type CharElement = |
| 16 | + | Character |
| 17 | + | CharacterSet |
| 18 | + | CharacterClass |
| 19 | + | ExpressionCharacterClass |
| 20 | + |
| 21 | +function isCharElement(node: Node): node is CharElement { |
| 22 | + return ( |
| 23 | + node.type === "Character" || |
| 24 | + node.type === "CharacterSet" || |
| 25 | + node.type === "CharacterClass" || |
| 26 | + node.type === "ExpressionCharacterClass" |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +type CharLookaround = LookaroundAssertion & { |
| 31 | + alternatives: [Alternative & { elements: [CharElement] }] |
| 32 | +} |
| 33 | + |
| 34 | +function isCharLookaround(node: Node): node is CharLookaround { |
| 35 | + return ( |
| 36 | + node.type === "Assertion" && |
| 37 | + (node.kind === "lookahead" || node.kind === "lookbehind") && |
| 38 | + node.alternatives.length === 1 && |
| 39 | + node.alternatives[0].elements.length === 1 && |
| 40 | + isCharElement(node.alternatives[0].elements[0]) |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +function escapeRaw(raw: string): string { |
| 45 | + if (/^[&\-^]$/u.test(raw)) { |
| 46 | + return `\\${raw}` |
| 47 | + } |
| 48 | + return raw |
| 49 | +} |
| 50 | + |
| 51 | +export default createRule("prefer-set-operation", { |
| 52 | + meta: { |
| 53 | + docs: { |
| 54 | + description: |
| 55 | + "prefer character class set operations instead of lookarounds", |
| 56 | + category: "Best Practices", |
| 57 | + recommended: true, |
| 58 | + }, |
| 59 | + fixable: "code", |
| 60 | + schema: [], |
| 61 | + messages: { |
| 62 | + unexpected: |
| 63 | + "This lookaround can be combined with '{{char}}' using a set operation.", |
| 64 | + }, |
| 65 | + type: "suggestion", |
| 66 | + }, |
| 67 | + create(context) { |
| 68 | + function createVisitor( |
| 69 | + regexpContext: RegExpContext, |
| 70 | + ): RegExpVisitor.Handlers { |
| 71 | + const { node, flags, getRegexpLocation, fixReplaceNode } = |
| 72 | + regexpContext |
| 73 | + |
| 74 | + if (!flags.unicodeSets) { |
| 75 | + // set operations are exclusive to the v flag. |
| 76 | + return {} |
| 77 | + } |
| 78 | + |
| 79 | + function tryApply( |
| 80 | + element: CharElement, |
| 81 | + assertion: CharLookaround, |
| 82 | + parent: Alternative, |
| 83 | + ): void { |
| 84 | + const assertElement = assertion.alternatives[0].elements[0] |
| 85 | + if (hasStrings(assertElement, flags)) { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + context.report({ |
| 90 | + node, |
| 91 | + loc: getRegexpLocation(assertion), |
| 92 | + messageId: "unexpected", |
| 93 | + data: { |
| 94 | + char: element.raw, |
| 95 | + }, |
| 96 | + fix: fixReplaceNode(parent, () => { |
| 97 | + const op = assertion.negate ? "--" : "&&" |
| 98 | + const left = escapeRaw(element.raw) |
| 99 | + const right = escapeRaw(assertElement.raw) |
| 100 | + const replacement = `[${left}${op}${right}]` |
| 101 | + |
| 102 | + return parent.elements |
| 103 | + .map((e) => { |
| 104 | + if (e === assertion) { |
| 105 | + return "" |
| 106 | + } else if (e === element) { |
| 107 | + return replacement |
| 108 | + } |
| 109 | + return e.raw |
| 110 | + }) |
| 111 | + .join("") |
| 112 | + }), |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + return { |
| 117 | + onAlternativeEnter(alternative) { |
| 118 | + const { elements } = alternative |
| 119 | + for (let i = 1; i < elements.length; i++) { |
| 120 | + const a = elements[i - 1] |
| 121 | + const b = elements[i] |
| 122 | + |
| 123 | + if ( |
| 124 | + isCharElement(a) && |
| 125 | + isCharLookaround(b) && |
| 126 | + b.kind === "lookbehind" |
| 127 | + ) { |
| 128 | + tryApply(a, b, alternative) |
| 129 | + } |
| 130 | + if ( |
| 131 | + isCharLookaround(a) && |
| 132 | + a.kind === "lookahead" && |
| 133 | + isCharElement(b) |
| 134 | + ) { |
| 135 | + tryApply(b, a, alternative) |
| 136 | + } |
| 137 | + } |
| 138 | + }, |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return defineRegexpVisitor(context, { |
| 143 | + createVisitor, |
| 144 | + }) |
| 145 | + }, |
| 146 | +}) |
0 commit comments