Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mark some known globals or their functions as impure #4955

Merged
merged 10 commits into from Apr 30, 2023
12 changes: 6 additions & 6 deletions src/ast/nodes/shared/knownGlobals.ts
Expand Up @@ -94,7 +94,7 @@ const PC: GlobalDescription = {
const ARRAY_TYPE: GlobalDescription = {
__proto__: null,
[ValueProperties]: PURE,
from: PF,
from: O,
of: PF,
prototype: O
};
Expand Down Expand Up @@ -164,7 +164,7 @@ const knownGlobals: GlobalDescription = {
isNaN: PF,
isPrototypeOf: O,
JSON: O,
Map: PC,
Map: C,
Math: {
__proto__: null,
[ValueProperties]: IMPURE,
Expand Down Expand Up @@ -237,7 +237,7 @@ const knownGlobals: GlobalDescription = {
isFrozen: PF,
isSealed: PF,
keys: PF,
fromEntries: PF,
fromEntries: O,
entries: PF,
prototype: O
},
Expand All @@ -260,7 +260,7 @@ const knownGlobals: GlobalDescription = {
ReferenceError: PC,
Reflect: O,
RegExp: PC,
Set: PC,
Set: C,
SharedArrayBuffer: C,
String: {
__proto__: null,
Expand Down Expand Up @@ -300,8 +300,8 @@ const knownGlobals: GlobalDescription = {
unescape: PF,
URIError: PC,
valueOf: O,
WeakMap: PC,
WeakSet: PC,
WeakMap: C,
WeakSet: C,

// Additional globals shared by Node and Browser that are not strictly part of the language
clearInterval: C,
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/tree-shake-iterable/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'retain functions that accept a iterable that has side effects on iteration'
};
32 changes: 32 additions & 0 deletions test/function/samples/tree-shake-iterable/main.js
@@ -0,0 +1,32 @@
let effects = 0;

const iterable = {
[Symbol.iterator]() {
return {
next() {
effects++;
return { done: true };
}
};
}
};

new Map(iterable);
new Set(iterable);
new WeakMap(iterable);
new WeakSet(iterable);
Array.from(iterable);
BigInt64Array.from(iterable);
BigUint64Array.from(iterable);
Float32Array.from(iterable);
Float64Array.from(iterable);
Int16Array.from(iterable);
Int32Array.from(iterable);
Int8Array.from(iterable);
Uint16Array.from(iterable);
Uint32Array.from(iterable);
Uint8Array.from(iterable);
Uint8ClampedArray.from(iterable);
TrickyPi marked this conversation as resolved.
Show resolved Hide resolved
Object.fromEntries(iterable);

assert.equal(effects, 17);