Skip to content

Commit

Permalink
feat: mark Set, Map, WeakSet and WeakMap with array arguments as pure (
Browse files Browse the repository at this point in the history
…#5005)

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
TrickyPi and lukastaegert authored May 22, 2023
1 parent 6d78dea commit 5ea3655
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/ast/nodes/shared/knownGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
UNKNOWN_NON_ACCESSOR_PATH,
UNKNOWN_PATH
} from '../../utils/PathTracker';
import ArrayExpression from '../ArrayExpression';
import type { LiteralValueOrUnknown } from './Expression';
import { UnknownTruthyValue } from './Expression';

Expand Down Expand Up @@ -43,6 +44,14 @@ const IMPURE: ValueDescription = {
hasEffectsWhenCalled: returnTrue
};

const PURE_WITH_ARRAY: ValueDescription = {
deoptimizeArgumentsOnCall: doNothing,
getLiteralValue: getTruthyLiteralValue,
hasEffectsWhenCalled({ args }) {
return args.length > 1 && !(args[1] instanceof ArrayExpression);
}
};

// We use shortened variables to reduce file size here
/* OBJECT */
const O: GlobalDescription = {
Expand Down Expand Up @@ -91,6 +100,12 @@ const PC: GlobalDescription = {
prototype: O
};

const PC_WITH_ARRAY = {
__proto__: null,
[ValueProperties]: PURE_WITH_ARRAY,
prototype: O
};

const ARRAY_TYPE: GlobalDescription = {
__proto__: null,
[ValueProperties]: PURE,
Expand Down Expand Up @@ -164,7 +179,7 @@ const knownGlobals: GlobalDescription = {
isNaN: PF,
isPrototypeOf: O,
JSON: O,
Map: C,
Map: PC_WITH_ARRAY,
Math: {
__proto__: null,
[ValueProperties]: IMPURE,
Expand Down Expand Up @@ -260,7 +275,7 @@ const knownGlobals: GlobalDescription = {
ReferenceError: PC,
Reflect: O,
RegExp: PC,
Set: C,
Set: PC_WITH_ARRAY,
SharedArrayBuffer: C,
String: {
__proto__: null,
Expand Down Expand Up @@ -300,8 +315,8 @@ const knownGlobals: GlobalDescription = {
unescape: PF,
URIError: PC,
valueOf: O,
WeakMap: C,
WeakSet: C,
WeakMap: PC_WITH_ARRAY,
WeakSet: PC_WITH_ARRAY,

// 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/form/samples/tree-shake-global-variables/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = defineTest({
description: 'retain Set, Map, WeakSet and WeakMap which called with array arguments'
});
7 changes: 7 additions & 0 deletions test/form/samples/tree-shake-global-variables/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
new Set([f()]); //retained

new WeakSet([f()]); //retained

new Map([['a', f()]]); //retained

new WeakMap([[f(), 'a']]); //retained
23 changes: 23 additions & 0 deletions test/form/samples/tree-shake-global-variables/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
new Set([f()]); //retained

new Set(['a']);

new Set();

new WeakSet([f()]); //retained

new WeakSet([{}]);

new WeakSet();

new Map([['a', f()]]); //retained

new Map([['a', 'a']]);

new Map();

new WeakMap([[f(), 'a']]); //retained

new WeakMap([[{}, 'a']]);

new WeakMap();

0 comments on commit 5ea3655

Please sign in to comment.