1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.createRegExpFeaturePlugin = createRegExpFeaturePlugin;
|
---|
7 |
|
---|
8 | var _regexpuCore = require("regexpu-core");
|
---|
9 |
|
---|
10 | var _features = require("./features");
|
---|
11 |
|
---|
12 | var _util = require("./util");
|
---|
13 |
|
---|
14 | var _core = require("@babel/core");
|
---|
15 |
|
---|
16 | var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
---|
17 |
|
---|
18 | function pullFlag(node, flag) {
|
---|
19 | node.flags = node.flags.replace(flag, "");
|
---|
20 | }
|
---|
21 |
|
---|
22 | const version = "7.14.5".split(".").reduce((v, x) => v * 1e5 + +x, 0);
|
---|
23 | const versionKey = "@babel/plugin-regexp-features/version";
|
---|
24 |
|
---|
25 | function createRegExpFeaturePlugin({
|
---|
26 | name,
|
---|
27 | feature,
|
---|
28 | options = {}
|
---|
29 | }) {
|
---|
30 | return {
|
---|
31 | name,
|
---|
32 |
|
---|
33 | pre() {
|
---|
34 | var _file$get;
|
---|
35 |
|
---|
36 | const {
|
---|
37 | file
|
---|
38 | } = this;
|
---|
39 | const features = (_file$get = file.get(_features.featuresKey)) != null ? _file$get : 0;
|
---|
40 | let newFeatures = (0, _features.enableFeature)(features, _features.FEATURES[feature]);
|
---|
41 | const {
|
---|
42 | useUnicodeFlag,
|
---|
43 | runtime = true
|
---|
44 | } = options;
|
---|
45 |
|
---|
46 | if (useUnicodeFlag === false) {
|
---|
47 | newFeatures = (0, _features.enableFeature)(newFeatures, _features.FEATURES.unicodeFlag);
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (newFeatures !== features) {
|
---|
51 | file.set(_features.featuresKey, newFeatures);
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (!runtime) {
|
---|
55 | file.set(_features.runtimeKey, false);
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (!file.has(versionKey) || file.get(versionKey) < version) {
|
---|
59 | file.set(versionKey, version);
|
---|
60 | }
|
---|
61 | },
|
---|
62 |
|
---|
63 | visitor: {
|
---|
64 | RegExpLiteral(path) {
|
---|
65 | var _file$get2;
|
---|
66 |
|
---|
67 | const {
|
---|
68 | node
|
---|
69 | } = path;
|
---|
70 | const {
|
---|
71 | file
|
---|
72 | } = this;
|
---|
73 | const features = file.get(_features.featuresKey);
|
---|
74 | const runtime = (_file$get2 = file.get(_features.runtimeKey)) != null ? _file$get2 : true;
|
---|
75 | const regexpuOptions = (0, _util.generateRegexpuOptions)(node, features);
|
---|
76 |
|
---|
77 | if (regexpuOptions === null) {
|
---|
78 | return;
|
---|
79 | }
|
---|
80 |
|
---|
81 | const namedCaptureGroups = {};
|
---|
82 |
|
---|
83 | if (regexpuOptions.namedGroup) {
|
---|
84 | regexpuOptions.onNamedGroup = (name, index) => {
|
---|
85 | namedCaptureGroups[name] = index;
|
---|
86 | };
|
---|
87 | }
|
---|
88 |
|
---|
89 | node.pattern = _regexpuCore(node.pattern, node.flags, regexpuOptions);
|
---|
90 |
|
---|
91 | if (regexpuOptions.namedGroup && Object.keys(namedCaptureGroups).length > 0 && runtime && !isRegExpTest(path)) {
|
---|
92 | const call = _core.types.callExpression(this.addHelper("wrapRegExp"), [node, _core.types.valueToNode(namedCaptureGroups)]);
|
---|
93 |
|
---|
94 | (0, _helperAnnotateAsPure.default)(call);
|
---|
95 | path.replaceWith(call);
|
---|
96 | }
|
---|
97 |
|
---|
98 | if ((0, _features.hasFeature)(features, _features.FEATURES.unicodeFlag)) {
|
---|
99 | pullFlag(node, "u");
|
---|
100 | }
|
---|
101 |
|
---|
102 | if ((0, _features.hasFeature)(features, _features.FEATURES.dotAllFlag)) {
|
---|
103 | pullFlag(node, "s");
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|
108 | };
|
---|
109 | }
|
---|
110 |
|
---|
111 | function isRegExpTest(path) {
|
---|
112 | return path.parentPath.isMemberExpression({
|
---|
113 | object: path.node,
|
---|
114 | computed: false
|
---|
115 | }) && path.parentPath.get("property").isIdentifier({
|
---|
116 | name: "test"
|
---|
117 | });
|
---|
118 | } |
---|