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

feat: mark Set, Map, WeakSet and WeakMap with array arguments as pure #5005

Merged
merged 2 commits into from May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/ast/nodes/shared/knownGlobals.ts
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
@@ -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
@@ -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
@@ -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();