Skip to content

Commit 3f5640c

Browse files
committed
untangle fix-regexp-well-known-symbol-logic by moving feature detection to related modules
1 parent 1a510ac commit 3f5640c

File tree

3 files changed

+48
-55
lines changed

3 files changed

+48
-55
lines changed

packages/core-js/internals/fix-regexp-well-known-symbol-logic.js

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,7 @@ var createNonEnumerableProperty = require('../internals/create-non-enumerable-pr
1010
var SPECIES = wellKnownSymbol('species');
1111
var RegExpPrototype = RegExp.prototype;
1212

13-
var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
14-
// #replace needs built-in support for named groups.
15-
// #match works fine because it just return the exec results, even if it has
16-
// a "grops" property.
17-
var re = /./;
18-
re.exec = function () {
19-
var result = [];
20-
result.groups = { a: '7' };
21-
return result;
22-
};
23-
return ''.replace(re, '$<a>') !== '7';
24-
});
25-
26-
// IE <= 11 replaces $0 with the whole match, as if it was $&
27-
// https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
28-
var REPLACE_KEEPS_$0 = (function () {
29-
// eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
30-
return 'a'.replace(/./, '$0') === '$0';
31-
})();
32-
33-
var REPLACE = wellKnownSymbol('replace');
34-
// Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
35-
var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
36-
if (/./[REPLACE]) {
37-
return /./[REPLACE]('a', '$0') === '';
38-
}
39-
return false;
40-
})();
41-
42-
// Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
43-
// Weex JS has frozen built-in prototypes, so use try / catch wrapper
44-
var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
45-
// eslint-disable-next-line regexp/no-empty-group -- required for testing
46-
var re = /(?:)/;
47-
var originalExec = re.exec;
48-
re.exec = function () { return originalExec.apply(this, arguments); };
49-
var result = 'ab'.split(re);
50-
return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
51-
});
52-
53-
module.exports = function (KEY, length, exec, sham) {
13+
module.exports = function (KEY, length, exec, FORCED, sham) {
5414
var SYMBOL = wellKnownSymbol(KEY);
5515

5616
var DELEGATES_TO_SYMBOL = !fails(function () {
@@ -87,12 +47,7 @@ module.exports = function (KEY, length, exec, sham) {
8747
if (
8848
!DELEGATES_TO_SYMBOL ||
8949
!DELEGATES_TO_EXEC ||
90-
(KEY === 'replace' && !(
91-
REPLACE_SUPPORTS_NAMED_GROUPS &&
92-
REPLACE_KEEPS_$0 &&
93-
!REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
94-
)) ||
95-
(KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
50+
FORCED
9651
) {
9752
var nativeRegExpMethod = /./[SYMBOL];
9853
var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
@@ -107,9 +62,6 @@ module.exports = function (KEY, length, exec, sham) {
10762
return { done: true, value: nativeMethod.call(str, regexp, arg2) };
10863
}
10964
return { done: false };
110-
}, {
111-
REPLACE_KEEPS_$0: REPLACE_KEEPS_$0,
112-
REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE: REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
11365
});
11466
var stringMethod = methods[0];
11567
var regexMethod = methods[1];

packages/core-js/modules/es.string.replace.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,47 @@ var requireObjectCoercible = require('../internals/require-object-coercible');
77
var advanceStringIndex = require('../internals/advance-string-index');
88
var getSubstitution = require('../internals/get-substitution');
99
var regExpExec = require('../internals/regexp-exec-abstract');
10+
var fails = require('../internals/fails');
11+
var wellKnownSymbol = require('../internals/well-known-symbol');
1012

13+
var REPLACE = wellKnownSymbol('replace');
1114
var max = Math.max;
1215
var min = Math.min;
1316

1417
var maybeToString = function (it) {
1518
return it === undefined ? it : String(it);
1619
};
1720

21+
var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
22+
// #replace needs built-in support for named groups.
23+
// #match works fine because it just return the exec results, even if it has
24+
// a "groups" property.
25+
var re = /./;
26+
re.exec = function () {
27+
var result = [];
28+
result.groups = { a: '7' };
29+
return result;
30+
};
31+
return ''.replace(re, '$<a>') !== '7';
32+
});
33+
34+
// IE <= 11 replaces $0 with the whole match, as if it was $&
35+
// https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
36+
var REPLACE_KEEPS_$0 = (function () {
37+
// eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
38+
return 'a'.replace(/./, '$0') === '$0';
39+
})();
40+
41+
// Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
42+
var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
43+
if (/./[REPLACE]) {
44+
return /./[REPLACE]('a', '$0') === '';
45+
}
46+
return false;
47+
})();
48+
1849
// @@replace logic
19-
fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, maybeCallNative, reason) {
20-
var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = reason.REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE;
21-
var REPLACE_KEEPS_$0 = reason.REPLACE_KEEPS_$0;
50+
fixRegExpWellKnownSymbolLogic('replace', 2, function (_, nativeReplace, maybeCallNative) {
2251
var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
2352

2453
return [
@@ -95,4 +124,4 @@ fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, ma
95124
return accumulatedResult + S.slice(nextSourcePosition);
96125
}
97126
];
98-
});
127+
}, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);

packages/core-js/modules/es.string.split.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ var toLength = require('../internals/to-length');
99
var callRegExpExec = require('../internals/regexp-exec-abstract');
1010
var regexpExec = require('../internals/regexp-exec');
1111
var stickyHelpers = require('../internals/regexp-sticky-helpers');
12+
var fails = require('../internals/fails');
1213

1314
var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y;
1415
var arrayPush = [].push;
1516
var min = Math.min;
1617
var MAX_UINT32 = 0xFFFFFFFF;
1718

19+
// Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
20+
// Weex JS has frozen built-in prototypes, so use try / catch wrapper
21+
var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
22+
// eslint-disable-next-line regexp/no-empty-group -- required for testing
23+
var re = /(?:)/;
24+
var originalExec = re.exec;
25+
re.exec = function () { return originalExec.apply(this, arguments); };
26+
var result = 'ab'.split(re);
27+
return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
28+
});
29+
1830
// @@split logic
1931
fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCallNative) {
2032
var internalSplit;
@@ -131,4 +143,4 @@ fixRegExpWellKnownSymbolLogic('split', 2, function (SPLIT, nativeSplit, maybeCal
131143
return A;
132144
}
133145
];
134-
}, UNSUPPORTED_Y);
146+
}, !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC, UNSUPPORTED_Y);

0 commit comments

Comments
 (0)