Skip to content

Commit

Permalink
fix: deoptimize right of for of stmt when assign in for of
Browse files Browse the repository at this point in the history
  • Loading branch information
wangwenlu committed Mar 13, 2023
1 parent 680912e commit a115d01
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ast/nodes/VariableDeclaration.ts
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
@@ -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
@@ -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
@@ -0,0 +1,9 @@
const list = [ { value: 1 } ];

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

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

0 comments on commit a115d01

Please sign in to comment.