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

fix: deoptimize right of for of stmt when assign in for of #4902

Closed
Closed
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
5 changes: 5 additions & 0 deletions src/ast/nodes/VariableDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { InclusionContext } from '../ExecutionContext';
import { EMPTY_PATH } from '../utils/PathTracker';
import type Variable from '../variables/Variable';
import ArrayPattern from './ArrayPattern';
import type ForOfStatement from './ForOfStatement';
import Identifier, { type IdentifierWithVariable } from './Identifier';
import * as NodeType from './NodeType';
import ObjectPattern from './ObjectPattern';
Expand Down Expand Up @@ -49,6 +50,10 @@ export default class VariableDeclaration extends NodeBase {
for (const declarator of this.declarations) {
declarator.deoptimizePath(EMPTY_PATH);
}
if (this.parent.type === NodeType.ForOfStatement) {
const forOfStatement = this.parent as ForOfStatement;
forOfStatement.right.deoptimizePath(EMPTY_PATH);
}
}

hasEffectsOnInteractionAtPath(): boolean {
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/tree-shake-for-of-statements/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'tree-shake for-of-statements with a condition that can be evaluated'
};
9 changes: 9 additions & 0 deletions test/form/samples/tree-shake-for-of-statements/_expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const list = [ { value: 1 } ];

for (const item of list) {
item.value = 0;
}

if (list[0].value === 0) {
console.log(42);
}
9 changes: 9 additions & 0 deletions test/form/samples/tree-shake-for-of-statements/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const list = [ { value: 1 } ];

for (const item of list) {
item.value = 0;
}

if (list[0].value === 0) {
console.log(42);
}