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

Treeshake chained assignment expressions #3919

Merged
merged 1 commit into from Jan 5, 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
20 changes: 17 additions & 3 deletions src/ast/nodes/AssignmentExpression.ts
Expand Up @@ -5,7 +5,7 @@ import {
RenderOptions
} from '../../utils/renderHelpers';
import { getSystemExportFunctionLeft } from '../../utils/systemJsRendering';
import { HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { createHasEffectsContext, HasEffectsContext, InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker';
import Variable from '../variables/Variable';
import * as NodeType from './NodeType';
Expand Down Expand Up @@ -48,13 +48,27 @@ export default class AssignmentExpression extends NodeBase {
include(context: InclusionContext, includeChildrenRecursively: IncludeChildren): void {
if (!this.deoptimized) this.applyDeoptimizations();
this.included = true;
this.left.include(context, includeChildrenRecursively);
let hasEffectsContext;
if (
includeChildrenRecursively ||
this.operator !== '=' ||
this.left.included ||
((hasEffectsContext = createHasEffectsContext()),
this.left.hasEffects(hasEffectsContext) ||
this.left.hasEffectsWhenAssignedAtPath(EMPTY_PATH, hasEffectsContext))
) {
this.left.include(context, includeChildrenRecursively);
}
this.right.include(context, includeChildrenRecursively);
}

render(code: MagicString, options: RenderOptions) {
this.left.render(code, options);
this.right.render(code, options);
if (this.left.included) {
this.left.render(code, options);
} else {
code.remove(this.start, this.right.start);
}
if (options.format === 'system') {
const exportNames =
this.left.variable && options.exportNamesByVariable.get(this.left.variable);
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/chained-assignments/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'treeshakes chained assignments'
};
8 changes: 8 additions & 0 deletions test/form/samples/chained-assignments/_expected.js
@@ -0,0 +1,8 @@
let a, d;
a = 1;
d = 2;
console.log(a, d);

let e = 'first', f = 'second';
e += f += 'third';
console.log(e);
8 changes: 8 additions & 0 deletions test/form/samples/chained-assignments/main.js
@@ -0,0 +1,8 @@
let a, b, c, d;
a = b = 1;
c = d = 2;
console.log(a, d);

let e = 'first', f = 'second';
e += f += 'third';
console.log(e);
4 changes: 1 addition & 3 deletions test/form/samples/nested-member-access/_expected.js
Expand Up @@ -6,9 +6,7 @@ const retainedResult2 = retained2.foo.bar;

const retained3 = void {};
const retainedResult3 = retained3.foo;

let retained4a;
const retained4b = retained4a = undefined;
const retained4b = undefined;
const retainedResult4 = retained4b.foo;

const retained5 = 1 + 2;
Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/nested-member-access/main.js
Expand Up @@ -32,8 +32,8 @@ const retainedResult2 = retained2.foo.bar;
const retained3 = void {};
const retainedResult3 = retained3.foo;

let retained4a;
const retained4b = retained4a = undefined;
let removed4a;
const retained4b = removed4a = undefined;
const retainedResult4 = retained4b.foo;

const retained5 = 1 + 2;
Expand Down