Skip to content

Commit

Permalink
fix(es/minifier): Fix analysis of for-in/of (#9340)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9263
  • Loading branch information
kdy1 authored Jul 29, 2024
1 parent 77da7cf commit 1454ab5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/eighty-comics-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_usage_analyzer: patch
swc_ecma_minifier: patch
---

fix(es/minifier): Fix analysis of for-in/of
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ impl Optimizer<'_> {
&& usage.declared
&& may_remove
&& !usage.reassigned
&& !usage.declared_as_for_init
&& usage.assign_count == 1
&& ref_count == 1
{
Expand Down
9 changes: 9 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9263/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";
const k = (function () {
var x = 42;
for (var x in [4242]) break;
return x;
})();


export { k };
8 changes: 8 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/9263/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
const k = function() {
for(var x in [
4242
])break;
return 42;
}();
export { k };
6 changes: 6 additions & 0 deletions crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,9 @@ where
self.with_child(SyntaxContext::empty(), ScopeKind::Block, |child| {
let head_ctx = Ctx {
in_left_of_for_loop: true,
is_id_ref: true,
executed_multiple_time: true,
in_cond: true,
..child.ctx
};
n.left.visit_with(&mut *child.with_ctx(head_ctx));
Expand Down Expand Up @@ -810,6 +813,9 @@ where
self.with_child(SyntaxContext::empty(), ScopeKind::Block, |child| {
let head_ctx = Ctx {
in_left_of_for_loop: true,
is_id_ref: true,
executed_multiple_time: true,
in_cond: true,
..child.ctx
};
n.left.visit_with(&mut *child.with_ctx(head_ctx));
Expand Down

0 comments on commit 1454ab5

Please sign in to comment.