-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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 = []; | ||||||||
args.push.apply(args, arguments); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Suggested change
|
||||||||
if (typeof args[args.length - 1] !== "object") { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is incorrect. Because we transform There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Crazy, I didn't realize it was accessing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more perf reason that makes implementers hate regexp subclasses 😛 |
||||||||
// 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); | ||||||||
} |
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.