Repository: swc-project/swc (rust)

Issue:
A better transform for "misc import thorw"
<!--
If you are using swc at work, please considering adding your company to https://swc.rs/users/
If then, your issue will be fixed more quickly.
-->

**Describe the feature**
Now these code
``` js
import { bar } from 'foo';
bar = jest.fn()
```
will be transformed to
``` js
bar = (jest.fn(), (function() { throw new Error('"' + 'bar' + '" is read-only.') })())
```
If the assignee `bar` becomes `_foo.bar`(Babel's implement now), after jest plugin removes the assert, in the test, the test writer can override modules, for mock something.


Pull request description:
feat(ecmascript/transforms/module): improve transform `misc import const throw`
close #2049 

Patch:
diff --git a/ecmascript/transforms/module/Cargo.toml b/ecmascript/transforms/module/Cargo.toml
index 9bcd7e2a71..5b308f88f0 100644
--- a/ecmascript/transforms/module/Cargo.toml
+++ b/ecmascript/transforms/module/Cargo.toml
@@ -6,7 +6,7 @@ edition = "2018"
 license = "Apache-2.0/MIT"
 name = "swc_ecma_transforms_module"
 repository = "https://github.com/swc-project/swc.git"
-version = "0.31.0"
+version = "0.31.1"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
diff --git a/ecmascript/transforms/module/src/util.rs b/ecmascript/transforms/module/src/util.rs
index 54971b8150..2e07a60cdf 100644
--- a/ecmascript/transforms/module/src/util.rs
+++ b/ecmascript/transforms/module/src/util.rs
@@ -708,11 +708,27 @@ impl Scope {
                             args: vec![],
                             type_args: Default::default(),
                         });
+
+                        let left = if let PatOrExpr::Pat(ref left_pat) = expr.left {
+                            if let Pat::Ident(BindingIdent { ref id, .. }) = **left_pat {
+                                let expr = match Self::fold_ident(folder, top_level, id.clone()) {
+                                    Ok(expr) => expr,
+                                    Err(ident) => Expr::Ident(ident),
+                                };
+                                PatOrExpr::Expr(Box::new(expr))
+                            } else {
+                                expr.left
+                            }
+                        } else {
+                            expr.left
+                        };
+
                         return Expr::Assign(AssignExpr {
                             right: Box::new(Expr::Seq(SeqExpr {
                                 span: DUMMY_SP,
                                 exprs: vec![expr.right, Box::new(throw)],
                             })),
+                            left,
                             ..expr
                         });
                     }


Test patch:
diff --git a/ecmascript/transforms/module/tests/common_js.rs b/ecmascript/transforms/module/tests/common_js.rs
index 85f3f31199..22bbef3822 100644
--- a/ecmascript/transforms/module/tests/common_js.rs
+++ b/ecmascript/transforms/module/tests/common_js.rs
@@ -1592,13 +1592,13 @@ var Bar = _interopRequireWildcard(require("bar"));
 
 var _baz = require("baz");
 
-Foo = (42, (function() {
+_foo.default = (42, (function() {
     throw new Error('"' + 'Foo' + '" is read-only.');
 })());
 Bar = (43, (function() {
     throw new Error('"' + 'Bar' + '" is read-only.');
 })());
-Baz = (44, (function() {
+_baz.Baz = (44, (function() {
     throw new Error('"' + 'Baz' + '" is read-only.');
 })());
 ({ Foo  } = ( {