New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify the wrapRegExp
helper for named groups
#13201
Simplify the wrapRegExp
helper for named groups
#13201
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/45619/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit aea8806:
|
BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { | ||
if (typeof substitution === "string") { | ||
var groups = _groups.get(this); | ||
return _super[Symbol.replace].call( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We can also hoist _super[Symbol.replace]
if we want to golf a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not caching it makes it possible to monkey-patch RegExp.protototype[Symbol.replace]
, similarly to how its possible with native regexps.
var args = []; | ||
args.push.apply(args, arguments); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
var args = []; | |
args.push.apply(args, arguments); | |
var args = [].slice.call(arguments); |
return _super[Symbol.replace].call(this, str, function () { | ||
var args = []; | ||
args.push.apply(args, arguments); | ||
if (typeof args[args.length - 1] !== "object") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is incorrect. Because we transform (?<year>\d{4})
to ([0-9]{4})
, if there is a object here, it won't include the year
group. I think we ned to pop and push if there's an object, and only push if there's not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The object here is not the one internally built by the engine, but it's the .groups
property returned by our exec()
(which defined .groups.year
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can verify it with
class MyRegExp extends RegExp {
exec() {
return Object.assign(["a", "b"], { groups: { name: "c" } });
}
[Symbol.replace](str, subst) {
super[Symbol.replace](str, (...args) => {
console.log(args);
return "_";
});
}
}
"abc".replace(new MyRegExp("b"));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crazy, I didn't realize it was accessing the .groups
directly from result
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more perf reason that makes implementers hate regexp subclasses 😛
return _super[Symbol.replace].call(this, str, function () { | ||
var args = []; | ||
args.push.apply(args, arguments); | ||
if (typeof args[args.length - 1] !== "object") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crazy, I didn't realize it was accessing the .groups
directly from result
.
possibleConstructorReturn
helper dependency was unused.wrapNativeSuper
, since we can usenew RegExp
and then manually set the result prototype.When compiling this code:
This was the old output (4.3kB, 2.3kB minified)
This is the new output (2.1kB, 1.2kB minified)