Skip to content
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

Polyfll named groups in RegExp#@@replace #435

Merged
merged 1 commit into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions client/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1795,15 +1795,13 @@ var SPECIES = wks('species');

module.exports = function (KEY, length, exec) {
var SYMBOL = wks(KEY);
var fns = exec(defined, SYMBOL, ''[KEY]);
var strfn = fns[0];
var rxfn = fns[1];
if (fails(function () {

var delegates = !fails(function () {
// String methods call symbol-named RegEp methods
var O = {};
O[SYMBOL] = function () { return 7; };
return ''[KEY](O) != 7;
}) || fails(function () {
}) && !fails(function () {
// Symbol-named RegExp methods call .exec
var execCalled = false;
var re = /a/;
Expand All @@ -1816,7 +1814,29 @@ module.exports = function (KEY, length, exec) {
}
re[SYMBOL]('');
return !execCalled;
})) {
});

var replaceSupportsNamedGroups = KEY === 'replace' && !fails(function () {
// #replace needs built-in support for named groups.
// #match works fine because it just return the exec results, even if it has
// a "grops" property.
var re = /./;
re.exec = function () {
var result = [];
result.groups = { a: '7' };
return result;
};
return ''.replace(re, '$<a>') !== '7';
});

if (!delegates || (KEY === 'replace' && !replaceSupportsNamedGroups)) {
var fns = exec(defined, SYMBOL, ''[KEY], /./[SYMBOL], {
delegates: delegates,
replaceSupportsNamedGroups: replaceSupportsNamedGroups
});
var strfn = fns[0];
var rxfn = fns[1];

redefine(String.prototype, KEY, strfn);
redefine(RegExp.prototype, SYMBOL, length == 2
// 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
Expand Down Expand Up @@ -6174,7 +6194,7 @@ var maybeToString = function (it) {
};

// @@replace logic
__webpack_require__(62)('replace', 2, function (defined, REPLACE, $replace) {
__webpack_require__(62)('replace', 2, function (defined, REPLACE, $replace, RegExp$replace, reason) {
return [
// `String.prototype.replace` method
// https://tc39.github.io/ecma262/#sec-string.prototype.replace
Expand All @@ -6188,7 +6208,16 @@ __webpack_require__(62)('replace', 2, function (defined, REPLACE, $replace) {
// `RegExp.prototype[@@replace]` method
// https://tc39.github.io/ecma262/#sec-regexp.prototype-@@replace
function (regexp, replaceValue) {
if (regexp.exec === nativeExec) return $replace.call(this, regexp, replaceValue);
if (regexp.exec === nativeExec) {
if (reason.delegates) {
// The native #replaceMethod already delegates to @@replace (this
// polyfilled function, leasing to infinite recursion).
// We avoid it by directly calling the native @@replace method.
return RegExp$replace.call(regexp, this, replaceValue);
}
return $replace.call(this, regexp, replaceValue);
}

var rx = anObject(regexp);
var S = String(this);
var functionalReplace = typeof replaceValue === 'function';
Expand Down
6 changes: 3 additions & 3 deletions client/core.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/core.min.js.map

Large diffs are not rendered by default.

Loading