|
| 1 | +import type { LookaroundAssertion } from "regexpp/ast" |
| 2 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 3 | +import type { RegExpContext } from "../utils" |
| 4 | +import { createRule, defineRegexpVisitor } from "../utils" |
| 5 | + |
| 6 | +export default createRule("no-extra-lookaround-assertions", { |
| 7 | + meta: { |
| 8 | + docs: { |
| 9 | + description: "disallow unnecessary nested lookaround assertions", |
| 10 | + category: "Best Practices", |
| 11 | + // TODO Switch to recommended in the major version. |
| 12 | + // recommended: true, |
| 13 | + recommended: false, |
| 14 | + }, |
| 15 | + fixable: "code", |
| 16 | + schema: [], |
| 17 | + messages: { |
| 18 | + canBeInlined: |
| 19 | + "This {{kind}} assertion is useless and can be inlined.", |
| 20 | + canBeConvertedIntoGroup: |
| 21 | + "This {{kind}} assertion is useless and can be converted into a group.", |
| 22 | + }, |
| 23 | + type: "suggestion", |
| 24 | + }, |
| 25 | + create(context) { |
| 26 | + /** |
| 27 | + * Create visitor |
| 28 | + */ |
| 29 | + function createVisitor( |
| 30 | + regexpContext: RegExpContext, |
| 31 | + ): RegExpVisitor.Handlers { |
| 32 | + return { |
| 33 | + onAssertionEnter(aNode) { |
| 34 | + if ( |
| 35 | + aNode.kind === "lookahead" || |
| 36 | + aNode.kind === "lookbehind" |
| 37 | + ) { |
| 38 | + verify(regexpContext, aNode) |
| 39 | + } |
| 40 | + }, |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** Verify for lookaround assertion */ |
| 45 | + function verify( |
| 46 | + regexpContext: RegExpContext, |
| 47 | + assertion: LookaroundAssertion, |
| 48 | + ) { |
| 49 | + for (const alternative of assertion.alternatives) { |
| 50 | + const nested = at( |
| 51 | + alternative.elements, |
| 52 | + assertion.kind === "lookahead" |
| 53 | + ? // The last positive lookahead assertion within |
| 54 | + // a lookahead assertion is the same without the assertion. |
| 55 | + -1 |
| 56 | + : // The first positive lookbehind assertion within |
| 57 | + // a lookbehind assertion is the same without the assertion. |
| 58 | + 0, |
| 59 | + ) |
| 60 | + if ( |
| 61 | + nested?.type === "Assertion" && |
| 62 | + nested.kind === assertion.kind && |
| 63 | + !nested.negate |
| 64 | + ) { |
| 65 | + reportLookaroundAssertion(regexpContext, nested) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** Report */ |
| 71 | + function reportLookaroundAssertion( |
| 72 | + { node, getRegexpLocation, fixReplaceNode }: RegExpContext, |
| 73 | + assertion: LookaroundAssertion, |
| 74 | + ) { |
| 75 | + let messageId, replaceText |
| 76 | + if (assertion.alternatives.length === 1) { |
| 77 | + messageId = "canBeInlined" |
| 78 | + // unwrap `(?=` and `)`, `(?<=` and `)` |
| 79 | + replaceText = assertion.alternatives[0].raw |
| 80 | + } else { |
| 81 | + messageId = "canBeConvertedIntoGroup" |
| 82 | + // replace `?=` with `?:`, or `?<=` with `?:` |
| 83 | + replaceText = `(?:${assertion.alternatives |
| 84 | + .map((alt) => alt.raw) |
| 85 | + .join("|")})` |
| 86 | + } |
| 87 | + |
| 88 | + context.report({ |
| 89 | + node, |
| 90 | + loc: getRegexpLocation(assertion), |
| 91 | + messageId, |
| 92 | + data: { |
| 93 | + kind: assertion.kind, |
| 94 | + }, |
| 95 | + fix: fixReplaceNode(assertion, replaceText), |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + return defineRegexpVisitor(context, { |
| 100 | + createVisitor, |
| 101 | + }) |
| 102 | + }, |
| 103 | +}) |
| 104 | + |
| 105 | +// TODO After dropping support for Node < v16.6.0 we can use native `.at()`. |
| 106 | +/** |
| 107 | + * `.at()` polyfill |
| 108 | + * see https://github.com/tc39/proposal-relative-indexing-method#polyfill |
| 109 | + */ |
| 110 | +function at<T>(array: T[], n: number) { |
| 111 | + // ToInteger() abstract op |
| 112 | + let num = Math.trunc(n) || 0 |
| 113 | + // Allow negative indexing from the end |
| 114 | + if (num < 0) num += array.length |
| 115 | + // OOB access is guaranteed to return undefined |
| 116 | + if (num < 0 || num >= array.length) return undefined |
| 117 | + // Otherwise, this is just normal property access |
| 118 | + return array[num] |
| 119 | +} |
0 commit comments