Skip to content

Commit

Permalink
detect and fix early Set methods implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 27, 2023
1 parent ce0913b commit 09c96b2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- `DataView.prototype.setUint8Clamped`
- Used strict mode in some missed cases, [#1269](https://github.com/zloirock/core-js/issues/1269)
- Fixed [a Chromium 117 bug](https://bugs.chromium.org/p/v8/issues/detail?id=14222) in `value` argument of `URLSearchParams.prototype.{ has, delete }`
- Fixed early WebKit `Set` methods implementation by the actual spec
- Fixed incorrect `Symbol.{ dispose, asyncDispose }` descriptors from [NodeJS 20.4](https://github.com/nodejs/node/issues/48699) / transpilers helpers / userland code
- Fixed forced polyfilling of some iterator helpers that should return wrapped iterator in the pure version
- Fixed and exposed [`AsyncIteratorPrototype` `core-js/configurator` option](https://github.com/zloirock/core-js#asynciterator-helpers), [#1268](https://github.com/zloirock/core-js/issues/1268)
Expand Down
14 changes: 0 additions & 14 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2200,8 +2200,6 @@ export const data = {
'esnext.set.delete-all': {
},
'esnext.set.difference.v2': {
bun: '0.6.0',
safari: '17.0',
},
// TODO: Remove from `core-js@4`
'esnext.set.difference': {
Expand All @@ -2215,29 +2213,21 @@ export const data = {
'esnext.set.from': {
},
'esnext.set.intersection.v2': {
bun: '0.5.7',
safari: '17.0',
},
// TODO: Remove from `core-js@4`
'esnext.set.intersection': {
},
'esnext.set.is-disjoint-from.v2': {
bun: '0.5.7',
safari: '17.0',
},
// TODO: Remove from `core-js@4`
'esnext.set.is-disjoint-from': {
},
'esnext.set.is-subset-of.v2': {
bun: '0.5.7',
safari: '17.0',
},
// TODO: Remove from `core-js@4`
'esnext.set.is-subset-of': {
},
'esnext.set.is-superset-of.v2': {
bun: '0.5.7',
safari: '17.0',
},
// TODO: Remove from `core-js@4`
'esnext.set.is-superset-of': {
Expand All @@ -2253,15 +2243,11 @@ export const data = {
'esnext.set.some': {
},
'esnext.set.symmetric-difference.v2': {
bun: '0.5.7',
safari: '17.0',
},
// TODO: Remove from `core-js@4`
'esnext.set.symmetric-difference': {
},
'esnext.set.union.v2': {
bun: '0.5.7',
safari: '17.0',
},
// TODO: Remove from `core-js@4`
'esnext.set.union': {
Expand Down
17 changes: 12 additions & 5 deletions packages/core-js/internals/set-method-accept-set-like.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
var getBuiltIn = require('../internals/get-built-in');

var createEmptySetLike = function () {
var createSetLike = function (size) {
return {
size: 0,
size: size,
has: function () {
return false;
},
Expand All @@ -18,10 +18,17 @@ var createEmptySetLike = function () {
};

module.exports = function (name) {
var Set = getBuiltIn('Set');
try {
var Set = getBuiltIn('Set');
new Set()[name](createEmptySetLike());
return true;
new Set()[name](createSetLike(0));
try {
// late spec change, early Safari implementation does not pass it
// https://github.com/tc39/proposal-set-methods/pull/88
new Set()[name](createSetLike(-1));
return false;
} catch (error2) {
return true;
}
} catch (error) {
return false;
}
Expand Down
62 changes: 42 additions & 20 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,40 @@ function createStringTrimMethodTest(METHOD_NAME) {
};
}

function createSetLike(size) {
return {
size: size,
has: function () {
return false;
},
keys: function () {
return {
next: function () {
return { done: true };
}
};
}
};
}

function createSetMethodTest(METHOD_NAME) {
return function () {
try {
new Set()[METHOD_NAME](createSetLike(0));
try {
// late spec change, early Safari implementation does not pass it
// https://github.com/tc39/proposal-set-methods/pull/88
new Set()[METHOD_NAME](createSetLike(-1));
return false;
} catch (error2) {
return true;
}
} catch (error) {
return false;
}
};
}

function NATIVE_RAW_JSON() {
var unsafeInt = '9007199254740993';
var raw = JSON.rawJSON(unsafeInt);
Expand Down Expand Up @@ -1731,9 +1765,7 @@ GLOBAL.tests = {
'esnext.set.delete-all': function () {
return Set.prototype.deleteAll;
},
'esnext.set.difference.v2': function () {
return Set.prototype.difference;
},
'esnext.set.difference.v2': createSetMethodTest('difference'),
'esnext.set.every': function () {
return Set.prototype.every;
},
Expand All @@ -1746,18 +1778,12 @@ GLOBAL.tests = {
'esnext.set.from': function () {
return Set.from;
},
'esnext.set.intersection.v2': function () {
'esnext.set.intersection.v2': [createSetMethodTest('intersection'), function () {
return Array.from(new Set([1, 2, 3]).intersection(new Set([3, 2]))) == '3,2';
},
'esnext.set.is-disjoint-from.v2': function () {
return Set.prototype.isDisjointFrom;
},
'esnext.set.is-subset-of.v2': function () {
return Set.prototype.isSubsetOf;
},
'esnext.set.is-superset-of.v2': function () {
return Set.prototype.isSupersetOf;
},
}],
'esnext.set.is-disjoint-from.v2': createSetMethodTest('isDisjointFrom'),
'esnext.set.is-subset-of.v2': createSetMethodTest('isSubsetOf'),
'esnext.set.is-superset-of.v2': createSetMethodTest('isSupersetOf'),
'esnext.set.join': function () {
return Set.prototype.join;
},
Expand All @@ -1773,12 +1799,8 @@ GLOBAL.tests = {
'esnext.set.some': function () {
return Set.prototype.some;
},
'esnext.set.symmetric-difference.v2': function () {
return Set.prototype.symmetricDifference;
},
'esnext.set.union.v2': function () {
return Set.prototype.union;
},
'esnext.set.symmetric-difference.v2': createSetMethodTest('symmetricDifference'),
'esnext.set.union.v2': createSetMethodTest('union'),
'esnext.string.code-points': function () {
return String.prototype.codePoints;
},
Expand Down

0 comments on commit 09c96b2

Please sign in to comment.