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

Respect propertyReadSideEffects in spread elements #4119

Merged
merged 1 commit into from Jun 3, 2021
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
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() {} };
})();