Skip to content

Commit bfefc79

Browse files
authored
fix: include left-side effect of optional chaining in the end of hasEffectsAsChainElement (#5642)
include left-side effect of optional chaining in the end of hasEffectsAsChainElement
1 parent fc240ed commit bfefc79

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

src/ast/nodes/CallExpression.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ export default class CallExpression
106106
}
107107
return (
108108
!this.annotationPure &&
109-
this.callee.hasEffectsOnInteractionAtPath(EMPTY_PATH, this.interaction, context)
109+
(calleeHasEffects ||
110+
this.callee.hasEffectsOnInteractionAtPath(EMPTY_PATH, this.interaction, context))
110111
);
111112
}
112113

src/ast/nodes/MemberExpression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export default class MemberExpression
296296
}
297297
// We only apply deoptimizations lazily once we know we are not skipping
298298
if (!this.deoptimized) this.applyDeoptimizations();
299-
return this.property.hasEffects(context) || this.hasAccessEffect(context);
299+
return objectHasEffects || this.property.hasEffects(context) || this.hasAccessEffect(context);
300300
}
301301

302302
hasEffectsAsAssignmentTarget(context: HasEffectsContext, checkAccess: boolean): boolean {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = defineTest({
2+
description: 'preserve optional chaining with smallest treeshake',
3+
options: {
4+
treeshake: 'smallest'
5+
}
6+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
while (global[++i]?.key) {} //retained
2+
while (global(++i)?.key) {} //retained
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
while (global[++i]?.key) {} //retained
2+
while (global(++i)?.key) {} //retained

0 commit comments

Comments
 (0)