Skip to content

Commit ba1c2db

Browse files
authored
Do not handle declarations in modules without side effects as TDZ (#5322)
1 parent e004d92 commit ba1c2db

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

src/ast/nodes/Identifier.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ export default class Identifier extends NodeBase implements PatternNode {
243243
return (this.isTDZAccess = true);
244244
}
245245

246-
if (!this.variable.initReached) {
246+
// We ignore the case where the module is not yet executed because
247+
// moduleSideEffects are false.
248+
if (!this.variable.initReached && this.scope.context.module.isExecuted) {
247249
// Either a const/let TDZ violation or
248250
// var use before declaration was encountered.
249251
return (this.isTDZAccess = true);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = defineTest({
2+
description: 'properly tree-shakes nested function calls when moduleSideEffects are disabled',
3+
options: {
4+
treeshake: {
5+
moduleSideEffects: false
6+
}
7+
}
8+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const main = () => {
2+
};
3+
4+
export { main };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const doNothing = () => {};
2+
3+
const alpha = () => {
4+
doNothing();
5+
};
6+
7+
export { alpha };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { alpha } from "./alpha";
2+
3+
const main = () => {
4+
alpha();
5+
};
6+
7+
export { main };

0 commit comments

Comments
 (0)