From 7d6269e3b826524e340edf274a5e42b2a0ea058a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 28 Aug 2024 13:32:45 +0900 Subject: [PATCH] fix(es/minifier): Track if a var is used with `in` (#9508) **Related issue:** - Closes https://github.com/swc-project/swc/issues/9499 --- .changeset/sour-pumpkins-rescue.md | 6 ++++++ crates/swc_ecma_minifier/tests/exec.rs | 13 +++++++++++++ crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs | 1 + 3 files changed, 20 insertions(+) create mode 100644 .changeset/sour-pumpkins-rescue.md diff --git a/.changeset/sour-pumpkins-rescue.md b/.changeset/sour-pumpkins-rescue.md new file mode 100644 index 00000000000..f576fd24dc2 --- /dev/null +++ b/.changeset/sour-pumpkins-rescue.md @@ -0,0 +1,6 @@ +--- +swc_ecma_usage_analyzer: patch +swc_core: patch +--- + +fix(es/minifier): Track if a var is used with `in` diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 1ec5f9cc016..4d4acb59c1e 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -11347,6 +11347,19 @@ fn issue_9184_2() { ); } +#[test] +fn issue_9499() { + run_default_exec_test( + " + const o = {'a': 1, 'b': 2}; + function fn() { + return 'a' in o; + } + console.log(fn()); +", + ) +} + #[test] fn issue_9356() { run_default_exec_test("console.log((function ({ } = 42) { }).length)"); diff --git a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs index c901df71a48..785226e3b81 100644 --- a/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs +++ b/crates/swc_ecma_usage_analyzer/src/analyzer/mod.rs @@ -340,6 +340,7 @@ where if e.op == op!("in") { for_each_id_ref_in_expr(&e.right, &mut |obj| { let var = self.data.var_or_default(obj.to_id()); + var.mark_used_as_ref(); match &*e.left { Expr::Lit(Lit::Str(prop)) if prop.value.parse::().is_err() => {