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

Do not partially tree-shake unused declarations in for loops #3943

Merged
merged 1 commit into from Jan 31, 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
2 changes: 1 addition & 1 deletion src/ast/nodes/ForStatement.ts
Expand Up @@ -40,7 +40,7 @@ export default class ForStatement extends StatementBase {

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
if (this.init) this.init.include(context, includeChildrenRecursively);
if (this.init) this.init.includeAllDeclaredVariables(context, includeChildrenRecursively);
if (this.test) this.test.include(context, includeChildrenRecursively);
const { brokenFlow } = context;
if (this.update) this.update.include(context, includeChildrenRecursively);
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/VariableDeclaration.ts
Expand Up @@ -87,6 +87,7 @@ export default class VariableDeclaration extends NodeBase {

render(code: MagicString, options: RenderOptions, nodeRenderOptions: NodeRenderOptions = BLANK) {
if (
nodeRenderOptions.isNoStatement ||
areAllDeclarationsIncludedAndNotExported(this.declarations, options.exportNamesByVariable)
) {
for (const declarator of this.declarations) {
Expand Down
3 changes: 3 additions & 0 deletions src/ast/nodes/VariableDeclarator.ts
Expand Up @@ -45,6 +45,9 @@ export default class VariableDeclarator extends NodeBase {
): void {
this.included = true;
this.id.include(context, includeChildrenRecursively);
if (this.init) {
this.init.include(context, includeChildrenRecursively);
}
}

render(code: MagicString, options: RenderOptions) {
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/unused-for-loop-declaration/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'does not partially tree-shake unused declarations with side-effects in for loops'
};
9 changes: 9 additions & 0 deletions test/function/samples/unused-for-loop-declaration/main.js
@@ -0,0 +1,9 @@
let result;
let reassigned;

for (var a = (reassigned = 'reassigned'), b = 0; b < 2; b++) {
result = b;
}

assert.strictEqual(result, 1);
assert.strictEqual(reassigned, 'reassigned');