Skip to content

Commit

Permalink
add a bugfix for the WebKit `Array.prototype.{ groupBy, groupByToMap …
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Feb 12, 2022
1 parent 94c7055 commit 3ebe589
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Added a [bug](https://bugs.webkit.org/show_bug.cgi?id=236541)fix for the WebKit `Array.prototype.{ groupBy, groupByToMap }` implementation
- `atob` / `btoa` marked as [fixed](https://github.com/nodejs/node/pull/41478) in NodeJS 17.5
- Added Electron 18.0 compat data mapping

Expand Down
4 changes: 2 additions & 2 deletions packages/core-js/internals/array-method-is-strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var fails = require('../internals/fails');
module.exports = function (METHOD_NAME, argument) {
var method = [][METHOD_NAME];
return !!method && fails(function () {
// eslint-disable-next-line no-useless-call,no-throw-literal -- required for testing
method.call(null, argument || function () { throw 1; }, 1);
// eslint-disable-next-line no-useless-call -- required for testing
method.call(null, argument || function () { return 1; }, 1);
});
};
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.array.group-by-to-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var uncurryThis = require('../internals/function-uncurry-this');
var IndexedObject = require('../internals/indexed-object');
var toObject = require('../internals/to-object');
var lengthOfArrayLike = require('../internals/length-of-array-like');
var arrayMethodIsStrict = require('../internals/array-method-is-strict');
var addToUnscopables = require('../internals/add-to-unscopables');

var Map = getBuiltIn('Map');
Expand All @@ -17,7 +18,8 @@ var push = uncurryThis([].push);

// `Array.prototype.groupByToMap` method
// https://github.com/tc39/proposal-array-grouping
$({ target: 'Array', proto: true }, {
// https://bugs.webkit.org/show_bug.cgi?id=236541
$({ target: 'Array', proto: true, forced: !arrayMethodIsStrict('groupByToMap') }, {
groupByToMap: function groupByToMap(callbackfn /* , thisArg */) {
var O = toObject(this);
var self = IndexedObject(O);
Expand Down
4 changes: 3 additions & 1 deletion packages/core-js/modules/esnext.array.group-by.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';
var $ = require('../internals/export');
var $groupBy = require('../internals/array-group-by');
var arrayMethodIsStrict = require('../internals/array-method-is-strict');
var addToUnscopables = require('../internals/add-to-unscopables');

// `Array.prototype.groupBy` method
// https://github.com/tc39/proposal-array-grouping
$({ target: 'Array', proto: true }, {
// https://bugs.webkit.org/show_bug.cgi?id=236541
$({ target: 'Array', proto: true, forced: !arrayMethodIsStrict('groupBy') }, {
groupBy: function groupBy(callbackfn /* , thisArg */) {
var thisArg = arguments.length > 1 ? arguments[1] : undefined;
return $groupBy(this, callbackfn, thisArg);
Expand Down
14 changes: 12 additions & 2 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,10 +1275,20 @@ GLOBAL.tests = {
return [].findLastIndex;
},
'esnext.array.group-by': function () {
return [].groupBy;
try {
// https://bugs.webkit.org/show_bug.cgi?id=236541
Array.prototype.groupBy.call(null, function () { /* empty */ });
return false;
} catch (error) { /* empty */ }
return Array.prototype.groupBy;
},
'esnext.array.group-by-to-map': function () {
return [].groupByToMap;
try {
// https://bugs.webkit.org/show_bug.cgi?id=236541
Array.prototype.groupByToMap.call(null, function () { /* empty */ });
return false;
} catch (error) { /* empty */ }
return Array.prototype.groupByToMap;
},
'esnext.array.is-template-object': function () {
return Array.isTemplateObject;
Expand Down
4 changes: 2 additions & 2 deletions tests/pure/esnext.array.group-by-to-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ QUnit.test('Array#groupByToMap', assert => {
);
assert.deepEqual(from(groupByToMap(Array(3), it => it)), [[undefined, [undefined, undefined, undefined]]], '#3');
if (STRICT) {
assert.throws(() => groupByToMap(null, () => { /* empty */ }), TypeError);
assert.throws(() => groupByToMap(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => groupByToMap(null, () => { /* empty */ }), TypeError, 'null this -> TypeError');
assert.throws(() => groupByToMap(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError');
}
array = [1];
// eslint-disable-next-line object-shorthand -- constructor
Expand Down
4 changes: 2 additions & 2 deletions tests/pure/esnext.array.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ QUnit.test('Array#groupBy', assert => {
);
assert.deepEqual(groupBy(Array(3), it => it), { undefined: [undefined, undefined, undefined] }, '#3');
if (STRICT) {
assert.throws(() => groupBy(null, () => { /* empty */ }), TypeError);
assert.throws(() => groupBy(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => groupBy(null, () => { /* empty */ }), TypeError, 'null this -> TypeError');
assert.throws(() => groupBy(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError');
}
array = [1];
// eslint-disable-next-line object-shorthand -- constructor
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/esnext.array.group-by-to-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ QUnit.test('Array#groupByToMap', assert => {
);
assert.deepEqual(from(Array(3).groupByToMap(it => it)), [[undefined, [undefined, undefined, undefined]]], '#3');
if (STRICT) {
assert.throws(() => groupByToMap.call(null, () => { /* empty */ }), TypeError);
assert.throws(() => groupByToMap.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => groupByToMap.call(null, () => { /* empty */ }), TypeError, 'null this -> TypeError');
assert.throws(() => groupByToMap.call(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError');
}
array = [1];
// eslint-disable-next-line object-shorthand -- constructor
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/esnext.array.group-by.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ QUnit.test('Array#groupBy', assert => {
);
assert.deepEqual(Array(3).groupBy(it => it), { undefined: [undefined, undefined, undefined] }, '#3');
if (STRICT) {
assert.throws(() => groupBy.call(null, () => { /* empty */ }), TypeError);
assert.throws(() => groupBy.call(undefined, () => { /* empty */ }), TypeError);
assert.throws(() => groupBy.call(null, () => { /* empty */ }), TypeError, 'null this -> TypeError');
assert.throws(() => groupBy.call(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError');
}
array = [1];
// eslint-disable-next-line object-shorthand -- constructor
Expand Down

0 comments on commit 3ebe589

Please sign in to comment.