diff --git a/.changeset/quick-actors-do.md b/.changeset/quick-actors-do.md new file mode 100644 index 00000000000..47c0e9d1710 --- /dev/null +++ b/.changeset/quick-actors-do.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_compat_es2015: patch +--- + +fix(es/generator): Fix code generation for `break` in nested while diff --git a/crates/swc/tests/exec/issues-9xxx/9110/.swcrc b/crates/swc/tests/exec/issues-9xxx/9110/.swcrc new file mode 100644 index 00000000000..2ac251cd12a --- /dev/null +++ b/crates/swc/tests/exec/issues-9xxx/9110/.swcrc @@ -0,0 +1,10 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript" + }, + "externalHelpers": false, + "target": "es5" + }, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-9xxx/9110/1/exec.js b/crates/swc/tests/exec/issues-9xxx/9110/1/exec.js new file mode 100644 index 00000000000..acd67fe9150 --- /dev/null +++ b/crates/swc/tests/exec/issues-9xxx/9110/1/exec.js @@ -0,0 +1,36 @@ +function* test() { + while (!False()) { + // execute this line + while (!False()) { + // execute this line + break; + } + // execute this line + if (False()) { + // NOT execute this line + break; + } + + // execute this line + yield "correct"; + return; + } + + // NOT execute this line + yield "wrong"; + return; +} + +function False() { + return false; +} + +const t = test(); +expect(t.next()).toEqual({ + value: "correct", + done: false, +}); +expect(t.next()).toEqual({ + value: undefined, + done: true, +}); diff --git a/crates/swc_ecma_compat_es2015/src/generator.rs b/crates/swc_ecma_compat_es2015/src/generator.rs index 6303ab11178..d7debecdcf1 100644 --- a/crates/swc_ecma_compat_es2015/src/generator.rs +++ b/crates/swc_ecma_compat_es2015/src/generator.rs @@ -1659,7 +1659,7 @@ impl Generator { self.emit_break(loop_label, None); self.end_loop_block(); } else { - node.visit_mut_children_with(self); + node.visit_mut_with(self); self.emit_stmt(node.into()); }