Skip to content

Commit

Permalink
Treeshake chained assignment expressions (#3919)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 5, 2021
1 parent 1378cae commit 79cd3b4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
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

0 comments on commit 79cd3b4

Please sign in to comment.