Skip to content

Commit

Permalink
Respect propertyReadSideEffects in spread elements (#4119)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 3, 2021
1 parent b3d130b commit 5c05997
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/ast/nodes/SpreadElement.ts
@@ -1,3 +1,4 @@
import { NormalizedTreeshakingOptions } from '../../rollup/types';
import { HasEffectsContext } from '../ExecutionContext';
import { NodeEvent } from '../NodeEvents';
import { ObjectPath, PathTracker, UNKNOWN_PATH, UnknownKey } from '../utils/PathTracker';
Expand Down Expand Up @@ -27,9 +28,14 @@ export default class SpreadElement extends NodeBase {
}

hasEffects(context: HasEffectsContext): boolean {
if (!this.deoptimized) this.applyDeoptimizations();
const { propertyReadSideEffects } = this.context.options
.treeshake as NormalizedTreeshakingOptions;
return (
this.argument.hasEffects(context) ||
this.argument.hasEffectsWhenAccessedAtPath(UNKNOWN_PATH, context)
(propertyReadSideEffects &&
(propertyReadSideEffects === 'always' ||
this.argument.hasEffectsWhenAccessedAtPath(UNKNOWN_PATH, context)))
);
}

Expand Down
24 changes: 21 additions & 3 deletions test/form/samples/ignore-property-access-side-effects/main.js
@@ -1,6 +1,6 @@
const getter = {
get foo () {
console.log( 'effect' );
get foo() {
console.log('effect');
}
};
const foo1 = getter.foo;
Expand All @@ -13,4 +13,22 @@ function accessArg(arg) {
}
accessArg(null);

const foo4 = globalThis.globalThis.unknown.unknownProperty;
const foo4 = globalThis.unknown.unknownProperty;

const foo5 = {
...{
get prop() {
console.log('effect');
}
}
};

const foo6 = (async function () {
await {
get then() {
console.log('effect');
return () => {};
}
};
return { then() {} };
})();
18 changes: 18 additions & 0 deletions test/form/samples/keep-property-access-side-effects/_expected.js
Expand Up @@ -14,3 +14,21 @@ function accessArg(arg) {
accessArg(null);

globalThis.unknown.unknownProperty;

({
...{
get prop() {
console.log('effect');
}
}
});

((async function () {
await {
get then() {
console.log('effect');
return () => {};
}
};
return { then() {} };
}))();
18 changes: 18 additions & 0 deletions test/form/samples/keep-property-access-side-effects/main.js
Expand Up @@ -14,3 +14,21 @@ function accessArg(arg) {
accessArg(null);

const foo4 = globalThis.unknown.unknownProperty;

const foo5 = {
...{
get prop() {
console.log('effect');
}
}
};

const foo6 = (async function () {
await {
get then() {
console.log('effect');
return () => {};
}
};
return { then() {} };
})();

0 comments on commit 5c05997

Please sign in to comment.