Simplify the wrapRegExp helper for named groups (#13201)
This commit is contained in:
parent
026e7f5a95
commit
a0e20ac5df
@ -116,6 +116,8 @@ module.exports = {
|
|||||||
rules: {
|
rules: {
|
||||||
"no-var": "off",
|
"no-var": "off",
|
||||||
"comma-dangle": "off",
|
"comma-dangle": "off",
|
||||||
|
"no-func-assign": "off",
|
||||||
|
"import/no-extraneous-dependencies": "off",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -29,3 +29,11 @@ const _typeof = {
|
|||||||
'\nexport default function _typeof(obj) {\n "@babel/helpers - typeof";\n if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {\n _typeof = function (obj) {\n return typeof obj;\n };\n } else {\n _typeof = function (obj) {\n return obj &&\n typeof Symbol === "function" &&\n obj.constructor === Symbol &&\n obj !== Symbol.prototype\n ? "symbol"\n : typeof obj;\n };\n }\n return _typeof(obj);\n}\n',
|
'\nexport default function _typeof(obj) {\n "@babel/helpers - typeof";\n if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {\n _typeof = function (obj) {\n return typeof obj;\n };\n } else {\n _typeof = function (obj) {\n return obj &&\n typeof Symbol === "function" &&\n obj.constructor === Symbol &&\n obj !== Symbol.prototype\n ? "symbol"\n : typeof obj;\n };\n }\n return _typeof(obj);\n}\n',
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const wrapRegExp = {
|
||||||
|
minVersion: "7.2.6",
|
||||||
|
ast: () =>
|
||||||
|
template.program.ast(
|
||||||
|
'\nimport setPrototypeOf from "setPrototypeOf";\nimport inherits from "inherits";\nexport default function _wrapRegExp() {\n _wrapRegExp = function (re, groups) {\n return new BabelRegExp(re, undefined, groups);\n };\n var _super = RegExp.prototype;\n var _groups = new WeakMap();\n function BabelRegExp(re, flags, groups) {\n var _this = new RegExp(re, flags);\n \n _groups.set(_this, groups || _groups.get(re));\n return setPrototypeOf(_this, BabelRegExp.prototype);\n }\n inherits(BabelRegExp, RegExp);\n BabelRegExp.prototype.exec = function (str) {\n var result = _super.exec.call(this, str);\n if (result) result.groups = buildGroups(result, this);\n return result;\n };\n BabelRegExp.prototype[Symbol.replace] = function (str, substitution) {\n if (typeof substitution === "string") {\n var groups = _groups.get(this);\n return _super[Symbol.replace].call(\n this,\n str,\n substitution.replace(/\\$<([^>]+)>/g, function (_, name) {\n return "$" + groups[name];\n })\n );\n } else if (typeof substitution === "function") {\n var _this = this;\n return _super[Symbol.replace].call(this, str, function () {\n var args = arguments;\n \n if (typeof args[args.length - 1] !== "object") {\n args = [].slice.call(args);\n args.push(buildGroups(args, _this));\n }\n return substitution.apply(this, args);\n });\n } else {\n return _super[Symbol.replace].call(this, str, substitution);\n }\n };\n function buildGroups(result, re) {\n \n \n var g = _groups.get(re);\n return Object.keys(g).reduce(function (groups, name) {\n groups[name] = result[g[name]];\n return groups;\n }, Object.create(null));\n }\n return _wrapRegExp.apply(this, arguments);\n}\n',
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|||||||
@ -2021,76 +2021,3 @@ if (!process.env.BABEL_8_BREAKING) {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
helpers.wrapRegExp = helper("7.2.6")`
|
|
||||||
import wrapNativeSuper from "wrapNativeSuper";
|
|
||||||
import getPrototypeOf from "getPrototypeOf";
|
|
||||||
import possibleConstructorReturn from "possibleConstructorReturn";
|
|
||||||
import inherits from "inherits";
|
|
||||||
|
|
||||||
export default function _wrapRegExp(re, groups) {
|
|
||||||
_wrapRegExp = function(re, groups) {
|
|
||||||
return new BabelRegExp(re, undefined, groups);
|
|
||||||
};
|
|
||||||
|
|
||||||
var _RegExp = wrapNativeSuper(RegExp);
|
|
||||||
var _super = RegExp.prototype;
|
|
||||||
var _groups = new WeakMap();
|
|
||||||
|
|
||||||
function BabelRegExp(re, flags, groups) {
|
|
||||||
var _this = _RegExp.call(this, re, flags);
|
|
||||||
// if the regex is recreated with 'g' flag
|
|
||||||
_groups.set(_this, groups || _groups.get(re));
|
|
||||||
return _this;
|
|
||||||
}
|
|
||||||
inherits(BabelRegExp, _RegExp);
|
|
||||||
|
|
||||||
BabelRegExp.prototype.exec = function(str) {
|
|
||||||
var result = _super.exec.call(this, str);
|
|
||||||
if (result) result.groups = buildGroups(result, this);
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
BabelRegExp.prototype[Symbol.replace] = function(str, substitution) {
|
|
||||||
if (typeof substitution === "string") {
|
|
||||||
var groups = _groups.get(this);
|
|
||||||
return _super[Symbol.replace].call(
|
|
||||||
this,
|
|
||||||
str,
|
|
||||||
substitution.replace(/\\$<([^>]+)>/g, function(_, name) {
|
|
||||||
return "$" + groups[name];
|
|
||||||
})
|
|
||||||
);
|
|
||||||
} else if (typeof substitution === "function") {
|
|
||||||
var _this = this;
|
|
||||||
return _super[Symbol.replace].call(
|
|
||||||
this,
|
|
||||||
str,
|
|
||||||
function() {
|
|
||||||
var args = [];
|
|
||||||
args.push.apply(args, arguments);
|
|
||||||
if (typeof args[args.length - 1] !== "object") {
|
|
||||||
// Modern engines already pass result.groups as the last arg.
|
|
||||||
args.push(buildGroups(args, _this));
|
|
||||||
}
|
|
||||||
return substitution.apply(this, args);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return _super[Symbol.replace].call(this, str, substitution);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function buildGroups(result, re) {
|
|
||||||
// NOTE: This function should return undefined if there are no groups,
|
|
||||||
// but in that case Babel doesn't add the wrapper anyway.
|
|
||||||
|
|
||||||
var g = _groups.get(re);
|
|
||||||
return Object.keys(g).reduce(function(groups, name) {
|
|
||||||
groups[name] = result[g[name]];
|
|
||||||
return groups;
|
|
||||||
}, Object.create(null));
|
|
||||||
}
|
|
||||||
|
|
||||||
return _wrapRegExp.apply(this, arguments);
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|||||||
65
packages/babel-helpers/src/helpers/wrapRegExp.js
Normal file
65
packages/babel-helpers/src/helpers/wrapRegExp.js
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/* @minVersion 7.2.6 */
|
||||||
|
|
||||||
|
import setPrototypeOf from "setPrototypeOf";
|
||||||
|
import inherits from "inherits";
|
||||||
|
|
||||||
|
export default function _wrapRegExp() {
|
||||||
|
_wrapRegExp = function (re, groups) {
|
||||||
|
return new BabelRegExp(re, undefined, groups);
|
||||||
|
};
|
||||||
|
|
||||||
|
var _super = RegExp.prototype;
|
||||||
|
var _groups = new WeakMap();
|
||||||
|
|
||||||
|
function BabelRegExp(re, flags, groups) {
|
||||||
|
var _this = new RegExp(re, flags);
|
||||||
|
// if the regex is recreated with 'g' flag
|
||||||
|
_groups.set(_this, groups || _groups.get(re));
|
||||||
|
return setPrototypeOf(_this, BabelRegExp.prototype);
|
||||||
|
}
|
||||||
|
inherits(BabelRegExp, RegExp);
|
||||||
|
|
||||||
|
BabelRegExp.prototype.exec = function (str) {
|
||||||
|
var result = _super.exec.call(this, str);
|
||||||
|
if (result) result.groups = buildGroups(result, this);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
BabelRegExp.prototype[Symbol.replace] = function (str, substitution) {
|
||||||
|
if (typeof substitution === "string") {
|
||||||
|
var groups = _groups.get(this);
|
||||||
|
return _super[Symbol.replace].call(
|
||||||
|
this,
|
||||||
|
str,
|
||||||
|
substitution.replace(/\$<([^>]+)>/g, function (_, name) {
|
||||||
|
return "$" + groups[name];
|
||||||
|
})
|
||||||
|
);
|
||||||
|
} else if (typeof substitution === "function") {
|
||||||
|
var _this = this;
|
||||||
|
return _super[Symbol.replace].call(this, str, function () {
|
||||||
|
var args = arguments;
|
||||||
|
// Modern engines already pass result.groups returned by exec() as the last arg.
|
||||||
|
if (typeof args[args.length - 1] !== "object") {
|
||||||
|
args = [].slice.call(args);
|
||||||
|
args.push(buildGroups(args, _this));
|
||||||
|
}
|
||||||
|
return substitution.apply(this, args);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return _super[Symbol.replace].call(this, str, substitution);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function buildGroups(result, re) {
|
||||||
|
// NOTE: This function should return undefined if there are no groups,
|
||||||
|
// but in that case Babel doesn't add the wrapper anyway.
|
||||||
|
|
||||||
|
var g = _groups.get(re);
|
||||||
|
return Object.keys(g).reduce(function (groups, name) {
|
||||||
|
groups[name] = result[g[name]];
|
||||||
|
return groups;
|
||||||
|
}, Object.create(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
return _wrapRegExp.apply(this, arguments);
|
||||||
|
}
|
||||||
@ -45,6 +45,15 @@
|
|||||||
"./helpers/typeof.js"
|
"./helpers/typeof.js"
|
||||||
],
|
],
|
||||||
"./helpers/esm/typeof": "./helpers/esm/typeof.js",
|
"./helpers/esm/typeof": "./helpers/esm/typeof.js",
|
||||||
|
"./helpers/wrapRegExp": [
|
||||||
|
{
|
||||||
|
"node": "./helpers/wrapRegExp.js",
|
||||||
|
"import": "./helpers/esm/wrapRegExp.js",
|
||||||
|
"default": "./helpers/wrapRegExp.js"
|
||||||
|
},
|
||||||
|
"./helpers/wrapRegExp.js"
|
||||||
|
],
|
||||||
|
"./helpers/esm/wrapRegExp": "./helpers/esm/wrapRegExp.js",
|
||||||
"./helpers/asyncIterator": [
|
"./helpers/asyncIterator": [
|
||||||
{
|
{
|
||||||
"node": "./helpers/asyncIterator.js",
|
"node": "./helpers/asyncIterator.js",
|
||||||
@ -801,15 +810,6 @@
|
|||||||
"./helpers/classPrivateMethodSet.js"
|
"./helpers/classPrivateMethodSet.js"
|
||||||
],
|
],
|
||||||
"./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js",
|
"./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js",
|
||||||
"./helpers/wrapRegExp": [
|
|
||||||
{
|
|
||||||
"node": "./helpers/wrapRegExp.js",
|
|
||||||
"import": "./helpers/esm/wrapRegExp.js",
|
|
||||||
"default": "./helpers/wrapRegExp.js"
|
|
||||||
},
|
|
||||||
"./helpers/wrapRegExp.js"
|
|
||||||
],
|
|
||||||
"./helpers/esm/wrapRegExp": "./helpers/esm/wrapRegExp.js",
|
|
||||||
"./package": "./package.json",
|
"./package": "./package.json",
|
||||||
"./package.json": "./package.json",
|
"./package.json": "./package.json",
|
||||||
"./regenerator": "./regenerator/index.js",
|
"./regenerator": "./regenerator/index.js",
|
||||||
|
|||||||
@ -44,6 +44,15 @@
|
|||||||
"./helpers/typeof.js"
|
"./helpers/typeof.js"
|
||||||
],
|
],
|
||||||
"./helpers/esm/typeof": "./helpers/esm/typeof.js",
|
"./helpers/esm/typeof": "./helpers/esm/typeof.js",
|
||||||
|
"./helpers/wrapRegExp": [
|
||||||
|
{
|
||||||
|
"node": "./helpers/wrapRegExp.js",
|
||||||
|
"import": "./helpers/esm/wrapRegExp.js",
|
||||||
|
"default": "./helpers/wrapRegExp.js"
|
||||||
|
},
|
||||||
|
"./helpers/wrapRegExp.js"
|
||||||
|
],
|
||||||
|
"./helpers/esm/wrapRegExp": "./helpers/esm/wrapRegExp.js",
|
||||||
"./helpers/asyncIterator": [
|
"./helpers/asyncIterator": [
|
||||||
{
|
{
|
||||||
"node": "./helpers/asyncIterator.js",
|
"node": "./helpers/asyncIterator.js",
|
||||||
@ -800,15 +809,6 @@
|
|||||||
"./helpers/classPrivateMethodSet.js"
|
"./helpers/classPrivateMethodSet.js"
|
||||||
],
|
],
|
||||||
"./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js",
|
"./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js",
|
||||||
"./helpers/wrapRegExp": [
|
|
||||||
{
|
|
||||||
"node": "./helpers/wrapRegExp.js",
|
|
||||||
"import": "./helpers/esm/wrapRegExp.js",
|
|
||||||
"default": "./helpers/wrapRegExp.js"
|
|
||||||
},
|
|
||||||
"./helpers/wrapRegExp.js"
|
|
||||||
],
|
|
||||||
"./helpers/esm/wrapRegExp": "./helpers/esm/wrapRegExp.js",
|
|
||||||
"./package": "./package.json",
|
"./package": "./package.json",
|
||||||
"./package.json": "./package.json",
|
"./package.json": "./package.json",
|
||||||
"./regenerator": "./regenerator/index.js",
|
"./regenerator": "./regenerator/index.js",
|
||||||
|
|||||||
@ -44,6 +44,15 @@
|
|||||||
"./helpers/typeof.js"
|
"./helpers/typeof.js"
|
||||||
],
|
],
|
||||||
"./helpers/esm/typeof": "./helpers/esm/typeof.js",
|
"./helpers/esm/typeof": "./helpers/esm/typeof.js",
|
||||||
|
"./helpers/wrapRegExp": [
|
||||||
|
{
|
||||||
|
"node": "./helpers/wrapRegExp.js",
|
||||||
|
"import": "./helpers/esm/wrapRegExp.js",
|
||||||
|
"default": "./helpers/wrapRegExp.js"
|
||||||
|
},
|
||||||
|
"./helpers/wrapRegExp.js"
|
||||||
|
],
|
||||||
|
"./helpers/esm/wrapRegExp": "./helpers/esm/wrapRegExp.js",
|
||||||
"./helpers/asyncIterator": [
|
"./helpers/asyncIterator": [
|
||||||
{
|
{
|
||||||
"node": "./helpers/asyncIterator.js",
|
"node": "./helpers/asyncIterator.js",
|
||||||
@ -800,15 +809,6 @@
|
|||||||
"./helpers/classPrivateMethodSet.js"
|
"./helpers/classPrivateMethodSet.js"
|
||||||
],
|
],
|
||||||
"./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js",
|
"./helpers/esm/classPrivateMethodSet": "./helpers/esm/classPrivateMethodSet.js",
|
||||||
"./helpers/wrapRegExp": [
|
|
||||||
{
|
|
||||||
"node": "./helpers/wrapRegExp.js",
|
|
||||||
"import": "./helpers/esm/wrapRegExp.js",
|
|
||||||
"default": "./helpers/wrapRegExp.js"
|
|
||||||
},
|
|
||||||
"./helpers/wrapRegExp.js"
|
|
||||||
],
|
|
||||||
"./helpers/esm/wrapRegExp": "./helpers/esm/wrapRegExp.js",
|
|
||||||
"./package": "./package.json",
|
"./package": "./package.json",
|
||||||
"./package.json": "./package.json",
|
"./package.json": "./package.json",
|
||||||
"./regenerator": "./regenerator/index.js",
|
"./regenerator": "./regenerator/index.js",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user