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