From 4f00914c1af6e2dee7116c09dd6e63b5883cf8b5 Mon Sep 17 00:00:00 2001 From: Austaras Date: Tue, 24 May 2022 19:32:49 +0800 Subject: [PATCH] fix(es/compat): Handle nested for loops with `break`/`continue` (#4777) --- .../src/es2015/block_scoping/mod.rs | 4 ---- .../tests/block-scoping/issue-4238/1/input.js | 9 +++++++ .../block-scoping/issue-4238/1/output.js | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/input.js create mode 100644 crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/output.js diff --git a/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs b/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs index 72deab51375..6cce0e90f5e 100644 --- a/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs +++ b/crates/swc_ecma_transforms_compat/src/es2015/block_scoping/mod.rs @@ -796,10 +796,6 @@ impl VisitMut for FlowHelper<'_> { match node { Stmt::Continue(ContinueStmt { label, .. }) => { - if self.in_nested_loop { - return; - } - let value = if let Some(label) = label { let value: JsWord = format!("continue|{}", label.sym).into(); self.label diff --git a/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/input.js b/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/input.js new file mode 100644 index 00000000000..cc9ff685fee --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/input.js @@ -0,0 +1,9 @@ +out: for (let i = 0; i < 4; i++) { + for (let j = 0; j < 4; ++j) { + if (i < 2) continue out; + + [1].forEach((_) => { + console.log(i, j); + }); + } +} diff --git a/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/output.js b/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/output.js new file mode 100644 index 00000000000..036f1477b96 --- /dev/null +++ b/crates/swc_ecma_transforms_compat/tests/block-scoping/issue-4238/1/output.js @@ -0,0 +1,24 @@ +var _loop = function(i) { + var _loop1 = function(j) { + if (i < 2) return "continue|out"; + [ + 1 + ].forEach((_)=>{ + console.log(i, j); + }); + }; + for(var j = 0; j < 4; ++j){ + var _ret1 = _loop1(j); + switch(_ret1){ + case "continue|out": + return "continue|out"; + } + } +}; +out: for(var i = 0; i < 4; i++){ + var _ret = _loop(i); + switch(_ret){ + case "continue|out": + continue out; + } +}