index.js.map
8.71 KB
{"version":3,"names":["version","split","reduce","v","x","versionKey","createRegExpFeaturePlugin","name","feature","options","manipulateOptions","pre","file","features","get","featuresKey","newFeatures","enableFeature","FEATURES","useUnicodeFlag","runtime","unicodeFlag","set","undefined","has","runtimeKey","hasFeature","duplicateNamedCaptureGroups","Error","visitor","RegExpLiteral","path","node","regexpuOptions","generateRegexpuOptions","pattern","canSkipRegexpu","namedCaptureGroups","__proto__","namedGroups","onNamedGroup","index","prev","Array","isArray","push","rewritePattern","flags","Object","keys","length","isRegExpTest","call","t","callExpression","addHelper","valueToNode","annotateAsPure","replaceWith","transformFlags","parentPath","isMemberExpression","object","computed","isIdentifier"],"sources":["../src/index.ts"],"sourcesContent":["import rewritePattern from \"regexpu-core\";\nimport {\n featuresKey,\n FEATURES,\n enableFeature,\n runtimeKey,\n hasFeature,\n} from \"./features\";\nimport { generateRegexpuOptions, canSkipRegexpu, transformFlags } from \"./util\";\nimport type { NodePath } from \"@babel/traverse\";\n\nimport { types as t } from \"@babel/core\";\nimport type { PluginObject } from \"@babel/core\";\nimport annotateAsPure from \"@babel/helper-annotate-as-pure\";\n\ndeclare const PACKAGE_JSON: { name: string; version: string };\n\n// Note: Versions are represented as an integer. e.g. 7.1.5 is represented\n// as 70000100005. This method is easier than using a semver-parsing\n// package, but it breaks if we release x.y.z where x, y or z are\n// greater than 99_999.\nconst version = PACKAGE_JSON.version\n .split(\".\")\n .reduce((v, x) => v * 1e5 + +x, 0);\nconst versionKey = \"@babel/plugin-regexp-features/version\";\n\nexport interface Options {\n name: string;\n feature: keyof typeof FEATURES;\n options?: {\n useUnicodeFlag?: boolean;\n runtime?: boolean;\n };\n manipulateOptions?: PluginObject[\"manipulateOptions\"];\n}\n\nexport function createRegExpFeaturePlugin({\n name,\n feature,\n options = {},\n manipulateOptions = () => {},\n}: Options): PluginObject {\n return {\n name,\n\n manipulateOptions,\n\n pre() {\n const { file } = this;\n const features = file.get(featuresKey) ?? 0;\n let newFeatures = enableFeature(features, FEATURES[feature]);\n\n const { useUnicodeFlag, runtime } = options;\n if (useUnicodeFlag === false) {\n newFeatures = enableFeature(newFeatures, FEATURES.unicodeFlag);\n }\n if (newFeatures !== features) {\n file.set(featuresKey, newFeatures);\n }\n\n if (runtime !== undefined) {\n if (\n file.has(runtimeKey) &&\n file.get(runtimeKey) !== runtime &&\n // TODO(Babel 8): Remove this check. It's necessary because in Babel 7\n // we allow multiple copies of transform-named-capturing-groups-regex\n // with conflicting 'runtime' options.\n hasFeature(newFeatures, FEATURES.duplicateNamedCaptureGroups)\n ) {\n throw new Error(\n `The 'runtime' option must be the same for ` +\n `'@babel/plugin-transform-named-capturing-groups-regex' and ` +\n `'@babel/plugin-proposal-duplicate-named-capturing-groups-regex'.`,\n );\n }\n // TODO(Babel 8): Remove this check and always set it.\n // It's necessary because in Babel 7 we allow multiple copies of\n // transform-named-capturing-groups-regex with conflicting 'runtime' options.\n if (feature === \"namedCaptureGroups\") {\n if (!runtime || !file.has(runtimeKey)) file.set(runtimeKey, runtime);\n } else {\n file.set(runtimeKey, runtime);\n }\n }\n\n if (!file.has(versionKey) || file.get(versionKey) < version) {\n file.set(versionKey, version);\n }\n },\n\n visitor: {\n RegExpLiteral(path) {\n const { node } = path;\n const { file } = this;\n const features = file.get(featuresKey);\n const runtime = file.get(runtimeKey) ?? true;\n\n const regexpuOptions = generateRegexpuOptions(node.pattern, features);\n if (canSkipRegexpu(node, regexpuOptions)) {\n return;\n }\n\n const namedCaptureGroups: Record<string, number | number[]> = {\n __proto__: null,\n };\n if (regexpuOptions.namedGroups === \"transform\") {\n regexpuOptions.onNamedGroup = (name, index) => {\n const prev = namedCaptureGroups[name];\n if (typeof prev === \"number\") {\n namedCaptureGroups[name] = [prev, index];\n } else if (Array.isArray(prev)) {\n prev.push(index);\n } else {\n namedCaptureGroups[name] = index;\n }\n };\n }\n\n node.pattern = rewritePattern(node.pattern, node.flags, regexpuOptions);\n\n if (\n regexpuOptions.namedGroups === \"transform\" &&\n Object.keys(namedCaptureGroups).length > 0 &&\n runtime &&\n !isRegExpTest(path)\n ) {\n const call = t.callExpression(this.addHelper(\"wrapRegExp\"), [\n node,\n t.valueToNode(namedCaptureGroups),\n ]);\n annotateAsPure(call);\n\n path.replaceWith(call);\n }\n\n node.flags = transformFlags(regexpuOptions, node.flags);\n },\n },\n };\n}\n\nfunction isRegExpTest(path: NodePath<t.RegExpLiteral>) {\n return (\n path.parentPath.isMemberExpression({\n object: path.node,\n computed: false,\n }) && path.parentPath.get(\"property\").isIdentifier({ name: \"test\" })\n );\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AAOA;;AAGA;;AAEA;;AAQA,MAAMA,OAAO,GAAG,SACbC,KADa,CACP,GADO,EAEbC,MAFa,CAEN,CAACC,CAAD,EAAIC,CAAJ,KAAUD,CAAC,GAAG,GAAJ,GAAU,CAACC,CAFf,EAEkB,CAFlB,CAAhB;AAGA,MAAMC,UAAU,GAAG,uCAAnB;;AAYO,SAASC,yBAAT,CAAmC;EACxCC,IADwC;EAExCC,OAFwC;EAGxCC,OAAO,GAAG,EAH8B;EAIxCC,iBAAiB,GAAG,MAAM,CAAE;AAJY,CAAnC,EAKmB;EACxB,OAAO;IACLH,IADK;IAGLG,iBAHK;;IAKLC,GAAG,GAAG;MAAA;;MACJ,MAAM;QAAEC;MAAF,IAAW,IAAjB;MACA,MAAMC,QAAQ,gBAAGD,IAAI,CAACE,GAAL,CAASC,qBAAT,CAAH,wBAA4B,CAA1C;MACA,IAAIC,WAAW,GAAG,IAAAC,uBAAA,EAAcJ,QAAd,EAAwBK,kBAAA,CAASV,OAAT,CAAxB,CAAlB;MAEA,MAAM;QAAEW,cAAF;QAAkBC;MAAlB,IAA8BX,OAApC;;MACA,IAAIU,cAAc,KAAK,KAAvB,EAA8B;QAC5BH,WAAW,GAAG,IAAAC,uBAAA,EAAcD,WAAd,EAA2BE,kBAAA,CAASG,WAApC,CAAd;MACD;;MACD,IAAIL,WAAW,KAAKH,QAApB,EAA8B;QAC5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