Skip to content

Commit

Permalink
fix: mark some known globals or their functions as impure (#4955)
Browse files Browse the repository at this point in the history
* fix: mark Map is impure

* test: tweak test

* fix: mark some known globals or their functions as impure

* chore<devDeps>: update lint-staged version

* test: tweak test

* test: add more test cases

* test: tweak test
  • Loading branch information
TrickyPi committed Apr 30, 2023
1 parent 703e88f commit f8977a7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
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);
Object.fromEntries(iterable);

assert.equal(effects, 17);

0 comments on commit f8977a7

Please sign in to comment.