@babel/helper-create-regexp-features-plugin
Advanced tools
Comparing version
@@ -20,2 +20,3 @@ "use strict"; | ||
}); | ||
exports.FEATURES = FEATURES; | ||
@@ -26,7 +27,5 @@ const featuresKey = "@babel/plugin-regexp-features/featuresKey"; | ||
exports.runtimeKey = runtimeKey; | ||
function enableFeature(features, feature) { | ||
return features | feature; | ||
} | ||
function hasFeature(features, feature) { | ||
@@ -33,0 +32,0 @@ return !!(features & feature); |
@@ -7,16 +7,9 @@ "use strict"; | ||
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 version = "7.20.5".split(".").reduce((v, x) => v * 1e5 + +x, 0); | ||
const versionKey = "@babel/plugin-regexp-features/version"; | ||
function createRegExpFeaturePlugin({ | ||
@@ -31,6 +24,4 @@ name, | ||
manipulateOptions, | ||
pre() { | ||
var _file$get; | ||
const { | ||
@@ -45,16 +36,13 @@ file | ||
} = 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)) { | ||
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") { | ||
@@ -66,3 +54,2 @@ if (!runtime || !file.has(_features.runtimeKey)) file.set(_features.runtimeKey, runtime); | ||
} | ||
if (!file.has(versionKey) || file.get(versionKey) < version) { | ||
@@ -72,7 +59,5 @@ file.set(versionKey, version); | ||
}, | ||
visitor: { | ||
RegExpLiteral(path) { | ||
var _file$get2; | ||
const { | ||
@@ -87,15 +72,11 @@ node | ||
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") { | ||
@@ -110,19 +91,13 @@ namedCaptureGroups[name] = [prev, 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) { | ||
@@ -129,0 +104,0 @@ return path.parentPath.isMemberExpression({ |
@@ -9,5 +9,3 @@ "use strict"; | ||
exports.transformFlags = transformFlags; | ||
var _features = require("./features"); | ||
function generateRegexpuOptions(pattern, toTransform) { | ||
@@ -17,15 +15,12 @@ const feat = (name, ok = "transform") => { | ||
}; | ||
const featDuplicateNamedGroups = () => { | ||
if (!feat("duplicateNamedCaptureGroups")) return false; | ||
const regex = /\(\?<([^>]+)>/g; | ||
const seen = new Set(); | ||
for (let match; match = regex.exec(pattern); seen.add(match[1])) { | ||
if (seen.has(match[1])) return "transform"; | ||
} | ||
return false; | ||
}; | ||
return { | ||
@@ -40,3 +35,2 @@ unicodeFlag: feat("unicodeFlag"), | ||
} | ||
function canSkipRegexpu(node, options) { | ||
@@ -47,10 +41,7 @@ const { | ||
} = node; | ||
if (flags.includes("v")) { | ||
if (options.unicodeSetsFlag === "transform") return false; | ||
} | ||
if (flags.includes("u")) { | ||
if (options.unicodeFlag === "transform") return false; | ||
if (options.unicodePropertyEscapes === "transform" && /\\[pP]{/.test(pattern)) { | ||
@@ -60,14 +51,10 @@ return false; | ||
} | ||
if (flags.includes("s")) { | ||
if (options.dotAllFlag === "transform") return false; | ||
} | ||
if (options.namedGroups === "transform" && /\(\?<(?![=!])/.test(pattern)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
function transformFlags(regexpuOptions, flags) { | ||
@@ -77,11 +64,8 @@ if (regexpuOptions.unicodeSetsFlag === "transform") { | ||
} | ||
if (regexpuOptions.unicodeFlag === "transform") { | ||
flags = flags.replace("u", ""); | ||
} | ||
if (regexpuOptions.dotAllFlag === "transform") { | ||
flags = flags.replace("s", ""); | ||
} | ||
return flags; | ||
@@ -88,0 +72,0 @@ } |
{ | ||
"name": "@babel/helper-create-regexp-features-plugin", | ||
"version": "7.19.0", | ||
"version": "7.20.5", | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
@@ -22,3 +22,3 @@ "license": "MIT", | ||
"@babel/helper-annotate-as-pure": "^7.18.6", | ||
"regexpu-core": "^5.1.0" | ||
"regexpu-core": "^5.2.1" | ||
}, | ||
@@ -29,3 +29,3 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.19.0", | ||
"@babel/core": "^7.20.5", | ||
"@babel/helper-plugin-test-runner": "^7.18.6" | ||
@@ -32,0 +32,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
198
0.51%25082
-0.1%Updated