[79a0317] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.createRegExpFeaturePlugin = createRegExpFeaturePlugin;
|
---|
| 7 | var _regexpuCore = require("regexpu-core");
|
---|
| 8 | var _core = require("@babel/core");
|
---|
| 9 | var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
---|
| 10 | var _semver = require("semver");
|
---|
| 11 | var _features = require("./features.js");
|
---|
| 12 | var _util = require("./util.js");
|
---|
| 13 | const versionKey = "@babel/plugin-regexp-features/version";
|
---|
| 14 | function createRegExpFeaturePlugin({
|
---|
| 15 | name,
|
---|
| 16 | feature,
|
---|
| 17 | options = {},
|
---|
| 18 | manipulateOptions = () => {}
|
---|
| 19 | }) {
|
---|
| 20 | return {
|
---|
| 21 | name,
|
---|
| 22 | manipulateOptions,
|
---|
| 23 | pre() {
|
---|
| 24 | var _file$get;
|
---|
| 25 | const {
|
---|
| 26 | file
|
---|
| 27 | } = this;
|
---|
| 28 | const features = (_file$get = file.get(_features.featuresKey)) != null ? _file$get : 0;
|
---|
| 29 | let newFeatures = (0, _features.enableFeature)(features, _features.FEATURES[feature]);
|
---|
| 30 | const {
|
---|
| 31 | useUnicodeFlag,
|
---|
| 32 | runtime
|
---|
| 33 | } = options;
|
---|
| 34 | if (useUnicodeFlag === false) {
|
---|
| 35 | newFeatures = (0, _features.enableFeature)(newFeatures, _features.FEATURES.unicodeFlag);
|
---|
| 36 | }
|
---|
| 37 | if (newFeatures !== features) {
|
---|
| 38 | file.set(_features.featuresKey, newFeatures);
|
---|
| 39 | }
|
---|
| 40 | if (runtime !== undefined) {
|
---|
| 41 | if (file.has(_features.runtimeKey) && file.get(_features.runtimeKey) !== runtime && (0, _features.hasFeature)(newFeatures, _features.FEATURES.duplicateNamedCaptureGroups)) {
|
---|
| 42 | throw new Error(`The 'runtime' option must be the same for ` + `'@babel/plugin-transform-named-capturing-groups-regex' and ` + `'@babel/plugin-transform-duplicate-named-capturing-groups-regex'.`);
|
---|
| 43 | }
|
---|
| 44 | if (feature === "namedCaptureGroups") {
|
---|
| 45 | if (!runtime || !file.has(_features.runtimeKey)) file.set(_features.runtimeKey, runtime);
|
---|
| 46 | } else {
|
---|
| 47 | file.set(_features.runtimeKey, runtime);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 | {
|
---|
| 51 | if (typeof file.get(versionKey) === "number") {
|
---|
| 52 | file.set(versionKey, "7.26.3");
|
---|
| 53 | return;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.26.3")) {
|
---|
| 57 | file.set(versionKey, "7.26.3");
|
---|
| 58 | }
|
---|
| 59 | },
|
---|
| 60 | visitor: {
|
---|
| 61 | RegExpLiteral(path) {
|
---|
| 62 | var _file$get2, _newFlags;
|
---|
| 63 | const {
|
---|
| 64 | node
|
---|
| 65 | } = path;
|
---|
| 66 | const {
|
---|
| 67 | file
|
---|
| 68 | } = this;
|
---|
| 69 | const features = file.get(_features.featuresKey);
|
---|
| 70 | const runtime = (_file$get2 = file.get(_features.runtimeKey)) != null ? _file$get2 : true;
|
---|
| 71 | const regexpuOptions = (0, _util.generateRegexpuOptions)(node.pattern, features);
|
---|
| 72 | if ((0, _util.canSkipRegexpu)(node, regexpuOptions)) {
|
---|
| 73 | return;
|
---|
| 74 | }
|
---|
| 75 | const namedCaptureGroups = {
|
---|
| 76 | __proto__: null
|
---|
| 77 | };
|
---|
| 78 | if (regexpuOptions.namedGroups === "transform") {
|
---|
| 79 | regexpuOptions.onNamedGroup = (name, index) => {
|
---|
| 80 | const prev = namedCaptureGroups[name];
|
---|
| 81 | if (typeof prev === "number") {
|
---|
| 82 | namedCaptureGroups[name] = [prev, index];
|
---|
| 83 | } else if (Array.isArray(prev)) {
|
---|
| 84 | prev.push(index);
|
---|
| 85 | } else {
|
---|
| 86 | namedCaptureGroups[name] = index;
|
---|
| 87 | }
|
---|
| 88 | };
|
---|
| 89 | }
|
---|
| 90 | let newFlags;
|
---|
| 91 | if (regexpuOptions.modifiers === "transform") {
|
---|
| 92 | regexpuOptions.onNewFlags = flags => {
|
---|
| 93 | newFlags = flags;
|
---|
| 94 | };
|
---|
| 95 | }
|
---|
| 96 | node.pattern = _regexpuCore(node.pattern, node.flags, regexpuOptions);
|
---|
| 97 | if (regexpuOptions.namedGroups === "transform" && Object.keys(namedCaptureGroups).length > 0 && runtime && !isRegExpTest(path)) {
|
---|
| 98 | const call = _core.types.callExpression(this.addHelper("wrapRegExp"), [node, _core.types.valueToNode(namedCaptureGroups)]);
|
---|
| 99 | (0, _helperAnnotateAsPure.default)(call);
|
---|
| 100 | path.replaceWith(call);
|
---|
| 101 | }
|
---|
| 102 | node.flags = (0, _util.transformFlags)(regexpuOptions, (_newFlags = newFlags) != null ? _newFlags : node.flags);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | };
|
---|
| 106 | }
|
---|
| 107 | function isRegExpTest(path) {
|
---|
| 108 | return path.parentPath.isMemberExpression({
|
---|
| 109 | object: path.node,
|
---|
| 110 | computed: false
|
---|
| 111 | }) && path.parentPath.get("property").isIdentifier({
|
---|
| 112 | name: "test"
|
---|
| 113 | });
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | //# sourceMappingURL=index.js.map
|
---|