From f8977a76d06dd17f651a161ca1ec53184f6e51e9 Mon Sep 17 00:00:00 2001 From: XiaoPi <530257315@qq.com> Date: Sun, 30 Apr 2023 12:17:56 +0800 Subject: [PATCH] fix: mark some known globals or their functions as impure (#4955) * fix: mark Map is impure * test: tweak test * fix: mark some known globals or their functions as impure * chore: update lint-staged version * test: tweak test * test: add more test cases * test: tweak test --- src/ast/nodes/shared/knownGlobals.ts | 12 +++---- .../samples/tree-shake-iterable/_config.js | 3 ++ .../samples/tree-shake-iterable/main.js | 32 +++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 test/function/samples/tree-shake-iterable/_config.js create mode 100644 test/function/samples/tree-shake-iterable/main.js diff --git a/src/ast/nodes/shared/knownGlobals.ts b/src/ast/nodes/shared/knownGlobals.ts index 21d4e751af2..a6c7a6eaa10 100644 --- a/src/ast/nodes/shared/knownGlobals.ts +++ b/src/ast/nodes/shared/knownGlobals.ts @@ -94,7 +94,7 @@ const PC: GlobalDescription = { const ARRAY_TYPE: GlobalDescription = { __proto__: null, [ValueProperties]: PURE, - from: PF, + from: O, of: PF, prototype: O }; @@ -164,7 +164,7 @@ const knownGlobals: GlobalDescription = { isNaN: PF, isPrototypeOf: O, JSON: O, - Map: PC, + Map: C, Math: { __proto__: null, [ValueProperties]: IMPURE, @@ -237,7 +237,7 @@ const knownGlobals: GlobalDescription = { isFrozen: PF, isSealed: PF, keys: PF, - fromEntries: PF, + fromEntries: O, entries: PF, prototype: O }, @@ -260,7 +260,7 @@ const knownGlobals: GlobalDescription = { ReferenceError: PC, Reflect: O, RegExp: PC, - Set: PC, + Set: C, SharedArrayBuffer: C, String: { __proto__: null, @@ -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, diff --git a/test/function/samples/tree-shake-iterable/_config.js b/test/function/samples/tree-shake-iterable/_config.js new file mode 100644 index 00000000000..19d6004e133 --- /dev/null +++ b/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' +}; diff --git a/test/function/samples/tree-shake-iterable/main.js b/test/function/samples/tree-shake-iterable/main.js new file mode 100644 index 00000000000..6ac8d2d2759 --- /dev/null +++ b/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);