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
Merged
127 changes: 55 additions & 72 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -115,7 +115,7 @@
"husky": "^8.0.3",
"inquirer": "^9.1.4",
"is-reference": "^3.0.1",
"lint-staged": "^13.1.2",
"lint-staged": "^13.2.1",
"locate-character": "^2.0.5",
"magic-string": "^0.30.0",
"mocha": "^10.2.0",
Expand Down
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 consume iterable'
};
29 changes: 29 additions & 0 deletions test/function/samples/tree-shake-iterable/main.js
@@ -0,0 +1,29 @@
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);
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, 14);