Repository: swc-project/swc (rust)

Issue:
const_modules not handling object shorthand references properly
### Describe the bug

I have previously encountered a similar issue with modules transpilation and was able to resolve it using similar handling methods.

### Input code

```typescript
import { bar } from 'foo';
console.log({ bar });
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "ecmascript"
    },
    "transform": {
      "constModules": {
        "globals": {
          "foo": {
            "bar": "true"
          }
        }
      }
    },
    "target": "es5",
    "loose": false
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}
```


### Playground link

https://play.swc.rs/?version=1.3.38&code=H4sIAAAAAAAAA8vMLcgvKlGoVkhKLFKoVUgrys9VUE%2FLz1e35krOzyvOz0nVy8lP14DKa1oDAAwm73IwAAAA&config=H4sIAAAAAAAAAy2NQQ7EIAhF78La7czCO%2FQQ1MHGiYoBm4xpevcRUzZAeP9xwVcD%2BAsaipLYpKN2%2FIEHCgU1SGodbgddsGpkKcYErto3%2FpyZ1PYj8455jZHZ2o5TNkMnwT1r5lEO6qbVFzjIzErg40zRvJblsmAfjRb1tq8l1RTHwzlIuj2gme8%2FnocnU74AAAA%3D

### Expected behavior

```JavaScript
console.log({
    bar: true
});
```

### Actual behavior

```JavaScript
console.log({
    bar: bar
});
```

### Version

1.3.38

### Additional context

_No response_

Pull request:
fix(es/transforms): Handle object shorthand prop in `const_modules`
<!-- 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 #7021 

Patch:
diff --git a/crates/swc_ecma_transforms_optimization/src/const_modules.rs b/crates/swc_ecma_transforms_optimization/src/const_modules.rs
index 763cd5ebb2..5137c055b8 100644
--- a/crates/swc_ecma_transforms_optimization/src/const_modules.rs
+++ b/crates/swc_ecma_transforms_optimization/src/const_modules.rs
@@ -197,4 +197,26 @@ impl VisitMut for ConstModules {
             }
         };
     }
+
+    fn visit_mut_prop(&mut self, n: &mut Prop) {
+        match n {
+            Prop::Shorthand(id) => {
+                if let Some(value) = self.scope.imported.get(&id.sym) {
+                    *n = Prop::KeyValue(KeyValueProp {
+                        key: id.take().into(),
+                        value: Box::new((**value).clone()),
+                    });
+                    return;
+                }
+
+                if let Some(..) = self.scope.namespace.get(&id.to_id()) {
+                    panic!(
+                        "The const_module namespace `{}` cannot be used without member accessor",
+                        id.sym
+                    )
+                }
+            }
+            _ => 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 7951ae0131..50d4beb350 100644
--- a/crates/swc_ecma_transforms_optimization/tests/const_modules.rs
+++ b/crates/swc_ecma_transforms_optimization/tests/const_modules.rs
@@ -134,3 +134,16 @@ test!(
     })['var'];
     "#
 );
+
+test!(
+    ::swc_ecma_parser::Syntax::default(),
+    |tester| tr(tester, &[("foo", &[("bar", "true")])]),
+    use_as_object_prop_shorthand,
+    r#"
+import { bar } from 'foo';
+console.log({ bar });
+"#,
+    r#"
+console.log({ bar: true });
+"#
+);