Repository: swc-project/swc (rust)

Issue:
`compress.toplevel` should default to true for modules
### Describe the bug

I found this bug while looking at playground

### Input code

_No response_

### Config

_No response_

### Playground link

_No response_

### Expected behavior

`toplevel` defaults to true.

### Actual behavior

_No response_

### Version

v1.2.125

### Additional context

_No response_

Pull request:
fix(es/minifier): Fix for modules
swc_ecma_minifier:
 - Handle exported declarations correctly.

swc:
 - Change default of `compress.toplevel` and `mangle.top_level` to true for modules. (Closes #3169)

Patch:
diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs
index 93194b8bd4..7ec7f27d08 100644
--- a/crates/swc/src/config/mod.rs
+++ b/crates/swc/src/config/mod.rs
@@ -34,7 +34,7 @@ use swc_ecma_loader::resolvers::{
     lru::CachingResolver, node::NodeModulesResolver, tsc::TsConfigResolver,
 };
 use swc_ecma_minifier::option::{
-    terser::{TerserCompressorOptions, TerserEcmaVersion},
+    terser::{TerserCompressorOptions, TerserEcmaVersion, TerserTopLevelOptions},
     MangleOptions, ManglePropertiesOptions,
 };
 #[allow(deprecated)]
@@ -274,7 +274,7 @@ impl Options {
             keep_class_names,
             base_url,
             paths,
-            minify: js_minify,
+            minify: mut js_minify,
             experimental,
             ..
         } = config.jsc;
@@ -286,6 +286,40 @@ impl Options {
         let program = parse(syntax, target, is_module)?;
         let mut transform = transform.unwrap_or_default();
 
+        if program.is_module() {
+            js_minify = js_minify.map(|c| {
+                let compress = c
+                    .compress
+                    .into_obj()
+                    .map(|mut c| {
+                        if c.toplevel.is_none() {
+                            c.toplevel = Some(TerserTopLevelOptions::Bool(true));
+                        }
+
+                        c
+                    })
+                    .map(BoolOrObject::Obj)
+                    .unwrap_or(BoolOrObject::Bool(false));
+
+                let mangle = c
+                    .mangle
+                    .into_obj()
+                    .map(|mut c| {
+                        c.top_level = true;
+
+                        c
+                    })
+                    .map(BoolOrObject::Obj)
+                    .unwrap_or(BoolOrObject::Bool(false));
+
+                JsMinifyOptions {
+                    compress,
+                    mangle,
+                    ..c
+                }
+            });
+        }
+
         let regenerator = transform.regenerator.clone();
 
         let preserve_comments = js_minify.as_ref().map(|v| v.format.comments.clone());
diff --git a/crates/swc_ecma_minifier/src/analyzer/mod.rs b/crates/swc_ecma_minifier/src/analyzer/mod.rs
index ed12d61f8f..1e521fe857 100644
--- a/crates/swc_ecma_minifier/src/analyzer/mod.rs
+++ b/crates/swc_ecma_minifier/src/analyzer/mod.rs
@@ -13,7 +13,7 @@ use swc_common::{
     SyntaxContext,
 };
 use swc_ecma_ast::*;
-use swc_ecma_utils::{ident::IdentLike, Id};
+use swc_ecma_utils::{find_ids, ident::IdentLike, Id};
 use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
 
 mod ctx;
@@ -392,6 +392,24 @@ where
         }
     }
 
+    fn visit_default_decl(&mut self, d: &DefaultDecl) {
+        d.visit_children_with(self);
+
+        match d {
+            DefaultDecl::Class(c) => {
+                if let Some(i) = &c.ident {
+                    self.data.var_or_default(i.to_id()).prevent_inline();
+                }
+            }
+            DefaultDecl::Fn(f) => {
+                if let Some(i) = &f.ident {
+                    self.data.var_or_default(i.to_id()).prevent_inline();
+                }
+            }
+            _ => {}
+        }
+    }
+
     fn visit_do_while_stmt(&mut self, n: &DoWhileStmt) {
         let ctx = Ctx {
             in_loop: true,
@@ -401,8 +419,30 @@ where
         n.visit_children_with(&mut *self.with_ctx(ctx));
     }
 
+    fn visit_export_decl(&mut self, n: &ExportDecl) {
+        n.visit_children_with(self);
+
+        match &n.decl {
+            Decl::Class(c) => {
+                self.data.var_or_default(c.ident.to_id()).prevent_inline();
+            }
+            Decl::Fn(f) => {
+                self.data.var_or_default(f.ident.to_id()).prevent_inline();
+            }
+            Decl::Var(v) => {
+                let ids = find_ids(v);
+
+                for id in ids {
+                    self.data.var_or_default(id).prevent_inline();
+                }
+            }
+            _ => {}
+        }
+    }
+
     fn visit_export_named_specifier(&mut self, n: &ExportNamedSpecifier) {
-        self.report_usage(&n.orig, false)
+        self.report_usage(&n.orig, false);
+        self.data.var_or_default(n.orig.to_id()).prevent_inline();
     }
 
     fn visit_expr(&mut self, e: &Expr) {
diff --git a/crates/swc_ecma_minifier/src/analyzer/storage/mod.rs b/crates/swc_ecma_minifier/src/analyzer/storage/mod.rs
index cabefe1b37..aa92b1af70 100644
--- a/crates/swc_ecma_minifier/src/analyzer/storage/mod.rs
+++ b/crates/swc_ecma_minifier/src/analyzer/storage/mod.rs
@@ -57,4 +57,6 @@ pub(crate) trait VarDataLike: Sized {
     fn mark_reassigned(&mut self);
 
     fn add_infects(&mut self, other: Id);
+
+    fn prevent_inline(&mut self);
 }
diff --git a/crates/swc_ecma_minifier/src/analyzer/storage/normal.rs b/crates/swc_ecma_minifier/src/analyzer/storage/normal.rs
index a9464fa326..be2830d144 100644
--- a/crates/swc_ecma_minifier/src/analyzer/storage/normal.rs
+++ b/crates/swc_ecma_minifier/src/analyzer/storage/normal.rs
@@ -228,6 +228,10 @@ impl VarDataLike for VarUsageInfo {
         self.has_property_mutation = true;
     }
 
+    fn mark_used_as_callee(&mut self) {
+        self.used_as_callee = true;
+    }
+
     fn add_accessed_property(&mut self, name: swc_atoms::JsWord) {
         self.accessed_props.insert(name);
     }
@@ -244,7 +248,7 @@ impl VarDataLike for VarUsageInfo {
         self.infects.push(other);
     }
 
-    fn mark_used_as_callee(&mut self) {
-        self.used_as_callee = true;
+    fn prevent_inline(&mut self) {
+        self.inline_prevented = true;
     }
 }


Test patch:
diff --git a/crates/swc/tests/fixture/iissue-2118/input/index.js b/crates/swc/tests/fixture/iissue-2118/input/index.js
index b3010c47b4..4799ce319d 100644
--- a/crates/swc/tests/fixture/iissue-2118/input/index.js
+++ b/crates/swc/tests/fixture/iissue-2118/input/index.js
@@ -1,3 +1,3 @@
-function test(a, b) {
+export function test(a, b) {
     a.__proto__ = b
 }
diff --git a/crates/swc/tests/fixture/iissue-2118/output/index.js b/crates/swc/tests/fixture/iissue-2118/output/index.js
index f83c952183..288f2422d9 100644
--- a/crates/swc/tests/fixture/iissue-2118/output/index.js
+++ b/crates/swc/tests/fixture/iissue-2118/output/index.js
@@ -1,1 +1,1 @@
-function test(a,b){a.__proto__=b}
+export function test(a,b){a.__proto__=b}
diff --git a/crates/swc/tests/fixture/issue-2000/input/index.js b/crates/swc/tests/fixture/issue-2000/input/index.js
index d183132949..2d8ce85027 100644
--- a/crates/swc/tests/fixture/issue-2000/input/index.js
+++ b/crates/swc/tests/fixture/issue-2000/input/index.js
@@ -1,3 +1,3 @@
-function testme(...list) {
+export function testme(...list) {
     if (/^toto.+/.test(list.join(''))) global.other(true);
 }
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issue-2000/output/index.js b/crates/swc/tests/fixture/issue-2000/output/index.js
index 937a2b67d5..301cfb34a5 100644
--- a/crates/swc/tests/fixture/issue-2000/output/index.js
+++ b/crates/swc/tests/fixture/issue-2000/output/index.js
@@ -1,1 +1,1 @@
-function testme(){for(var a=arguments.length,b=new Array(a),c=0;c<a;c++)b[c]=arguments[c];/^toto.+/.test(b.join(""))&&global.other(!0)}
+export function testme(){for(var a=arguments.length,b=new Array(a),c=0;c<a;c++)b[c]=arguments[c];/^toto.+/.test(b.join(""))&&global.other(!0)}
diff --git a/crates/swc/tests/fixture/issue-2011/reduced/output/index.js b/crates/swc/tests/fixture/issue-2011/reduced/output/index.js
index 5fd1118ee2..fc527f2670 100644
--- a/crates/swc/tests/fixture/issue-2011/reduced/output/index.js
+++ b/crates/swc/tests/fixture/issue-2011/reduced/output/index.js
@@ -1,36 +1,36 @@
-function _classCallCheck(a, b) {
+function a(a, b) {
     if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function");
 }
-function _defineProperties(a, b) {
+function b(a, b) {
     for(var c = 0; c < b.length; c++){
         var d = b[c];
         d.enumerable = d.enumerable || !1, d.configurable = !0, "value" in d && (d.writable = !0), Object.defineProperty(a, d.key, d);
     }
 }
-var ClassA = function() {
+var c = function() {
     "use strict";
-    _classCallCheck(this, ClassA);
+    a(this, c);
 };
 module.exports = (function() {
-    var a, c, d = function() {
+    var d, e, f = function() {
         "use strict";
-        var b, c, e;
-        function d() {
-            _classCallCheck(this, d);
+        var b, d, e;
+        function f() {
+            a(this, f);
         }
-        return b = d, c = [
+        return b = f, d = [
             {
                 key: "it",
                 value: function() {
-                    this.bb = new d.MyA();
+                    this.bb = new f.MyA();
                 }
             }
-        ], _defineProperties(b.prototype, c), e && _defineProperties(b, e), d;
+        ], b(b.prototype, d), e && b(b, e), f;
     }();
-    return a = d, c = ClassA, "MyA" in a ? Object.defineProperty(a, "MyA", {
-        value: c,
+    return d = f, e = c, "MyA" in d ? Object.defineProperty(d, "MyA", {
+        value: e,
         enumerable: !0,
         configurable: !0,
         writable: !0
-    }) : a.MyA = c, d;
+    }) : d.MyA = e, f;
 })();
diff --git a/crates/swc/tests/fixture/issue-2086/output/index.ts b/crates/swc/tests/fixture/issue-2086/output/index.ts
index 85cf6d2002..5c0f50e5d2 100644
--- a/crates/swc/tests/fixture/issue-2086/output/index.ts
+++ b/crates/swc/tests/fixture/issue-2086/output/index.ts
@@ -1,1 +1,1 @@
-class Test{#a;constructor(a){this.#a=a} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1000)}run(){this.#b()}}export{Test as default}
+class a{#a;constructor(b){this.#a=b} #b(){setTimeout(()=>{this.#a.textContent="TESTED"},1000)}run(){this.#b()}}export{a as default}
diff --git a/crates/swc/tests/fixture/issue-2091/case1/input/index.js b/crates/swc/tests/fixture/issue-2091/case1/input/index.js
index 81da428bcb..60a850e2a6 100644
--- a/crates/swc/tests/fixture/issue-2091/case1/input/index.js
+++ b/crates/swc/tests/fixture/issue-2091/case1/input/index.js
@@ -1,4 +1,4 @@
-function test(list) {
+export function test(list) {
     let cur = list.findIndex(p => p == 1);
     if (!~cur) {
         cur = list.findIndex(p => p !== 0);
diff --git a/crates/swc/tests/fixture/issue-2091/case1/output/index.js b/crates/swc/tests/fixture/issue-2091/case1/output/index.js
index b7c493b4ae..e58def27e9 100644
--- a/crates/swc/tests/fixture/issue-2091/case1/output/index.js
+++ b/crates/swc/tests/fixture/issue-2091/case1/output/index.js
@@ -1,1 +1,1 @@
-function test(list){var cur=list.findIndex(function(p){return 1==p});~cur||(cur=list.findIndex(function(p){return 0!==p}));for(var _i=0;_i<list.length;_i++)cur+=list[_i].value;return cur}
+export function test(list){var cur=list.findIndex(function(p){return 1==p});~cur||(cur=list.findIndex(function(p){return 0!==p}));for(var _i=0;_i<list.length;_i++)cur+=list[_i].value;return cur}
diff --git a/crates/swc/tests/fixture/issue-2717/input/index.ts b/crates/swc/tests/fixture/issue-2717/input/index.ts
index ed86b8ea08..7eff1089bc 100644
--- a/crates/swc/tests/fixture/issue-2717/input/index.ts
+++ b/crates/swc/tests/fixture/issue-2717/input/index.ts
@@ -2,9 +2,9 @@ import { takeLatest, select } from 'redux-saga/effects';
 import { CHANGE_THEME, CHANGE_THEME_CUSTOM_PALETTE, CHANGE_THEME_SWITCH_MODE } from 'core/actions/changeTheme';
 import { ReduxAction, ReduxState } from 'types';
 
-const selectThemeObject = (state: ReduxState) => state.theme;
+export const selectThemeObject = (state: ReduxState) => state.theme;
 
-function* saveTheme({ type }: ReduxAction) {
+export function* saveTheme({ type }: ReduxAction) {
     // @ts-ignore
     const theme = yield select(selectThemeObject);
     window.localStorage.setItem(
diff --git a/crates/swc/tests/fixture/issue-2717/output/index.ts b/crates/swc/tests/fixture/issue-2717/output/index.ts
index 650e7c5890..3b02595cde 100644
--- a/crates/swc/tests/fixture/issue-2717/output/index.ts
+++ b/crates/swc/tests/fixture/issue-2717/output/index.ts
@@ -1,28 +1,28 @@
 import a from "regenerator-runtime";
 import { takeLatest as b, select as c } from "redux-saga/effects";
 import { CHANGE_THEME as d, CHANGE_THEME_CUSTOM_PALETTE as e, CHANGE_THEME_SWITCH_MODE as f } from "core/actions/changeTheme";
-var _marked = a.mark(saveTheme), selectThemeObject = function(a) {
-    return a.theme;
-};
-function saveTheme(b) {
-    var d, e, g;
+var g = a.mark(saveTheme);
+export function saveTheme(b) {
+    var d, e, h;
     return a.wrap(function(a) {
         for(;;)switch(a.prev = a.next){
             case 0:
-                return d = b.type, a.next = 3, c(selectThemeObject);
+                return d = b.type, a.next = 3, c(function(a) {
+                    return a.theme;
+                });
             case 3:
                 e = a.sent, window.localStorage.setItem("theme", JSON.stringify({
                     theme: e.theme,
                     mode: e.mode,
                     palette: e.palette
-                })), d === f && (g = window.document.querySelector("body")) && (g.classList.add("light" === e.mode ? "light" : "dark"), g.classList.remove("light" === e.mode ? "dark" : "light"));
+                })), d === f && (h = window.document.querySelector("body")) && (h.classList.add("light" === e.mode ? "light" : "dark"), h.classList.remove("light" === e.mode ? "dark" : "light"));
             case 6:
             case "end":
                 return a.stop();
         }
-    }, _marked);
+    }, g);
 }
-export default function g() {
+export default function h() {
     return a.wrap(function(a) {
         for(;;)switch(a.prev = a.next){
             case 0:
@@ -35,5 +35,5 @@ export default function g() {
             case "end":
                 return a.stop();
         }
-    }, g);
+    }, h);
 };
diff --git a/crates/swc/tests/fixture/issue-2988/input/index.ts b/crates/swc/tests/fixture/issue-2988/input/index.ts
index 9bfcb88fae..11d0489870 100644
--- a/crates/swc/tests/fixture/issue-2988/input/index.ts
+++ b/crates/swc/tests/fixture/issue-2988/input/index.ts
@@ -1,4 +1,4 @@
-namespace Test {
+export namespace Test {
     export enum testEnum {
         Test = "1",
         Test2 = "2",
diff --git a/crates/swc/tests/fixture/issue-2988/output/index.ts b/crates/swc/tests/fixture/issue-2988/output/index.ts
index 39249ddc5f..31044ef4cc 100644
--- a/crates/swc/tests/fixture/issue-2988/output/index.ts
+++ b/crates/swc/tests/fixture/issue-2988/output/index.ts
@@ -1,4 +1,4 @@
-var Test;
+export var Test;
 (function(a) {
     let b;
     (function(b) {
diff --git a/crates/swc/tests/fixture/sourcemap/003/output/index.js b/crates/swc/tests/fixture/sourcemap/003/output/index.js
index 5ad2a8e805..8399f720e8 100644
--- a/crates/swc/tests/fixture/sourcemap/003/output/index.js
+++ b/crates/swc/tests/fixture/sourcemap/003/output/index.js
@@ -1,1 +1,1 @@
-import*as a from"@swc/helpers";import b from"regenerator-runtime";export default function c(a){var b=a.data;return React.createElement("div",null,b.foo)};export function getStaticProps(){return _getStaticProps.apply(this,arguments)}function _getStaticProps(){return(_getStaticProps=a.asyncToGenerator(b.mark(function a(){return b.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",{props:{data:{foo:"bar"}}});case 1:case"end":return a.stop()}},a)}))).apply(this,arguments)}
+import*as a from"@swc/helpers";import b from"regenerator-runtime";export default function c(a){var b=a.data;return React.createElement("div",null,b.foo)};export function getStaticProps(){return d.apply(this,arguments)}function d(){return(d=a.asyncToGenerator(b.mark(function a(){return b.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:return a.abrupt("return",{props:{data:{foo:"bar"}}});case 1:case"end":return a.stop()}},a)}))).apply(this,arguments)}
diff --git a/crates/swc/tests/fixture/sourcemap/003/output/index.map b/crates/swc/tests/fixture/sourcemap/003/output/index.map
index 44a2088078..323e2cc499 100644
--- a/crates/swc/tests/fixture/sourcemap/003/output/index.map
+++ b/crates/swc/tests/fixture/sourcemap/003/output/index.map
@@ -1,5 +1,5 @@
 {
-  "mappings": "kEAAA,MAAM,SAAS,QAAQ,CAACA,CAAU,CAAC,CAAQ,CAAE,CAAC,IAATC,CAAI,CAAN,CAAQ,CAANA,IAAI,CACrC,MAAM,qBAAEC,CAAG,UAAED,CAAI,CAACE,GAAG,CACzB,CAAC,CAED,MAAM,UAAgBC,cAAc,UAAdA,eAAc,gCAAdA,eAAc,UAAdA,eAAc,2BAA7B,QAAQ,IAAwB,CAAC,uFAC7B,CAAC,AACJC,KAAK,CAAE,CAAC,AACJJ,IAAI,CAAE,CAAC,AACHE,GAAG,CAAE,CAAK,IACd,CAAC,AACL,CAAC,AACL,CAAC,uCACL,CAAC",
+  "mappings": "kEAAA,MAAM,SAAS,QAAQ,CAACA,CAAU,CAAC,CAAQ,CAAE,CAAC,IAATC,CAAI,CAAN,CAAQ,CAANA,IAAI,CACrC,MAAM,qBAAEC,CAAG,UAAED,CAAI,CAACE,GAAG,CACzB,CAAC,CAED,MAAM,UAAgBC,cAAc,UAAdA,CAAc,gCAAdA,CAAc,UAAdA,CAAc,2BAA7B,QAAQ,IAAwB,CAAC,uFAC7B,CAAC,AACJC,KAAK,CAAE,CAAC,AACJJ,IAAI,CAAE,CAAC,AACHE,GAAG,CAAE,CAAK,IACd,CAAC,AACL,CAAC,AACL,CAAC,uCACL,CAAC",
   "names": [
     "StaticPage",
     "data",
diff --git a/crates/swc/tests/tsc-references/anonymousDefaultExportsAmd_es2015.2.minified.js b/crates/swc/tests/tsc-references/anonymousDefaultExportsAmd_es2015.2.minified.js
index c42467f372..915be6afe6 100644
--- a/crates/swc/tests/tsc-references/anonymousDefaultExportsAmd_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/anonymousDefaultExportsAmd_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class _class {
+}
 export { _class as default };
 export default function() {
 };
diff --git a/crates/swc/tests/tsc-references/anonymousDefaultExportsCommonjs_es2015.2.minified.js b/crates/swc/tests/tsc-references/anonymousDefaultExportsCommonjs_es2015.2.minified.js
index c42467f372..915be6afe6 100644
--- a/crates/swc/tests/tsc-references/anonymousDefaultExportsCommonjs_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/anonymousDefaultExportsCommonjs_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class _class {
+}
 export { _class as default };
 export default function() {
 };
diff --git a/crates/swc/tests/tsc-references/anonymousDefaultExportsSystem_es2015.2.minified.js b/crates/swc/tests/tsc-references/anonymousDefaultExportsSystem_es2015.2.minified.js
index c42467f372..915be6afe6 100644
--- a/crates/swc/tests/tsc-references/anonymousDefaultExportsSystem_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/anonymousDefaultExportsSystem_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class _class {
+}
 export { _class as default };
 export default function() {
 };
diff --git a/crates/swc/tests/tsc-references/anonymousDefaultExportsUmd_es2015.2.minified.js b/crates/swc/tests/tsc-references/anonymousDefaultExportsUmd_es2015.2.minified.js
index c42467f372..915be6afe6 100644
--- a/crates/swc/tests/tsc-references/anonymousDefaultExportsUmd_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/anonymousDefaultExportsUmd_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class _class {
+}
 export { _class as default };
 export default function() {
 };
diff --git a/crates/swc/tests/tsc-references/chained2_es2015.2.minified.js b/crates/swc/tests/tsc-references/chained2_es2015.2.minified.js
index 4a5a307a14..fa9aa5d60e 100644
--- a/crates/swc/tests/tsc-references/chained2_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/chained2_es2015.2.minified.js
@@ -1,5 +1,7 @@
 import * as types from "./b";
 import types from "./c";
+class A {
+}
 export { A };
 export { types as default };
 new types.A(), new types.B();
diff --git a/crates/swc/tests/tsc-references/defaultExportsCannotMerge02_es2015.2.minified.js b/crates/swc/tests/tsc-references/defaultExportsCannotMerge02_es2015.2.minified.js
index c0f59f8abb..b3982a9944 100644
--- a/crates/swc/tests/tsc-references/defaultExportsCannotMerge02_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/defaultExportsCannotMerge02_es2015.2.minified.js
@@ -1,4 +1,6 @@
 import Entity from "m1";
+class Decl {
+}
 export { Decl as default };
 Entity();
 var z = new Entity();
diff --git a/crates/swc/tests/tsc-references/defaultExportsCannotMerge03_es2015.2.minified.js b/crates/swc/tests/tsc-references/defaultExportsCannotMerge03_es2015.2.minified.js
index c0f59f8abb..b3982a9944 100644
--- a/crates/swc/tests/tsc-references/defaultExportsCannotMerge03_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/defaultExportsCannotMerge03_es2015.2.minified.js
@@ -1,4 +1,6 @@
 import Entity from "m1";
+class Decl {
+}
 export { Decl as default };
 Entity();
 var z = new Entity();
diff --git a/crates/swc/tests/tsc-references/defaultExportsGetExportedAmd_es2015.2.minified.js b/crates/swc/tests/tsc-references/defaultExportsGetExportedAmd_es2015.2.minified.js
index bc0b592f1e..006473d0da 100644
--- a/crates/swc/tests/tsc-references/defaultExportsGetExportedAmd_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/defaultExportsGetExportedAmd_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class Foo {
+}
 export { Foo as default };
 export default function foo() {
 };
diff --git a/crates/swc/tests/tsc-references/defaultExportsGetExportedCommonjs_es2015.2.minified.js b/crates/swc/tests/tsc-references/defaultExportsGetExportedCommonjs_es2015.2.minified.js
index bc0b592f1e..006473d0da 100644
--- a/crates/swc/tests/tsc-references/defaultExportsGetExportedCommonjs_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/defaultExportsGetExportedCommonjs_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class Foo {
+}
 export { Foo as default };
 export default function foo() {
 };
diff --git a/crates/swc/tests/tsc-references/defaultExportsGetExportedSystem_es2015.2.minified.js b/crates/swc/tests/tsc-references/defaultExportsGetExportedSystem_es2015.2.minified.js
index bc0b592f1e..006473d0da 100644
--- a/crates/swc/tests/tsc-references/defaultExportsGetExportedSystem_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/defaultExportsGetExportedSystem_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class Foo {
+}
 export { Foo as default };
 export default function foo() {
 };
diff --git a/crates/swc/tests/tsc-references/defaultExportsGetExportedUmd_es2015.2.minified.js b/crates/swc/tests/tsc-references/defaultExportsGetExportedUmd_es2015.2.minified.js
index bc0b592f1e..006473d0da 100644
--- a/crates/swc/tests/tsc-references/defaultExportsGetExportedUmd_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/defaultExportsGetExportedUmd_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class Foo {
+}
 export { Foo as default };
 export default function foo() {
 };
diff --git a/crates/swc/tests/tsc-references/es6modulekindWithES2015Target_es2015.2.minified.js b/crates/swc/tests/tsc-references/es6modulekindWithES2015Target_es2015.2.minified.js
index 3b5a3d1503..1dd168fa43 100644
--- a/crates/swc/tests/tsc-references/es6modulekindWithES2015Target_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/es6modulekindWithES2015Target_es2015.2.minified.js
@@ -1,1 +1,8 @@
+class A {
+    B() {
+        return 42;
+    }
+    constructor(){
+    }
+}
 export { A as default };
diff --git a/crates/swc/tests/tsc-references/es6modulekind_es2015.2.minified.js b/crates/swc/tests/tsc-references/es6modulekind_es2015.2.minified.js
index 3b5a3d1503..1dd168fa43 100644
--- a/crates/swc/tests/tsc-references/es6modulekind_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/es6modulekind_es2015.2.minified.js
@@ -1,1 +1,8 @@
+class A {
+    B() {
+        return 42;
+    }
+    constructor(){
+    }
+}
 export { A as default };
diff --git a/crates/swc/tests/tsc-references/esnextmodulekindWithES2015Target_es2015.2.minified.js b/crates/swc/tests/tsc-references/esnextmodulekindWithES2015Target_es2015.2.minified.js
index 3b5a3d1503..1dd168fa43 100644
--- a/crates/swc/tests/tsc-references/esnextmodulekindWithES2015Target_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/esnextmodulekindWithES2015Target_es2015.2.minified.js
@@ -1,1 +1,8 @@
+class A {
+    B() {
+        return 42;
+    }
+    constructor(){
+    }
+}
 export { A as default };
diff --git a/crates/swc/tests/tsc-references/esnextmodulekind_es2015.2.minified.js b/crates/swc/tests/tsc-references/esnextmodulekind_es2015.2.minified.js
index 3b5a3d1503..1dd168fa43 100644
--- a/crates/swc/tests/tsc-references/esnextmodulekind_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/esnextmodulekind_es2015.2.minified.js
@@ -1,1 +1,8 @@
+class A {
+    B() {
+        return 42;
+    }
+    constructor(){
+    }
+}
 export { A as default };
diff --git a/crates/swc/tests/tsc-references/exportDefaultClassNameWithObject_es2015.2.minified.js b/crates/swc/tests/tsc-references/exportDefaultClassNameWithObject_es2015.2.minified.js
index fa61d07c13..2ce2fd32c5 100644
--- a/crates/swc/tests/tsc-references/exportDefaultClassNameWithObject_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/exportDefaultClassNameWithObject_es2015.2.minified.js
@@ -1,1 +1,3 @@
+class Object {
+}
 export { Object as default };
diff --git a/crates/swc/tests/tsc-references/importDefaultNamedType_es2015.2.minified.js b/crates/swc/tests/tsc-references/importDefaultNamedType_es2015.2.minified.js
index 3b5a3d1503..1c229e229f 100644
--- a/crates/swc/tests/tsc-references/importDefaultNamedType_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/importDefaultNamedType_es2015.2.minified.js
@@ -1,1 +1,3 @@
+class A {
+}
 export { A as default };
diff --git a/crates/swc/tests/tsc-references/importTypeGenericTypes_es2015.2.minified.js b/crates/swc/tests/tsc-references/importTypeGenericTypes_es2015.2.minified.js
index 71e209a521..ed1493d347 100644
--- a/crates/swc/tests/tsc-references/importTypeGenericTypes_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/importTypeGenericTypes_es2015.2.minified.js
@@ -1,4 +1,8 @@
 module.exports = Point;
+class Bar {
+    constructor(input){
+    }
+}
 export { Bar };
 export const x = {
     x: 0,
diff --git a/crates/swc/tests/tsc-references/importTypeLocalMissing_es2015.2.minified.js b/crates/swc/tests/tsc-references/importTypeLocalMissing_es2015.2.minified.js
index 9c1111bfd7..8d4b7b1873 100644
--- a/crates/swc/tests/tsc-references/importTypeLocalMissing_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/importTypeLocalMissing_es2015.2.minified.js
@@ -1,4 +1,8 @@
 module.exports = Point;
+class Bar {
+    constructor(input){
+    }
+}
 export { Bar };
 export const x = {
     x: 0,
diff --git a/crates/swc/tests/tsc-references/importTypeLocal_es2015.2.minified.js b/crates/swc/tests/tsc-references/importTypeLocal_es2015.2.minified.js
index c553174013..bb4750b9cc 100644
--- a/crates/swc/tests/tsc-references/importTypeLocal_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/importTypeLocal_es2015.2.minified.js
@@ -1,4 +1,8 @@
 module.exports = Point;
+class Bar {
+    constructor(input){
+    }
+}
 export { Bar };
 export const x = {
     x: 0,
diff --git a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js
index 3841401dd7..9396e5a4bd 100644
--- a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js
@@ -1,4 +1,6 @@
 var C, K, C, K;
+class _class {
+}
 export { _class as default };
 export class A {
 }
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsClasses_es2015.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsClasses_es2015.2.minified.js
index 7205fef6d3..3de449e7f9 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsClasses_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsClasses_es2015.2.minified.js
@@ -44,7 +44,11 @@ export class F {
     constructor(a, b){
     }
 }
+class G {
+}
 export { G };
+class HH {
+}
 export { HH as H };
 export class I {
 }
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsDefaultsErr_es2015.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsDefaultsErr_es2015.2.minified.js
index fa19f9b670..17ca75e5fd 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsDefaultsErr_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsDefaultsErr_es2015.2.minified.js
@@ -5,6 +5,8 @@ class Cls {
 }
 Cls.y = "ok";
 export default Cls;
+class C {
+}
 export { C as default };
 const x;
 export { x as default }; /**
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es2015.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es2015.2.minified.js
index 4d1a1d09b3..22d17dbb59 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es2015.2.minified.js
@@ -16,8 +16,14 @@ export function e() {
 export function f(a1) {
     return a1;
 }
+function g(a2, b1) {
+    return a2.x && b1.y();
+}
 f.self = f;
 export { g };
+function hh(a3, b2) {
+    return a3.x && b2.y();
+}
 export { hh as h };
 export function i() {
 }
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es5.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es5.2.minified.js
index 0ee9cadc4b..761f09df72 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es5.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsFunctions_es5.2.minified.js
@@ -20,8 +20,14 @@ export function e() {
 export function f(a1) {
     return a1;
 }
+function g(a2, b1) {
+    return a2.x && b1.y();
+}
 f.self = f;
 export { g };
+function hh(a3, b2) {
+    return a3.x && b2.y();
+}
 export { hh as h };
 export function i() {
 }
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es2015.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es2015.2.minified.js
index 8c005bcedf..c8be6ec288 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es2015.2.minified.js
@@ -1,4 +1,7 @@
 const myTypes;
 export { myTypes };
 const testFnTypes;
+function testFn(input) {
+    return "number" == typeof input ? 2 * input : null;
+}
 export { testFn, testFnTypes };
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es5.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es5.2.minified.js
index d045cd784c..400f4ee3b8 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es5.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsImportAliasExposedWithinNamespace_es5.2.minified.js
@@ -1,3 +1,6 @@
 var myTypes, testFnTypes;
 export { myTypes };
+function testFn(input) {
+    return "number" == typeof input ? 2 * input : null;
+}
 export { testFn, testFnTypes };
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsReexportAliases_es2015.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsReexportAliases_es2015.2.minified.js
index 6cbcde147c..664c8de8b8 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsReexportAliases_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsReexportAliases_es2015.2.minified.js
@@ -1,4 +1,6 @@
 import { default as Fooa } from "./cls";
+class Foo {
+}
 export { Foo as default };
 export const x = new Fooa();
 export { default as Foob } from "./cls";
diff --git a/crates/swc/tests/tsc-references/jsDeclarationsThisTypes_es2015.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsThisTypes_es2015.2.minified.js
index dbd04af39f..c4cad3cbf9 100644
--- a/crates/swc/tests/tsc-references/jsDeclarationsThisTypes_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/jsDeclarationsThisTypes_es2015.2.minified.js
@@ -3,4 +3,8 @@ export class A {
         return this;
     }
 }
+class Base extends A {
+    verify() {
+    }
+}
 export { Base as default };
diff --git a/crates/swc/tests/tsc-references/multipleDefaultExports01_es2015.2.minified.js b/crates/swc/tests/tsc-references/multipleDefaultExports01_es2015.2.minified.js
index 2e50e8adaf..2b40b35612 100644
--- a/crates/swc/tests/tsc-references/multipleDefaultExports01_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/multipleDefaultExports01_es2015.2.minified.js
@@ -1,4 +1,6 @@
 import Entity from "./m1";
+class foo {
+}
 export { foo as default };
 export default function bar() {
 };
diff --git a/crates/swc/tests/tsc-references/multipleDefaultExports05_es2015.2.minified.js b/crates/swc/tests/tsc-references/multipleDefaultExports05_es2015.2.minified.js
index 2bb9018a14..e03a29ce68 100644
--- a/crates/swc/tests/tsc-references/multipleDefaultExports05_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/multipleDefaultExports05_es2015.2.minified.js
@@ -1,3 +1,9 @@
+class AA1 {
+}
 export { AA1 as default };
+class BB1 {
+}
 export { BB1 as default };
+class CC1 {
+}
 export { CC1 as default };
diff --git a/crates/swc/tests/tsc-references/multipleExportDefault3_es2015.2.minified.js b/crates/swc/tests/tsc-references/multipleExportDefault3_es2015.2.minified.js
index 96ea71a462..91246f116c 100644
--- a/crates/swc/tests/tsc-references/multipleExportDefault3_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/multipleExportDefault3_es2015.2.minified.js
@@ -1,4 +1,6 @@
 export default {
     uhoh: "another default"
 };
+class C {
+}
 export { C as default };
diff --git a/crates/swc/tests/tsc-references/multipleExportDefault4_es2015.2.minified.js b/crates/swc/tests/tsc-references/multipleExportDefault4_es2015.2.minified.js
index c5d9edcb0f..8a189d12c7 100644
--- a/crates/swc/tests/tsc-references/multipleExportDefault4_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/multipleExportDefault4_es2015.2.minified.js
@@ -1,3 +1,5 @@
+class C {
+}
 export { C as default };
 export default {
     uhoh: "another default"
diff --git a/crates/swc/tests/tsc-references/multipleExportDefault5_es2015.2.minified.js b/crates/swc/tests/tsc-references/multipleExportDefault5_es2015.2.minified.js
index c34a7a60f5..17f65edccc 100644
--- a/crates/swc/tests/tsc-references/multipleExportDefault5_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/multipleExportDefault5_es2015.2.minified.js
@@ -1,3 +1,5 @@
 export default function bar() {
 };
+class C {
+}
 export { C as default };