Skip to content

Commit

Permalink
revert test of delegation to .exec().groups in .replace
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jun 20, 2021
1 parent 70842e9 commit bae33e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core-js/internals/regexp-sticky-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var fails = require('./fails');
var fails = require('../internals/fails');

// babel-minify transpiles RegExp('a', 'y') -> /a/y and it causes SyntaxError,
var RE = function (s, f) {
Expand Down
14 changes: 12 additions & 2 deletions packages/core-js/modules/es.string.replace.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
var fails = require('../internals/fails');
var anObject = require('../internals/an-object');
var toLength = require('../internals/to-length');
var toInteger = require('../internals/to-integer');
Expand All @@ -8,7 +9,6 @@ var advanceStringIndex = require('../internals/advance-string-index');
var getSubstitution = require('../internals/get-substitution');
var regExpExec = require('../internals/regexp-exec-abstract');
var wellKnownSymbol = require('../internals/well-known-symbol');
var UNSUPPORTED_NCG = require('../internals/regexp-unsupported-ncg');

var REPLACE = wellKnownSymbol('replace');
var max = Math.max;
Expand All @@ -33,6 +33,16 @@ var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
return false;
})();

var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
var re = /./;
re.exec = function () {
var result = [];
result.groups = { a: '7' };
return result;
};
return ''.replace(re, '$<a>') !== '7';
});

// @@replace logic
fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNative) {
var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
Expand Down Expand Up @@ -112,4 +122,4 @@ fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNa
return accumulatedResult + S.slice(nextSourcePosition);
}
];
}, UNSUPPORTED_NCG || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);
}, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);
12 changes: 10 additions & 2 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ GLOBAL.tests = {
'es.string.repeat': function () {
return String.prototype.repeat;
},
'es.string.replace': [NCG_SUPPORT, function () {
'es.string.replace': function () {
var O = {};
O[Symbol.replace] = function () { return 7; };

Expand All @@ -908,12 +908,20 @@ GLOBAL.tests = {
re.exec = function () { execCalled = true; return null; };
re[Symbol.replace]('');

var re2 = /./;
re2.exec = function () {
var result = [];
result.groups = { a: '7' };
return result;
};

return ''.replace(O) == 7
&& execCalled
&& ''.replace(re2, '$<a>') === '7'
// eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
&& 'a'.replace(/./, '$0') === '$0'
&& /./[Symbol.replace]('a', '$0') === '$0';
}],
},
'es.string.replace-all': function () {
return String.prototype.replaceAll;
},
Expand Down

0 comments on commit bae33e0

Please sign in to comment.