Repository: swc-project/swc (rust)

Issue:
Wrong output for "jsc.transform.constModules" configuration with object const
### Describe the bug

After update from **v1.3.37** to **v1.3.38** we noticed different output for [**jsc.transform.constModules**](https://swc.rs/docs/configuration/compilation#jsctransformconstmodules) for modules that contain non-primitive data.



### Input code

```typescript
import { testMap } from "testModule";
testMap['var'];
```


### Config

```json
{
  "jsc": {
    "target": "es2021",
    "parser": {
      "syntax": "typescript"
    },
    "transform": {
      "constModules": {
        "globals": {
          "testModule": {
            "testMap": "{ 'var': 'value' }"
          }
        }
      }
    }
  }
}
```


### Playground link

https://play.swc.rs/?version=1.3.38&code=H4sIAAAAAAAAA8vMLcgvKlGoVihJLS7xTSxQqFVIK8rPVVAC8%2FNTSnNSlay5oJLR6mWJReqx1gD907e3NQAAAA%3D%3D&config=H4sIAAAAAAAAAz1MOw6DMAy9CvKShYEy5g4cwg0GtUqTyDYVKMrd61Rq3%2FK%2BehWeEsBXKMhC3JVcSfEED3oVksCPotBGUMYkW%2BZX34ScRJe8HpGk%2Bz3mO8avVPo1f4fF3urg3sjOd4oHuaFBM9gx8k5qC5J5mm%2BWfgDY4jojlQAAAA%3D%3D

### Expected behavior

Output has to be like in v1.3.37 and earlier:
```
({
    'var': 'value'
})['var'];
```

### Actual behavior

```
testMap['var'];
```

### Version

v1.3.38

### Additional context

_No response_

Pull request description:
fix(es/transforms): handle member obj in missing else branch
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**Description:**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

**BREAKING CHANGE:**

<!--
If this PR introduces a breaking change, it must contain a notice for it to be included in the CHANGELOG. Add description or remove entirely if not breaking.

You may need to update `MIGRATION.md` for the breaking changes.
-->

**Related issue (if exists):**
- fix #7025

Patch:
diff --git a/crates/swc_ecma_transforms_optimization/src/const_modules.rs b/crates/swc_ecma_transforms_optimization/src/const_modules.rs
index a47500cacf..763cd5ebb2 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);
             }
         };
     }


Test patch:
diff --git a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs
index 0b6d12763b..7951ae0131 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'];
+    "#
+);