From 5c7c8e073634ff6347194fa0e3fae8c83b64718c Mon Sep 17 00:00:00 2001 From: magic-akari Date: Tue, 7 Mar 2023 12:10:06 +0800 Subject: [PATCH] fix(es/transforms): Handle member object in missing else branch (#7027) **Related issue:** - Closes https://github.com/swc-project/swc/issues/7025. --- .../src/const_modules.rs | 16 +++++++++------- .../tests/const_modules.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/crates/swc_ecma_transforms_optimization/src/const_modules.rs b/crates/swc_ecma_transforms_optimization/src/const_modules.rs index a47500cacfc4..763cd5ebb247 100644 --- a/crates/swc_ecma_transforms_optimization/src/const_modules.rs +++ b/crates/swc_ecma_transforms_optimization/src/const_modules.rs @@ -161,11 +161,11 @@ impl VisitMut for ConstModules { } } Expr::Member(MemberExpr { obj, prop, .. }) if obj.is_ident() => { - let member_obj = obj.as_ident().unwrap(); - - if self.scope.namespace.contains(&member_obj.to_id()) { - let module_name = &member_obj.sym; - + if let Some(module_name) = obj + .as_ident() + .filter(|member_obj| self.scope.namespace.contains(&member_obj.to_id())) + .map(|member_obj| &member_obj.sym) + { let imported_name = match prop { MemberProp::Ident(ref id) => &id.sym, MemberProp::Computed(ref p) => match &*p.expr { @@ -188,10 +188,12 @@ impl VisitMut for ConstModules { }); *n = (**value).clone(); + } else { + n.visit_mut_children_with(self); } } - e => { - e.visit_mut_children_with(self); + _ => { + n.visit_mut_children_with(self); } }; } diff --git a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs index 0b6d12763b7c..7951ae0131e9 100644 --- a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs +++ b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs @@ -116,3 +116,21 @@ console.log(foo["bar"]) console.log(true); "# ); + +test!( + ::swc_ecma_parser::Syntax::default(), + |tester| tr( + tester, + &[("testModule", &[("testMap", "{ 'var': 'value' }")])] + ), + issue_7025, + r#" + import { testMap } from "testModule"; + testMap['var']; +"#, + r#" + ({ + 'var': 'value' + })['var']; + "# +);