123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.createRegExpFeaturePlugin = createRegExpFeaturePlugin;
- var _regexpuCore = require("regexpu-core");
- var _features = require("./features");
- var _util = require("./util");
- var _core = require("@babel/core");
- var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
- const version = "7.19.0".split(".").reduce((v, x) => v * 1e5 + +x, 0);
- const versionKey = "@babel/plugin-regexp-features/version";
- function createRegExpFeaturePlugin({
- name,
- feature,
- options = {},
- manipulateOptions = () => {}
- }) {
- return {
- name,
- manipulateOptions,
- 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
- } = options;
- if (useUnicodeFlag === false) {
- newFeatures = (0, _features.enableFeature)(newFeatures, _features.FEATURES.unicodeFlag);
- }
- if (newFeatures !== features) {
- file.set(_features.featuresKey, newFeatures);
- }
- if (runtime !== undefined) {
- if (file.has(_features.runtimeKey) && file.get(_features.runtimeKey) !== runtime && (0, _features.hasFeature)(newFeatures, _features.FEATURES.duplicateNamedCaptureGroups)) {
- throw new Error(`The 'runtime' option must be the same for ` + `'@babel/plugin-transform-named-capturing-groups-regex' and ` + `'@babel/plugin-proposal-duplicate-named-capturing-groups-regex'.`);
- }
- if (feature === "namedCaptureGroups") {
- if (!runtime || !file.has(_features.runtimeKey)) file.set(_features.runtimeKey, runtime);
- } else {
- file.set(_features.runtimeKey, runtime);
- }
- }
- 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.pattern, features);
- if ((0, _util.canSkipRegexpu)(node, regexpuOptions)) {
- return;
- }
- const namedCaptureGroups = {
- __proto__: null
- };
- if (regexpuOptions.namedGroups === "transform") {
- regexpuOptions.onNamedGroup = (name, index) => {
- const prev = namedCaptureGroups[name];
- if (typeof prev === "number") {
- namedCaptureGroups[name] = [prev, index];
- } else if (Array.isArray(prev)) {
- prev.push(index);
- } else {
- namedCaptureGroups[name] = index;
- }
- };
- }
- node.pattern = _regexpuCore(node.pattern, node.flags, regexpuOptions);
- if (regexpuOptions.namedGroups === "transform" && 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);
- }
- node.flags = (0, _util.transformFlags)(regexpuOptions, node.flags);
- }
- }
- };
- }
- function isRegExpTest(path) {
- return path.parentPath.isMemberExpression({
- object: path.node,
- computed: false
- }) && path.parentPath.get("property").isIdentifier({
- name: "test"
- });
- }
- //# sourceMappingURL=index.js.map
|