Skip to content

Commit 41a8b12

Browse files
committed
some #453-related changes
1 parent 8a90bf6 commit 41a8b12

File tree

10 files changed

+48
-50
lines changed

10 files changed

+48
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- Add triggering unhandled `Promise` rejection events (instead of only global handlers), [#205](https://github.com/zloirock/core-js/issues/205).
5353
- Add support of `@@isConcatSpreadable` to `Array#concat`.
5454
- Add support of `@@species` to `Array#{concat, filter, map, slice, splice}`.
55-
- Add direct `.exec` calling to `RegExp#{@@replace, @@split, @@match, @@search}`, [#411](https://github.com/zloirock/core-js/issues/411).
55+
- Add direct `.exec` calling to `RegExp#{@@replace, @@split, @@match, @@search}`. Also, added fixes for `RegExp#exec` method. [#411](https://github.com/zloirock/core-js/issues/411), [#434](https://github.com/zloirock/core-js/issues/434), [#453](https://github.com/zloirock/core-js/issues/453).
5656
- Correct iterators prototypes chain, related [#261](https://github.com/zloirock/core-js/issues/261).
5757
- Correct Typed Arrays prototypes chain, related [#378](https://github.com/zloirock/core-js/issues/378).
5858
- Make the internal state of polyfilled features completely unobservable, [#146](https://github.com/zloirock/core-js/issues/146).

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ class String {
530530

531531
class RegExp {
532532
constructor(pattern: RegExp | string, flags?: string): RegExp; // ES2015+ fix - can alter flags (IE9+)
533+
exec(): Array<string | undefined> | null; // IE8- fixes
533534
toString(): string; // ES2015+ fix - generic
534535
@@match(string: string): Array | null;
535536
@@replace(string: string, replaceValue: Function | string): string;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,39 @@ var regexpExec = require('../internals/regexp-exec');
88

99
var SPECIES = wellKnownSymbol('species');
1010

11+
var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
12+
// #replace needs built-in support for named groups.
13+
// #match works fine because it just return the exec results, even if it has
14+
// a "grops" property.
15+
var re = /./;
16+
re.exec = function () {
17+
var result = [];
18+
result.groups = { a: '7' };
19+
return result;
20+
};
21+
return ''.replace(re, '$<a>') !== '7';
22+
});
23+
24+
var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = (function () {
25+
// Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
26+
var re = /(?:)/;
27+
var originalExec = re.exec;
28+
re.exec = function () { return originalExec.apply(this, arguments); };
29+
var result = 'ab'.split(re);
30+
return result.length === 2 && result[0] === 'a' && result[1] === 'b';
31+
})();
32+
1133
module.exports = function (KEY, length, exec, sham) {
1234
var SYMBOL = wellKnownSymbol(KEY);
1335

14-
var delegatesToSymbol = !fails(function () {
36+
var DELEGATES_TO_SYMBOL = !fails(function () {
1537
// String methods call symbol-named RegEp methods
1638
var O = {};
1739
O[SYMBOL] = function () { return 7; };
1840
return ''[KEY](O) != 7;
1941
});
2042

21-
var delegatesToExec = delegatesToSymbol ? !fails(function () {
43+
var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL ? !fails(function () {
2244
// Symbol-named RegExp methods call .exec
2345
var execCalled = false;
2446
var re = /a/;
@@ -35,42 +57,20 @@ module.exports = function (KEY, length, exec, sham) {
3557
return !execCalled;
3658
}) : undefined;
3759

38-
var replaceSupportsNamedGroups = KEY === 'replace' && !fails(function () {
39-
// #replace needs built-in support for named groups.
40-
// #match works fine because it just return the exec results, even if it has
41-
// a "grops" property.
42-
var re = /./;
43-
re.exec = function () {
44-
var result = [];
45-
result.groups = { a: '7' };
46-
return result;
47-
};
48-
return ''.replace(re, '$<a>') !== '7';
49-
});
50-
51-
var splitWorksWithOverwrittenExec = KEY === 'split' && (function () {
52-
// Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
53-
var re = /(?:)/;
54-
var originalExec = re.exec;
55-
re.exec = function () { return originalExec.apply(this, arguments); };
56-
var result = 'ab'.split(re);
57-
return result.length === 2 && result[0] === 'a' && result[1] === 'b';
58-
})();
59-
6060
if (
61-
!delegatesToSymbol ||
62-
!delegatesToExec ||
63-
(KEY === 'replace' && !replaceSupportsNamedGroups) ||
64-
(KEY === 'split' && !splitWorksWithOverwrittenExec)
61+
!DELEGATES_TO_SYMBOL ||
62+
!DELEGATES_TO_EXEC ||
63+
(KEY === 'replace' && !REPLACE_SUPPORTS_NAMED_GROUPS) ||
64+
(KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
6565
) {
6666
var nativeRegExpMethod = /./[SYMBOL];
6767
var methods = exec(
6868
requireObjectCoercible,
6969
SYMBOL,
7070
''[KEY],
71-
function maybeCallNative(nativeMethod, regexp, str, arg2, forceStringMethod) {
72-
if (regexp.exec === regexpExec.impl) {
73-
if (delegatesToSymbol && !forceStringMethod) {
71+
function (nativeMethod, regexp, str, arg2, forceStringMethod) {
72+
if (regexp.exec === regexpExec) {
73+
if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
7474
// The native String method already delegates to @@method (this
7575
// polyfilled function), leasing to infinite recursion.
7676
// We avoid it by directly calling the native @@method method.

packages/core-js/internals/regexp-exec-abstract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ module.exports = function (R, S) {
1717
throw new TypeError('RegExp#exec called on incompatible receiver');
1818
}
1919

20-
return regexpExec.impl.call(R, S);
20+
return regexpExec.call(R, S);
2121
};
2222

packages/core-js/internals/regexp-exec.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ var nativeReplace = String.prototype.replace;
1111
var patchedExec = nativeExec;
1212

1313
var LAST_INDEX = 'lastIndex';
14-
var LENGTH = 'length';
1514

1615
var UPDATES_LAST_INDEX_WRONG = (function () {
17-
var re1 = /a/,
18-
re2 = /b*/g;
16+
var re1 = /a/;
17+
var re2 = /b*/g;
1918
nativeExec.call(re1, 'a');
2019
nativeExec.call(re2, 'a');
2120
return re1[LAST_INDEX] !== 0 || re2[LAST_INDEX] !== 0;
@@ -24,9 +23,9 @@ var UPDATES_LAST_INDEX_WRONG = (function () {
2423
// nonparticipating capturing group, copied from es5-shim's String#split patch.
2524
var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
2625

27-
var patch = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED;
26+
var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED;
2827

29-
if (patch) {
28+
if (PATCH) {
3029
patchedExec = function exec(str) {
3130
var re = this;
3231
var lastIndex, reCopy, match, i;
@@ -39,14 +38,13 @@ if (patch) {
3938
match = nativeExec.call(re, str);
4039

4140
if (UPDATES_LAST_INDEX_WRONG && match) {
42-
re[LAST_INDEX] = re.global ? match.index + match[0][LENGTH] : lastIndex;
41+
re[LAST_INDEX] = re.global ? match.index + match[0].length : lastIndex;
4342
}
44-
if (NPCG_INCLUDED && match && match[LENGTH] > 1) {
43+
if (NPCG_INCLUDED && match && match.length > 1) {
4544
// Fix browsers whose `exec` methods don't consistently return `undefined`
4645
// for NPCG, like IE8. NOTE: This doesn' work for /(.?)?/
47-
// eslint-disable-next-line no-loop-func
4846
nativeReplace.call(match[0], reCopy, function () {
49-
for (i = 1; i < arguments[LENGTH] - 2; i++) {
47+
for (i = 1; i < arguments.length - 2; i++) {
5048
if (arguments[i] === undefined) match[i] = undefined;
5149
}
5250
});
@@ -56,8 +54,4 @@ if (patch) {
5654
};
5755
}
5856

59-
module.exports = {
60-
orig: nativeExec,
61-
impl: patchedExec,
62-
patched: patch
63-
};
57+
module.exports = patchedExec;

packages/core-js/modules/es.regexp.exec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var regexpExec = require('../internals/regexp-exec');
55
require('../internals/export')({
66
target: 'RegExp',
77
proto: true,
8-
forced: regexpExec.patched
8+
forced: regexpExec !== /./.exec
99
}, {
10-
exec: regexpExec.impl
10+
exec: regexpExec
1111
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ require('../internals/fix-regexp-well-known-symbol-logic')(
4646
// Make `global` and avoid `lastIndex` issues by working with a copy
4747
var separatorCopy = new RegExp(separator.source, flags + 'g');
4848
var match, lastIndex, lastLength;
49-
while (match = regexpExec.impl.call(separatorCopy, string)) {
49+
while (match = regexpExec.call(separatorCopy, string)) {
5050
lastIndex = separatorCopy.lastIndex;
5151
if (lastIndex > lastLastIndex) {
5252
output.push(string.slice(lastLastIndex, match.index));

0 commit comments

Comments
 (0)