|
| 1 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 2 | +import type { |
| 3 | + CharacterClass, |
| 4 | + CharacterSet, |
| 5 | + LookaroundAssertion, |
| 6 | +} from "regexpp/ast" |
| 7 | +import type { RegExpContext } from "../utils" |
| 8 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 9 | +import { |
| 10 | + Chars, |
| 11 | + getFirstCharAfter, |
| 12 | + getMatchingDirectionFromAssertionKind, |
| 13 | + invertMatchingDirection, |
| 14 | +} from "regexp-ast-analysis" |
| 15 | + |
| 16 | +/** |
| 17 | + * If the lookaround only consists of a single character, character set, or |
| 18 | + * character class, then this single character will be returned. |
| 19 | + */ |
| 20 | +function getCharacters( |
| 21 | + lookaround: LookaroundAssertion, |
| 22 | +): CharacterSet | CharacterClass | null { |
| 23 | + if (lookaround.alternatives.length === 1) { |
| 24 | + const alt = lookaround.alternatives[0] |
| 25 | + if (alt.elements.length === 1) { |
| 26 | + const first = alt.elements[0] |
| 27 | + if ( |
| 28 | + first.type === "CharacterSet" || |
| 29 | + first.type === "CharacterClass" |
| 30 | + ) { |
| 31 | + return first |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + return null |
| 36 | +} |
| 37 | + |
| 38 | +export default createRule("prefer-predefined-assertion", { |
| 39 | + meta: { |
| 40 | + docs: { |
| 41 | + description: |
| 42 | + "prefer predefined assertion over equivalent lookarounds", |
| 43 | + // TODO Switch to recommended in the major version. |
| 44 | + // recommended: true, |
| 45 | + recommended: false, |
| 46 | + }, |
| 47 | + fixable: "code", |
| 48 | + schema: [], |
| 49 | + messages: { |
| 50 | + replace: |
| 51 | + "This lookaround assertion can be replaced with {{kind}} ('{{expr}}').", |
| 52 | + }, |
| 53 | + type: "suggestion", // "problem", |
| 54 | + }, |
| 55 | + create(context) { |
| 56 | + /** |
| 57 | + * Create visitor |
| 58 | + */ |
| 59 | + function createVisitor( |
| 60 | + regexpContext: RegExpContext, |
| 61 | + ): RegExpVisitor.Handlers { |
| 62 | + const { |
| 63 | + node, |
| 64 | + flags, |
| 65 | + getRegexpLocation, |
| 66 | + toCharSet, |
| 67 | + fixReplaceNode, |
| 68 | + } = regexpContext |
| 69 | + |
| 70 | + const word = Chars.word(flags) |
| 71 | + const nonWord = Chars.word(flags).negate() |
| 72 | + |
| 73 | + // /\b/ == /(?<!\w)(?=\w)|(?<=\w)(?!\w)/ |
| 74 | + // /\B/ == /(?<=\w)(?=\w)|(?<!\w)(?!\w)/ |
| 75 | + |
| 76 | + /** |
| 77 | + * Tries to replace the given assertion with a word boundary |
| 78 | + * assertion |
| 79 | + */ |
| 80 | + function replaceWordAssertion( |
| 81 | + aNode: LookaroundAssertion, |
| 82 | + wordNegated: boolean, |
| 83 | + ): void { |
| 84 | + const direction = getMatchingDirectionFromAssertionKind( |
| 85 | + aNode.kind, |
| 86 | + ) |
| 87 | + |
| 88 | + /** |
| 89 | + * Whether the lookaround is equivalent to (?!\w) / (?<!\w) or (?=\w) / (?<=\w) |
| 90 | + */ |
| 91 | + let lookaroundNegated = aNode.negate |
| 92 | + if (wordNegated) { |
| 93 | + // if the lookaround only contains a \W, then we have to negate the lookaround, so it only |
| 94 | + // contains a \w. This is only possible iff we know that the pattern requires at least one |
| 95 | + // character after the lookaround (in the direction of the lookaround). |
| 96 | + // |
| 97 | + // Examples: |
| 98 | + // (?=\W) == (?!\w|$) ; Here we need to eliminate the $ which can be done by proving that the |
| 99 | + // pattern matches another character after the lookahead. Example: |
| 100 | + // (?=\W).+ == (?!\w).+ ; Since we know that the lookahead is always followed by a dot, we |
| 101 | + // eliminate the $ alternative because it will always reject. |
| 102 | + // (?!\W).+ == (?=\w|$).+ == (?=\w).+ |
| 103 | + |
| 104 | + const after = getFirstCharAfter(aNode, direction, flags) |
| 105 | + |
| 106 | + const hasNextCharacter = !after.edge |
| 107 | + if (hasNextCharacter) { |
| 108 | + // we can successfully negate the lookaround |
| 109 | + lookaroundNegated = !lookaroundNegated |
| 110 | + } else { |
| 111 | + // we couldn't negate the \W, so it's not possible to convert the lookaround into a |
| 112 | + // predefined assertion |
| 113 | + return |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + const before = getFirstCharAfter( |
| 118 | + aNode, |
| 119 | + invertMatchingDirection(direction), |
| 120 | + flags, |
| 121 | + ) |
| 122 | + if (before.edge) { |
| 123 | + // to do the branch elimination necessary, we need to know the previous/next character |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + let otherNegated |
| 128 | + if (before.char.isSubsetOf(word)) { |
| 129 | + // we can think of the previous/next character as \w |
| 130 | + otherNegated = false |
| 131 | + } else if (before.char.isSubsetOf(nonWord)) { |
| 132 | + // we can think of the previous/next character as \W |
| 133 | + otherNegated = true |
| 134 | + } else { |
| 135 | + // the previous/next character is a subset of neither \w nor \W, so we can't do anything here |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + let kind = undefined |
| 140 | + let replacement = undefined |
| 141 | + if (lookaroundNegated === otherNegated) { |
| 142 | + // \B |
| 143 | + kind = "a negated word boundary assertion" |
| 144 | + replacement = "\\B" |
| 145 | + } else { |
| 146 | + // \b |
| 147 | + kind = "a word boundary assertion" |
| 148 | + replacement = "\\b" |
| 149 | + } |
| 150 | + |
| 151 | + if (kind && replacement) { |
| 152 | + context.report({ |
| 153 | + node, |
| 154 | + loc: getRegexpLocation(aNode), |
| 155 | + messageId: "replace", |
| 156 | + data: { kind, expr: replacement }, |
| 157 | + fix: fixReplaceNode(aNode, replacement), |
| 158 | + }) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Tries to replace the given assertion with a edge assertion |
| 164 | + */ |
| 165 | + function replaceEdgeAssertion( |
| 166 | + aNode: LookaroundAssertion, |
| 167 | + lineAssertion: boolean, |
| 168 | + ): void { |
| 169 | + if (!aNode.negate) { |
| 170 | + return |
| 171 | + } |
| 172 | + if (flags.multiline === lineAssertion) { |
| 173 | + const replacement = aNode.kind === "lookahead" ? "$" : "^" |
| 174 | + |
| 175 | + context.report({ |
| 176 | + node, |
| 177 | + loc: getRegexpLocation(aNode), |
| 178 | + messageId: "replace", |
| 179 | + data: { kind: "an edge assertion", expr: replacement }, |
| 180 | + fix: fixReplaceNode(aNode, replacement), |
| 181 | + }) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return { |
| 186 | + onAssertionEnter(aNode) { |
| 187 | + if ( |
| 188 | + aNode.kind !== "lookahead" && |
| 189 | + aNode.kind !== "lookbehind" |
| 190 | + ) { |
| 191 | + // this rule doesn't affect predefined assertions |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + const chars = getCharacters(aNode) |
| 196 | + if (chars === null) { |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + if (chars.type === "CharacterSet") { |
| 201 | + if (chars.kind === "word") { |
| 202 | + replaceWordAssertion(aNode, chars.negate) |
| 203 | + return |
| 204 | + } |
| 205 | + if (chars.kind === "any") { |
| 206 | + replaceEdgeAssertion(aNode, !flags.dotAll) |
| 207 | + return |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + const charSet = toCharSet(chars) |
| 212 | + if (charSet.isAll) { |
| 213 | + replaceEdgeAssertion(aNode, false) |
| 214 | + } else if (charSet.equals(word)) { |
| 215 | + replaceWordAssertion(aNode, false) |
| 216 | + } else if (charSet.equals(nonWord)) { |
| 217 | + replaceWordAssertion(aNode, true) |
| 218 | + } |
| 219 | + }, |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return defineRegexpVisitor(context, { |
| 224 | + createVisitor, |
| 225 | + }) |
| 226 | + }, |
| 227 | +}) |
0 commit comments