| 1 | import rewritePattern from "regexpu-core"; |
| 2 | import { |
| 3 | featuresKey, |
| 4 | FEATURES, |
| 5 | enableFeature, |
| 6 | runtimeKey, |
| 7 | hasFeature, |
| 8 | } from "./features"; |
| 9 | import { generateRegexpuOptions, canSkipRegexpu, transformFlags } from "./util"; |
| 10 | import type { NodePath } from "@babel/traverse"; |
| 11 | |
| 12 | import { types as t } from "@babel/core"; |
| 13 | import type { PluginObject } from "@babel/core"; |
| 14 | import annotateAsPure from "@babel/helper-annotate-as-pure"; |
| 15 | |
| 16 | declare const PACKAGE_JSON: { name: string; version: string }; |
| 17 | |
| 18 | // Note: Versions are represented as an integer. e.g. 7.1.5 is represented |
| 19 | // as 70000100005. This method is easier than using a semver-parsing |
| 20 | // package, but it breaks if we release x.y.z where x, y or z are |
| 21 | // greater than 99_999. |
| 22 | const version = PACKAGE_JSON.version |
| 23 | .split(".") |
| 24 | .reduce((v, x) => v * 1e5 + +x, 0); |
| 25 | const versionKey = "@babel/plugin-regexp-features/version"; |
| 26 | |
| 27 | export interface Options { |
| 28 | name: string; |
| 29 | feature: keyof typeof FEATURES; |
| 30 | options?: { |
| 31 | useUnicodeFlag?: boolean; |
| 32 | runtime?: boolean; |
| 33 | }; |
| 34 | manipulateOptions?: PluginObject["manipulateOptions"]; |
| 35 | } |
| 36 | |
| 37 | export function createRegExpFeaturePlugin({ |
| 38 | name, |
| 39 | feature, |
| 40 | options = {}, |
| 41 | manipulateOptions = () => {}, |
| 42 | }: Options): PluginObject { |
| 43 | return { |
| 44 | name, |
| 45 | |
| 46 | manipulateOptions, |
| 47 | |
| 48 | pre() { |
| 49 | const { file } = this; |
| 50 | const features = file.get(featuresKey) ?? 0; |
| 51 | let newFeatures = enableFeature(features, FEATURES[feature]); |
| 52 | |
| 53 | const { useUnicodeFlag, runtime } = options; |
| 54 | if (useUnicodeFlag === false) { |
| 55 | newFeatures = enableFeature(newFeatures, FEATURES.unicodeFlag); |
| 56 | } |
| 57 | if (newFeatures !== features) { |
| 58 | file.set(featuresKey, newFeatures); |
| 59 | } |
| 60 | |
| 61 | if (runtime !== undefined) { |
| 62 | if ( |
| 63 | file.has(runtimeKey) && |
| 64 | file.get(runtimeKey) !== runtime && |
| 65 | // TODO(Babel 8): Remove this check. It's necessary because in Babel 7 |
| 66 | // we allow multiple copies of transform-named-capturing-groups-regex |
| 67 | // with conflicting 'runtime' options. |
| 68 | hasFeature(newFeatures, FEATURES.duplicateNamedCaptureGroups) |
| 69 | ) { |
| 70 | throw new Error( |
| 71 | `The 'runtime' option must be the same for ` + |
| 72 | `'@babel/plugin-transform-named-capturing-groups-regex' and ` + |
| 73 | `'@babel/plugin-proposal-duplicate-named-capturing-groups-regex'.`, |
| 74 | ); |
| 75 | } |
| 76 | // TODO(Babel 8): Remove this check and always set it. |
| 77 | // It's necessary because in Babel 7 we allow multiple copies of |
| 78 | // transform-named-capturing-groups-regex with conflicting 'runtime' options. |
| 79 | if (feature === "namedCaptureGroups") { |
| 80 | if (!runtime || !file.has(runtimeKey)) file.set(runtimeKey, runtime); |
| 81 | } else { |
| 82 | file.set(runtimeKey, runtime); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (!file.has(versionKey) || file.get(versionKey) < version) { |
| 87 | file.set(versionKey, version); |
| 88 | } |
| 89 | }, |
| 90 | |
| 91 | visitor: { |
| 92 | RegExpLiteral(path) { |
| 93 | const { node } = path; |
| 94 | const { file } = this; |
| 95 | const features = file.get(featuresKey); |
| 96 | const runtime = file.get(runtimeKey) ?? true; |
| 97 | |
| 98 | const regexpuOptions = generateRegexpuOptions(node.pattern, features); |
| 99 | if (canSkipRegexpu(node, regexpuOptions)) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | const namedCaptureGroups: Record<string, number | number[]> = { |
| 104 | __proto__: null, |
| 105 | }; |
| 106 | if (regexpuOptions.namedGroups === "transform") { |
| 107 | regexpuOptions.onNamedGroup = (name, index) => { |
| 108 | const prev = namedCaptureGroups[name]; |
| 109 | if (typeof prev === "number") { |
| 110 | namedCaptureGroups[name] = [prev, index]; |
| 111 | } else if (Array.isArray(prev)) { |
| 112 | prev.push(index); |
| 113 | } else { |
| 114 | namedCaptureGroups[name] = index; |
| 115 | } |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | node.pattern = rewritePattern(node.pattern, node.flags, regexpuOptions); |
| 120 | |
| 121 | if ( |
| 122 | regexpuOptions.namedGroups === "transform" && |
| 123 | Object.keys(namedCaptureGroups).length > 0 && |
| 124 | runtime && |
| 125 | !isRegExpTest(path) |
| 126 | ) { |
| 127 | const call = t.callExpression(this.addHelper("wrapRegExp"), [ |
| 128 | node, |
| 129 | t.valueToNode(namedCaptureGroups), |
| 130 | ]); |
| 131 | annotateAsPure(call); |
| 132 | |
| 133 | path.replaceWith(call); |
| 134 | } |
| 135 | |
| 136 | node.flags = transformFlags(regexpuOptions, node.flags); |
| 137 | }, |
| 138 | }, |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | function isRegExpTest(path: NodePath<t.RegExpLiteral>) { |
| 143 | return ( |
| 144 | path.parentPath.isMemberExpression({ |
| 145 | object: path.node, |
| 146 | computed: false, |
| 147 | }) && path.parentPath.get("property").isIdentifier({ name: "test" }) |
| 148 | ); |
| 149 | } |