From d5b3a73e8c711e93706481d8d48dbfcafec76142 Mon Sep 17 00:00:00 2001 From: TrickyPi <530257315@qq.com> Date: Tue, 25 Apr 2023 12:23:25 +0800 Subject: [PATCH] feat: mark some known globals or their functions as impure --- src/ast/nodes/shared/knownGlobals.ts | 10 +++---- .../samples/tree-shake-iterable/_config.js | 3 ++ .../samples/tree-shake-iterable/main.js | 29 +++++++++++++++++++ .../tree-shake-map-constructor/_config.js | 4 --- .../tree-shake-map-constructor/main.js | 14 --------- 5 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 test/function/samples/tree-shake-iterable/_config.js create mode 100644 test/function/samples/tree-shake-iterable/main.js delete mode 100644 test/function/samples/tree-shake-map-constructor/_config.js delete mode 100644 test/function/samples/tree-shake-map-constructor/main.js diff --git a/src/ast/nodes/shared/knownGlobals.ts b/src/ast/nodes/shared/knownGlobals.ts index 34b21ca9eb4..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 }; @@ -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..2448fd8d804 --- /dev/null +++ b/test/function/samples/tree-shake-iterable/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'Retain functions that consume iterable' +}; 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..31990102554 --- /dev/null +++ b/test/function/samples/tree-shake-iterable/main.js @@ -0,0 +1,29 @@ +let effects = []; + +const iterable = { + [Symbol.iterator]() { + return { + next() { + effects.push('effect'); + 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); +Object.fromEntries(iterable); + +assert.equal(effects.length, 14); diff --git a/test/function/samples/tree-shake-map-constructor/_config.js b/test/function/samples/tree-shake-map-constructor/_config.js deleted file mode 100644 index 1de38a64953..00000000000 --- a/test/function/samples/tree-shake-map-constructor/_config.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - description: - 'Retain the Map constructor is called with the new keyword if the accepted param has side effects' -}; diff --git a/test/function/samples/tree-shake-map-constructor/main.js b/test/function/samples/tree-shake-map-constructor/main.js deleted file mode 100644 index fe883187019..00000000000 --- a/test/function/samples/tree-shake-map-constructor/main.js +++ /dev/null @@ -1,14 +0,0 @@ -let effect = false; - -new Map({ - [Symbol.iterator]() { - return { - next() { - effect = true; - return { done: true }; - } - }; - } -}); - -assert.ok(effect);