From f713f6aba84ffe84bed9dff80a772b0cd78135b8 Mon Sep 17 00:00:00 2001 From: thy486 Date: Thu, 3 Aug 2023 10:43:17 +0800 Subject: [PATCH] fix(es/module): Fix handling of continuous assignments in `systemjs` (#7741) **Description:** Fix transpile the continuous assignment code like `d.a = d.b = d.c = d.d = ... = void 0;` to SystemJS may cause Infinite loop. **Related issue:** - Closes #7143. --- .../src/system_js.rs | 9 +++++++++ .../tests/system_js.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/crates/swc_ecma_transforms_module/src/system_js.rs b/crates/swc_ecma_transforms_module/src/system_js.rs index 25bc0127a0b..520eb06b7de 100644 --- a/crates/swc_ecma_transforms_module/src/system_js.rs +++ b/crates/swc_ecma_transforms_module/src/system_js.rs @@ -562,6 +562,15 @@ impl Fold for SystemJs { let assign_expr = AssignExpr { right: match *assign.right { Expr::Ident(ident) => Box::new(self.fold_module_name_ident(ident)), + Expr::Assign(AssignExpr { + op: AssignOp::Assign, + .. + }) => { + return self.replace_assign_expr(AssignExpr { + right: assign.right, + ..assign + }); + } _ => assign.right, }, ..assign diff --git a/crates/swc_ecma_transforms_module/tests/system_js.rs b/crates/swc_ecma_transforms_module/tests/system_js.rs index 2d4cc2cde3d..40aeae243f1 100644 --- a/crates/swc_ecma_transforms_module/tests/system_js.rs +++ b/crates/swc_ecma_transforms_module/tests/system_js.rs @@ -22,6 +22,24 @@ fn tr(_tester: &mut Tester<'_>, config: Config) -> impl Fold { ) } +test!( + syntax(), + |tester| tr(tester, Default::default()), + allow_continuous_assignment, + r#"var e = {}; e.a = e.b = e.c = e.d = e.e = e.f = e.g = e.h = e.i = e.j = e.k = e.l = e.m = e.n = e.o = e.p = e.q = e.r = e.s = e.t = e.u = e.v = e.w = e.x = e.y = e.z = e.A = e.B = e.C = e.D = e.E = e.F = e.G = e.H = e.I = e.J = e.K = e.L = e.M = e.N = e.O = e.P = e.Q = e.R = e.S = void 0;"#, + r#"System.register([], function (_export, _context) { + "use strict"; + var e; + return { + setters: [], + execute: function () { + e = {}; + e.a = e.b = e.c = e.d = e.e = e.f = e.g = e.h = e.i = e.j = e.k = e.l = e.m = e.n = e.o = e.p = e.q = e.r = e.s = e.t = e.u = e.v = e.w = e.x = e.y = e.z = e.A = e.B = e.C = e.D = e.E = e.F = e.G = e.H = e.I = e.J = e.K = e.L = e.M = e.N = e.O = e.P = e.Q = e.R = e.S = void 0; + } + }; + });"# +); + test!( syntax(), |tester| tr(