From cf96171a53589118a0103495169e02fed10a675f Mon Sep 17 00:00:00 2001 From: magic-akari Date: Fri, 29 Sep 2023 15:34:24 -0500 Subject: [PATCH] fix(es/typescript): Preserve default value of an exported binding in a namespace (#8029) **Related issue:** - Closes: #8025 --- .../fixture/issues-2xxx/2214/output/index.tsx | 2 +- .../fixture/issues-2xxx/2310/output/index.tsx | 2 +- .../fixture/issues-2xxx/2423/output/index.map | 2 +- .../fixture/issues-2xxx/2423/output/index.ts | 4 +- .../fixture/issues-2xxx/2636/output/index.ts | 4 +- .../issues-3xxx/3073/1/output/index.ts | 4 +- .../issues-3xxx/3073/2/output/index.ts | 4 +- .../issues-4xxx/4393/1/output/index.ts | 2 +- .../fixture/issues-4xxx/4897/output/index.ts | 2 +- ...ortedAndNonExportedImportAlias.1.normal.js | 2 +- .../circularImportAlias.1.normal.js | 10 +-- .../circularImportAlias.2.minified.js | 6 +- .../es6modulekindWithES5Target12.1.normal.js | 10 +-- ...snextmodulekindWithES5Target12.1.normal.js | 10 +-- .../exportImportAlias.1.normal.js | 7 +- .../filterNamespace_import.1.normal.js | 2 +- .../tsc-references/typeOnlyMerge3.1.normal.js | 2 +- .../valuesMergingAcrossModules.1.normal.js | 2 +- ...duleSyntaxInternalImportEquals.1.normal.js | 2 +- ...rbatimModuleSyntaxNoElisionCJS.1.normal.js | 2 +- ...timModuleSyntaxRestrictionsCJS.1.normal.js | 4 +- .../src/transform.rs | 79 +++++++++---------- .../tests/fixture/issue-1653/output.js | 4 +- .../tests/fixture/issue-2243/output.js | 8 +- .../tests/fixture/issue-2669/1/output.js | 2 +- .../tests/fixture/issue-2670/output.js | 4 +- .../tests/fixture/issue-3073/1/output.js | 4 +- .../tests/fixture/issue-3073/2/output.js | 4 +- .../tests/fixture/issue-3073/3/output.js | 2 +- .../tests/fixture/issue-3073/4/output.js | 16 ++-- .../tests/fixture/issue-3158/1/output.js | 6 +- .../tests/fixture/issue-3158/2/output.js | 4 +- .../tests/fixture/issue-4393/1/output.js | 2 +- .../tests/fixture/issue-4481/1/output.js | 6 +- .../tests/fixture/issue-8025/input.ts | 9 +++ .../tests/fixture/issue-8025/output.js | 8 ++ .../tests/strip.rs | 22 +++--- 37 files changed, 138 insertions(+), 127 deletions(-) create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/input.ts create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/output.js diff --git a/crates/swc/tests/fixture/issues-2xxx/2214/output/index.tsx b/crates/swc/tests/fixture/issues-2xxx/2214/output/index.tsx index b732a3aa7cb5..a880aafa508d 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2214/output/index.tsx +++ b/crates/swc/tests/fixture/issues-2xxx/2214/output/index.tsx @@ -1,5 +1,5 @@ import React from "react"; export var Test; (function(Test) { - var content = Test.content = /*#__PURE__*/ React.createElement("div", null, "Content"); + Test.content = /*#__PURE__*/ React.createElement("div", null, "Content"); })(Test || (Test = {})); diff --git a/crates/swc/tests/fixture/issues-2xxx/2310/output/index.tsx b/crates/swc/tests/fixture/issues-2xxx/2310/output/index.tsx index d6a8666966c2..afbf533af3b8 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2310/output/index.tsx +++ b/crates/swc/tests/fixture/issues-2xxx/2310/output/index.tsx @@ -1,6 +1,6 @@ var A; (function(A) { - var Foo = A.Foo = function() { + A.Foo = function() { return /*#__PURE__*/ React.createElement("div", null); }; })(A || (A = {})); diff --git a/crates/swc/tests/fixture/issues-2xxx/2423/output/index.map b/crates/swc/tests/fixture/issues-2xxx/2423/output/index.map index 7ba6bf780d84..f36709498258 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2423/output/index.map +++ b/crates/swc/tests/fixture/issues-2xxx/2423/output/index.map @@ -1,5 +1,5 @@ { - "mappings": ";;UAAUA;IACC,MAAMC,MAAAA,IAAI;IACV,SAASC;QACZC,QAAQC,GAAG,CAACH;IAChB;MAFgBC,IAAAA;AAGpB,GALUF,MAAAA", + "mappings": ";;UAAUA;MACOC,IAAI;IACV,SAASC;QACZC,QAAQC,GAAG,GAACH;IAChB;MAFgBC,IAAAA;AAGpB,GALUF,MAAAA", "names": [ "A", "v", diff --git a/crates/swc/tests/fixture/issues-2xxx/2423/output/index.ts b/crates/swc/tests/fixture/issues-2xxx/2423/output/index.ts index 69321d20b5ec..1103870821ce 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2423/output/index.ts +++ b/crates/swc/tests/fixture/issues-2xxx/2423/output/index.ts @@ -1,9 +1,9 @@ "use strict"; var A; (function(A) { - const v = A.v = 25; + A.v = 25; function a() { - console.log(v); + console.log(A.v); } A.a = a; })(A || (A = {})); diff --git a/crates/swc/tests/fixture/issues-2xxx/2636/output/index.ts b/crates/swc/tests/fixture/issues-2xxx/2636/output/index.ts index 5f08afbf680a..22c6109df2ae 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2636/output/index.ts +++ b/crates/swc/tests/fixture/issues-2xxx/2636/output/index.ts @@ -1,5 +1,5 @@ export var RuleInterpreterHelper; (function(RuleInterpreterHelper) { - var fieldNameMap = RuleInterpreterHelper.fieldNameMap = []; - var fieldNameHashMap = RuleInterpreterHelper.fieldNameHashMap = new Map(fieldNameMap); + RuleInterpreterHelper.fieldNameMap = []; + RuleInterpreterHelper.fieldNameHashMap = new Map(RuleInterpreterHelper.fieldNameMap); })(RuleInterpreterHelper || (RuleInterpreterHelper = {})); diff --git a/crates/swc/tests/fixture/issues-3xxx/3073/1/output/index.ts b/crates/swc/tests/fixture/issues-3xxx/3073/1/output/index.ts index b2870519f826..5583a26af79c 100644 --- a/crates/swc/tests/fixture/issues-3xxx/3073/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-3xxx/3073/1/output/index.ts @@ -1,7 +1,7 @@ var n; (function(n) { var ref; - var a = (ref = { + ref = { a: 1 - }, n.a = ref.a, ref).a; + }, n.a = ref.a, ref; })(n || (n = {})); diff --git a/crates/swc/tests/fixture/issues-3xxx/3073/2/output/index.ts b/crates/swc/tests/fixture/issues-3xxx/3073/2/output/index.ts index abd877a028a4..954ea553b863 100644 --- a/crates/swc/tests/fixture/issues-3xxx/3073/2/output/index.ts +++ b/crates/swc/tests/fixture/issues-3xxx/3073/2/output/index.ts @@ -1,8 +1,8 @@ var Foo; (function(Foo) { var ref; - var _ref = (ref = { + ref = { a: 1, b: 2 - }, Foo.A = ref.a, Foo.B = ref.b, ref), A = _ref.a, B = _ref.b; + }, Foo.A = ref.a, Foo.B = ref.b, ref; })(Foo || (Foo = {})); diff --git a/crates/swc/tests/fixture/issues-4xxx/4393/1/output/index.ts b/crates/swc/tests/fixture/issues-4xxx/4393/1/output/index.ts index 6efae1288496..86a48c900d61 100644 --- a/crates/swc/tests/fixture/issues-4xxx/4393/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-4xxx/4393/1/output/index.ts @@ -1,5 +1,5 @@ var foo; (function(foo) { - var bar = foo.bar = 0; + foo.bar = 0; })(foo || (foo = {})); export { foo }; diff --git a/crates/swc/tests/fixture/issues-4xxx/4897/output/index.ts b/crates/swc/tests/fixture/issues-4xxx/4897/output/index.ts index 2e64e0f23712..1fe36edf9d95 100644 --- a/crates/swc/tests/fixture/issues-4xxx/4897/output/index.ts +++ b/crates/swc/tests/fixture/issues-4xxx/4897/output/index.ts @@ -22,6 +22,6 @@ define([ } var Foo; (function(Foo) { - const Bar = Foo.Bar = 1234; + Foo.Bar = 1234; })(Foo || (Foo = {})); }); diff --git a/crates/swc/tests/tsc-references/ModuleWithExportedAndNonExportedImportAlias.1.normal.js b/crates/swc/tests/tsc-references/ModuleWithExportedAndNonExportedImportAlias.1.normal.js index d2c4bb452c74..8b1e54362960 100644 --- a/crates/swc/tests/tsc-references/ModuleWithExportedAndNonExportedImportAlias.1.normal.js +++ b/crates/swc/tests/tsc-references/ModuleWithExportedAndNonExportedImportAlias.1.normal.js @@ -12,7 +12,7 @@ var B; })(B || (B = {})); var Geometry; (function(Geometry) { - var Points = Geometry.Points = A; + Geometry.Points = A; var Lines = B; Geometry.Origin = { x: 0, diff --git a/crates/swc/tests/tsc-references/circularImportAlias.1.normal.js b/crates/swc/tests/tsc-references/circularImportAlias.1.normal.js index 68a7daf8c327..10f39de15ab0 100644 --- a/crates/swc/tests/tsc-references/circularImportAlias.1.normal.js +++ b/crates/swc/tests/tsc-references/circularImportAlias.1.normal.js @@ -5,17 +5,17 @@ import { _ as _inherits } from "@swc/helpers/_/_inherits"; import { _ as _create_super } from "@swc/helpers/_/_create_super"; var B; (function(B) { - var a = B.a = A; - var D = /*#__PURE__*/ function(_a_C) { + B.a = A; + var D = /*#__PURE__*/ function(_B_a_C) { "use strict"; - _inherits(D, _a_C); + _inherits(D, _B_a_C); var _super = _create_super(D); function D() { _class_call_check(this, D); return _super.apply(this, arguments); } return D; - }(a.C); + }(B.a.C); B.D = D; })(B || (B = {})); var A; @@ -25,7 +25,7 @@ var A; _class_call_check(this, C); }; A.C = C; - var b = A.b = B; + A.b = B; })(A || (A = {})); var c; var c = new B.a.C(); diff --git a/crates/swc/tests/tsc-references/circularImportAlias.2.minified.js b/crates/swc/tests/tsc-references/circularImportAlias.2.minified.js index 283e930eaecc..d3590cf3c25f 100644 --- a/crates/swc/tests/tsc-references/circularImportAlias.2.minified.js +++ b/crates/swc/tests/tsc-references/circularImportAlias.2.minified.js @@ -4,13 +4,13 @@ var B, A, B1, D, A1; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; import { _ as _create_super } from "@swc/helpers/_/_create_super"; -D = function(_a_C) { - _inherits(D, _a_C); +(B1 = B || (B = {})).a = A, D = function(_B_a_C) { + _inherits(D, _B_a_C); var _super = _create_super(D); function D() { return _class_call_check(this, D), _super.apply(this, arguments); } return D; -}(((B1 = B || (B = {})).a = A).C), B1.D = D, (A1 = A || (A = {})).C = function C() { +}(B1.a.C), B1.D = D, (A1 = A || (A = {})).C = function C() { _class_call_check(this, C); }, A1.b = B, new B.a.C(); diff --git a/crates/swc/tests/tsc-references/es6modulekindWithES5Target12.1.normal.js b/crates/swc/tests/tsc-references/es6modulekindWithES5Target12.1.normal.js index ea9ba20c40b2..a805fe65979f 100644 --- a/crates/swc/tests/tsc-references/es6modulekindWithES5Target12.1.normal.js +++ b/crates/swc/tests/tsc-references/es6modulekindWithES5Target12.1.normal.js @@ -5,7 +5,7 @@ export var C = function C() { _class_call_check(this, C); }; (function(C) { - var x = C.x = 1; + C.x = 1; })(C || (C = {})); export var E; (function(E) { @@ -15,16 +15,16 @@ export var E; E[E["x"] = 2] = "x"; })(E || (E = {})); (function(E) { - var y = E.y = 1; + E.y = 1; })(E || (E = {})); (function(E) { - var z = E.z = 1; + E.z = 1; })(E || (E = {})); export var N; (function(N) { - var x = N.x = 1; + N.x = 1; })(N || (N = {})); export function F() {} (function(F) { - var x = F.x = 1; + F.x = 1; })(F || (F = {})); diff --git a/crates/swc/tests/tsc-references/esnextmodulekindWithES5Target12.1.normal.js b/crates/swc/tests/tsc-references/esnextmodulekindWithES5Target12.1.normal.js index 1ddc1276ff7c..a5f03d9f98fe 100644 --- a/crates/swc/tests/tsc-references/esnextmodulekindWithES5Target12.1.normal.js +++ b/crates/swc/tests/tsc-references/esnextmodulekindWithES5Target12.1.normal.js @@ -5,7 +5,7 @@ export var C = function C() { _class_call_check(this, C); }; (function(C) { - var x = C.x = 1; + C.x = 1; })(C || (C = {})); export var E; (function(E) { @@ -15,16 +15,16 @@ export var E; E[E["x"] = 2] = "x"; })(E || (E = {})); (function(E) { - var y = E.y = 1; + E.y = 1; })(E || (E = {})); (function(E) { - var z = E.z = 1; + E.z = 1; })(E || (E = {})); export var N; (function(N) { - var x = N.x = 1; + N.x = 1; })(N || (N = {})); export function F() {} (function(F) { - var x = F.x = 1; + F.x = 1; })(F || (F = {})); diff --git a/crates/swc/tests/tsc-references/exportImportAlias.1.normal.js b/crates/swc/tests/tsc-references/exportImportAlias.1.normal.js index 102aa2e0855b..a8aa8e18c0f7 100644 --- a/crates/swc/tests/tsc-references/exportImportAlias.1.normal.js +++ b/crates/swc/tests/tsc-references/exportImportAlias.1.normal.js @@ -14,7 +14,7 @@ var A; })(A || (A = {})); var C; (function(C) { - var a = C.a = A; + C.a = A; })(C || (C = {})); var a = C.a.x; var b = new C.a.Point(0, 0); @@ -38,7 +38,8 @@ var X; })(X || (X = {})); var Z; (function(Z) { - var y = Z.y = X.Y; + // 'y' should be a fundule here + Z.y = X.Y; })(Z || (Z = {})); var m = Z.y(); var n = new Z.y.Point(0, 0); @@ -56,7 +57,7 @@ var K; })(K || (K = {})); var M; (function(M) { - var D = M.D = K.L; + M.D = K.L; })(M || (M = {})); var o; var o = new M.D("Hello"); diff --git a/crates/swc/tests/tsc-references/filterNamespace_import.1.normal.js b/crates/swc/tests/tsc-references/filterNamespace_import.1.normal.js index c64ea6654eab..a6db0ead817a 100644 --- a/crates/swc/tests/tsc-references/filterNamespace_import.1.normal.js +++ b/crates/swc/tests/tsc-references/filterNamespace_import.1.normal.js @@ -7,7 +7,7 @@ var ns; _class_call_check(this, Class); }; ns.Class = Class; - var Value = ns.Value = ""; + ns.Value = ""; var nested; (function(nested) { var NestedClass = function NestedClass() { diff --git a/crates/swc/tests/tsc-references/typeOnlyMerge3.1.normal.js b/crates/swc/tests/tsc-references/typeOnlyMerge3.1.normal.js index 207c1c2748f9..f43b4e6ce4a1 100644 --- a/crates/swc/tests/tsc-references/typeOnlyMerge3.1.normal.js +++ b/crates/swc/tests/tsc-references/typeOnlyMerge3.1.normal.js @@ -4,7 +4,7 @@ export { }; //// [b.ts] var A; (function(A) { - var displayName = A.displayName = "A"; + A.displayName = "A"; })(A || (A = {})); export { A }; //// [c.ts] diff --git a/crates/swc/tests/tsc-references/valuesMergingAcrossModules.1.normal.js b/crates/swc/tests/tsc-references/valuesMergingAcrossModules.1.normal.js index 838a7e07da53..6c7d1d22009a 100644 --- a/crates/swc/tests/tsc-references/valuesMergingAcrossModules.1.normal.js +++ b/crates/swc/tests/tsc-references/valuesMergingAcrossModules.1.normal.js @@ -6,7 +6,7 @@ import { A } from "./a"; //// [c.ts] var A; (function(A) { - var displayName = A.displayName = "A"; + A.displayName = "A"; })(A || (A = {})); A(); A.displayName; diff --git a/crates/swc/tests/tsc-references/verbatimModuleSyntaxInternalImportEquals.1.normal.js b/crates/swc/tests/tsc-references/verbatimModuleSyntaxInternalImportEquals.1.normal.js index 03215f735704..e3e1d3c836f7 100644 --- a/crates/swc/tests/tsc-references/verbatimModuleSyntaxInternalImportEquals.1.normal.js +++ b/crates/swc/tests/tsc-references/verbatimModuleSyntaxInternalImportEquals.1.normal.js @@ -3,7 +3,7 @@ export { }; var f1 = NonExistent; var Foo; (function(Foo) { - var foo = Foo.foo = 1; + Foo.foo = 1; })(Foo || (Foo = {})); var f2 = Foo.foo; var f3 = Foo.T; diff --git a/crates/swc/tests/tsc-references/verbatimModuleSyntaxNoElisionCJS.1.normal.js b/crates/swc/tests/tsc-references/verbatimModuleSyntaxNoElisionCJS.1.normal.js index 9df87238475b..4f2ce71c0105 100644 --- a/crates/swc/tests/tsc-references/verbatimModuleSyntaxNoElisionCJS.1.normal.js +++ b/crates/swc/tests/tsc-references/verbatimModuleSyntaxNoElisionCJS.1.normal.js @@ -13,7 +13,7 @@ const I = require("./a"); "use strict"; var I; (function(I) { - const x = I.x = 1; + I.x = 1; })(I || (I = {})); module.exports = I; //// [/d.ts] diff --git a/crates/swc/tests/tsc-references/verbatimModuleSyntaxRestrictionsCJS.1.normal.js b/crates/swc/tests/tsc-references/verbatimModuleSyntaxRestrictionsCJS.1.normal.js index 51d28577895d..6ab662acc521 100644 --- a/crates/swc/tests/tsc-references/verbatimModuleSyntaxRestrictionsCJS.1.normal.js +++ b/crates/swc/tests/tsc-references/verbatimModuleSyntaxRestrictionsCJS.1.normal.js @@ -39,7 +39,7 @@ Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard._(require("./ const x = 1; // error var Values; (function(Values) { - const x = Values.x = 1; + Values.x = 1; })(Values || (Values = {})); // sketchy, but ok //// [/main2.ts] @@ -51,7 +51,7 @@ module.exports = { "use strict"; var ns; (function(ns) { - const x = ns.x = 1; + ns.x = 1; })(ns || (ns = {})); module.exports = ns; //// [/main4.ts] diff --git a/crates/swc_ecma_transforms_typescript/src/transform.rs b/crates/swc_ecma_transforms_typescript/src/transform.rs index acda6c011120..866737056b18 100644 --- a/crates/swc_ecma_transforms_typescript/src/transform.rs +++ b/crates/swc_ecma_transforms_typescript/src/transform.rs @@ -578,10 +578,8 @@ impl Transform { } /// Note: -/// Const exported declarations are transformed into immutable bindings. -/// `export` from `export let` / ` export var` will be stripped, -/// and a mutable binding will be transformed into assignment to the -/// namespace. All references to the mutable binding will be replaced with +/// All exported variable declarations are transformed into assignment to the +/// namespace. All references to the exported binding will be replaced with /// qualified access to the namespace property. /// /// Exported function and class will be treat as const exported which is in @@ -592,7 +590,7 @@ impl Transform { /// /// Input: /// ```TypeScript -/// export const foo = init, { bar: baz } = init; +/// export const foo = init, { bar: baz = init } = init; /// /// export function a() {} /// @@ -601,7 +599,7 @@ impl Transform { /// /// Output: /// ```TypeScript -/// const foo = NS.foo = init, { bar: baz } = { bar: NS.baz } = init; +/// NS.foo = init, { bar: NS.baz = init } = init; /// /// function a() {} /// NS.a = a; @@ -640,18 +638,14 @@ impl Transform { ) -> Option { debug_assert!(!var_decl.declare); - var_decl.decls.iter_mut().for_each(|var_declarator| { - Self::assign_init_to_ns_id(var_declarator, id); - }); - - if var_decl.kind == VarDeclKind::Const { - return Some(var_decl.into()); - } - let mut collector = ExportedIdentCollector::default(); var_decl.visit_with(&mut collector); mutable_export_ids.extend(collector.export_list); + var_decl.decls.visit_mut_with(&mut ExportedPatRewriter { + id: id.clone().into(), + }); + let mut expr_list: Vec> = var_decl.decls.into_iter().filter_map(|d| d.init).collect(); @@ -672,31 +666,6 @@ impl Transform { Some(Stmt::Expr(ExprStmt { span, expr })) } - - /// Input: - /// ```TypeScript - /// const foo = init; - /// ``` - /// Output: - /// ```TypeScript - /// const foo = NS.foo = init; - /// ``` - fn assign_init_to_ns_id(var_declarator: &mut VarDeclarator, id: &Id) { - let Some(right) = var_declarator.init.take() else { - return; - }; - - let mut left = var_declarator.name.clone(); - left.visit_mut_with(&mut ExportedPatRewriter { - id: id.clone().into(), - }); - - var_declarator.init = Some( - right - .make_assign_to(op!("="), PatOrExpr::Pat(left.into())) - .into(), - ); - } } impl Transform { @@ -1135,6 +1104,21 @@ struct ExportedPatRewriter { impl VisitMut for ExportedPatRewriter { noop_visit_mut_type!(); + fn visit_mut_var_declarator(&mut self, n: &mut VarDeclarator) { + let Some(right) = n.init.take() else { + return; + }; + + let mut left = n.name.take(); + left.visit_mut_with(self); + + n.init = Some( + right + .make_assign_to(op!("="), PatOrExpr::Pat(left.into())) + .into(), + ); + } + fn visit_mut_pat(&mut self, n: &mut Pat) { if let Pat::Ident(BindingIdent { id, .. }) = n { *n = Pat::Expr(Box::new(self.id.clone().make_member(id.take()))); @@ -1145,10 +1129,23 @@ impl VisitMut for ExportedPatRewriter { } fn visit_mut_object_pat_prop(&mut self, n: &mut ObjectPatProp) { - if let ObjectPatProp::Assign(AssignPatProp { key, .. }) = n { + if let ObjectPatProp::Assign(AssignPatProp { key, value, .. }) = n { + let left = Box::new(Pat::Expr(self.id.clone().make_member(key.clone()).into())); + + let value = if let Some(right) = value.take() { + Pat::Assign(AssignPat { + span: DUMMY_SP, + left, + right, + }) + .into() + } else { + left + }; + *n = ObjectPatProp::KeyValue(KeyValuePatProp { key: PropName::Ident(key.clone()), - value: Box::new(Pat::Expr(self.id.clone().make_member(key.clone()).into())), + value, }); return; } diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-1653/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-1653/output.js index 5af1a9fbc89c..d7089cdcf19a 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-1653/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-1653/output.js @@ -2,13 +2,13 @@ var X; (function(X) { let Z; (function(Z) { - const foo = Z.foo = 0; + Z.foo = 0; })(Z = X.Z || (X.Z = {})); })(X || (X = {})); var Y; (function(Y) { let Z; (function(Z) { - const bar = Z.bar = 1; + Z.bar = 1; })(Z = Y.Z || (Y.Z = {})); })(Y || (Y = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2243/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2243/output.js index 09bbd105722b..3723e2c4a473 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2243/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2243/output.js @@ -2,7 +2,7 @@ export function Colors(member) { return Colors.ValueFor(member); } (function(Colors) { - const ValueMap = Colors.ValueMap = { + Colors.ValueMap = { Red: { value: 0.0, label: "Red" @@ -16,17 +16,17 @@ export function Colors(member) { label: "Green" } }; - const Values = Colors.Values = [ + Colors.Values = [ 0.0, 1.0, 2.0 ]; function ValueFor(member) { - return ValueMap[member]?.value; + return Colors.ValueMap[member]?.value; } Colors.ValueFor = ValueFor; async function LabelFor(member) { - return ValueMap[member]?.label; + return Colors.ValueMap[member]?.label; } Colors.LabelFor = LabelFor; })(Colors || (Colors = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2669/1/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2669/1/output.js index d0117e667e9e..b78219007575 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2669/1/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2669/1/output.js @@ -1,5 +1,5 @@ export var TEST; (function(TEST) { - const VALUE = TEST.VALUE = "value"; + TEST.VALUE = "value"; })(TEST || (TEST = {})); export default TEST; diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2670/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2670/output.js index 6fc5b10ef5a0..d910c437c007 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2670/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-2670/output.js @@ -1,5 +1,5 @@ export var TEST; (function(TEST) { - const VAL1 = TEST.VAL1 = "value1"; - const VAL2 = TEST.VAL2 = `${VAL1}_value2`; + TEST.VAL1 = "value1"; + TEST.VAL2 = `${TEST.VAL1}_value2`; })(TEST || (TEST = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/1/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/1/output.js index 2772e2980458..f756daad9886 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/1/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/1/output.js @@ -1,6 +1,6 @@ var Foo; (function(Foo) { - const { a } = { a: Foo.a } = { + ({ a: Foo.a } = { a: 1 - }; + }); })(Foo || (Foo = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/2/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/2/output.js index 81e6f5b3638e..fa06fcec7fb5 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/2/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/2/output.js @@ -1,7 +1,7 @@ var Bar; (function(Bar) { - const { a, b } = { a: Bar.a, b: Bar.b } = { + ({ a: Bar.a, b: Bar.b } = { a: 1, b: 2 - }; + }); })(Bar || (Bar = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/3/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/3/output.js index 17a0966a2292..afd734b74db6 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/3/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/3/output.js @@ -1,6 +1,6 @@ var Baz; (function(Baz) { - const baz = Baz.baz = { + Baz.baz = { a: 1, b: 2 }; diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/4/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/4/output.js index f04110dd8185..51f4632224a2 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/4/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3073/4/output.js @@ -2,20 +2,20 @@ var A; (function(A) { const a = 1; const b = 2; - const { a: A1 } = { a: A.A } = { + ({ a: A.A } = { a - }; - const { b: B } = { b: A.B } = { + }); + ({ b: A.B } = { b - }; + }); })(A || (A = {})); (function(A) { const c = 3; const d = 4; - const { c: C } = { c: A.C } = { + ({ c: A.C } = { c: c - }; - const { d: D } = { d: A.D } = { + }); + ({ d: A.D } = { d: d - }; + }); })(A || (A = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/1/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/1/output.js index d7c6e5c993a1..d17dd509f1c4 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/1/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/1/output.js @@ -1,11 +1,11 @@ var Foo; (function(Foo) { - const bar = Foo.bar = 42; - const foo = Foo.foo = function() { + Foo.bar = 42; + Foo.foo = function() { return 20; }; function xyz() { - return foo() * bar; + return Foo.foo() * Foo.bar; } Foo.xyz = xyz; })(Foo || (Foo = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/2/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/2/output.js index ab27eaa34d43..34167b9ba252 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/2/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-3158/2/output.js @@ -4,11 +4,11 @@ var Test; return 10; } Test.abc = abc; - const foo = Test.foo = function() { + Test.foo = function() { return 20; }; function xyz() { - return abc() * foo(); + return abc() * Test.foo(); } Test.xyz = xyz; })(Test || (Test = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4393/1/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4393/1/output.js index cf63cb61d429..86a48c900d61 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4393/1/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4393/1/output.js @@ -1,5 +1,5 @@ var foo; (function(foo) { - const bar = foo.bar = 0; + foo.bar = 0; })(foo || (foo = {})); export { foo }; diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4481/1/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4481/1/output.js index 9231d2a0dcf1..5af757693f37 100644 --- a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4481/1/output.js +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-4481/1/output.js @@ -1,7 +1,7 @@ import * as I from "./ABC"; export var IE; (function(IE) { - const A = IE.A = I.A; - const C = IE.C = I.C; - const D = IE.D = I.D; + IE.A = I.A; + IE.C = I.C; + IE.D = I.D; })(IE || (IE = {})); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/input.ts b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/input.ts new file mode 100644 index 000000000000..081be3adedb2 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/input.ts @@ -0,0 +1,9 @@ +namespace Foo { + export let { a = 1 } = {}; + export const { b = 2 } = {}; + export const [c = 3, { d = 4 } = {}] = []; + + console.log("inner", a, b, c, d); +} + +console.log("outer", Foo.a, Foo.b, Foo.c, Foo.d); diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/output.js new file mode 100644 index 000000000000..8d5834fde71a --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-8025/output.js @@ -0,0 +1,8 @@ +var Foo; +(function(Foo) { + ({ a: Foo.a = 1 } = {}); + ({ b: Foo.b = 2 } = {}); + [Foo.c = 3, { d: Foo.d = 4 } = {}] = []; + console.log("inner", Foo.a, Foo.b, Foo.c, Foo.d); +})(Foo || (Foo = {})); +console.log("outer", Foo.a, Foo.b, Foo.c, Foo.d); diff --git a/crates/swc_ecma_transforms_typescript/tests/strip.rs b/crates/swc_ecma_transforms_typescript/tests/strip.rs index 5821b1d2d843..6c8b564bd296 100644 --- a/crates/swc_ecma_transforms_typescript/tests/strip.rs +++ b/crates/swc_ecma_transforms_typescript/tests/strip.rs @@ -3365,14 +3365,10 @@ to!( throw new Error(); } util.assertNever = assertNever; - const arrayToEnum = util.arrayToEnum = (items)=>{ - }; - const getValidEnumValues = util.getValidEnumValues = (obj)=>{ - }; - const getValues = util.getValues = (obj)=>{ - }; - const objectValues = util.objectValues = (obj)=>{ - }; + util.arrayToEnum = (items)=>{}; + util.getValidEnumValues = (obj)=>{}; + util.getValues = (obj)=>{}; + util.objectValues = (obj)=>{}; })(util || (util = {})); " ); @@ -3389,7 +3385,7 @@ to!( export var util; (function (util) { const c = 3; - const [a, b] = [util.a, util.b] = [1, 2, 3]; + [util.a, util.b] = [1, 2, 3]; })(util || (util = {})); " ); @@ -3435,7 +3431,7 @@ to!( var Test; (function (Test) { (function (Inner) { - const c = Inner.c = 3; + Inner.c = 3; })(Test.Inner || (Test.Inner = {})); })(Test || (Test = {})); " @@ -3490,7 +3486,7 @@ to!( })(MyEnum = MyNamespace.MyEnum || (MyNamespace.MyEnum = {})); let MyInnerNamespace; (function (MyInnerNamespace) { - const Dec2 = MyInnerNamespace.Dec2 = 2; + MyInnerNamespace.Dec2 = 2; })(MyInnerNamespace = MyNamespace.MyInnerNamespace || (MyNamespace.MyInnerNamespace = {})); })(MyNamespace || (MyNamespace = {})); (function (MyNamespace) { @@ -3524,8 +3520,8 @@ to!( })(A || (A = {})); var B; (function (B) { - const a = B.a = A; - console.log(a.Test); + B.a = A; + console.log(B.a.Test); const b = A; console.log(b.Test); })(B || (B = {}));