Skip to content

Commit 1454ab5

Browse files
authoredJul 29, 2024··
fix(es/minifier): Fix analysis of for-in/of (#9340)
**Related issue:** - Closes #9263
1 parent 77da7cf commit 1454ab5

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed
 

‎.changeset/eighty-comics-act.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_usage_analyzer: patch
3+
swc_ecma_minifier: patch
4+
---
5+
6+
fix(es/minifier): Fix analysis of for-in/of

‎crates/swc_ecma_minifier/src/compress/optimize/inline.rs

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ impl Optimizer<'_> {
337337
&& usage.declared
338338
&& may_remove
339339
&& !usage.reassigned
340+
&& !usage.declared_as_for_init
340341
&& usage.assign_count == 1
341342
&& ref_count == 1
342343
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
const k = (function () {
3+
var x = 42;
4+
for (var x in [4242]) break;
5+
return x;
6+
})();
7+
8+
9+
export { k };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
const k = function() {
3+
for(var x in [
4+
4242
5+
])break;
6+
return 42;
7+
}();
8+
export { k };

‎crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,9 @@ where
783783
self.with_child(SyntaxContext::empty(), ScopeKind::Block, |child| {
784784
let head_ctx = Ctx {
785785
in_left_of_for_loop: true,
786+
is_id_ref: true,
787+
executed_multiple_time: true,
788+
in_cond: true,
786789
..child.ctx
787790
};
788791
n.left.visit_with(&mut *child.with_ctx(head_ctx));
@@ -810,6 +813,9 @@ where
810813
self.with_child(SyntaxContext::empty(), ScopeKind::Block, |child| {
811814
let head_ctx = Ctx {
812815
in_left_of_for_loop: true,
816+
is_id_ref: true,
817+
executed_multiple_time: true,
818+
in_cond: true,
813819
..child.ctx
814820
};
815821
n.left.visit_with(&mut *child.with_ctx(head_ctx));

0 commit comments

Comments
 (0)
Please sign in to comment.