Skip to content

Commit

Permalink
fix(es/generator): Fix code generation for break in nested while (#…
Browse files Browse the repository at this point in the history
…9684)

**Related issue:**

 - Closes #9110
  • Loading branch information
CPunisher authored Oct 29, 2024
1 parent 7aab945 commit 65872af
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/quick-actors-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_compat_es2015: patch
---

fix(es/generator): Fix code generation for `break` in nested while
10 changes: 10 additions & 0 deletions crates/swc/tests/exec/issues-9xxx/9110/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"externalHelpers": false,
"target": "es5"
},
"isModule": true
}
36 changes: 36 additions & 0 deletions crates/swc/tests/exec/issues-9xxx/9110/1/exec.js
Original file line number Diff line number Diff line change
@@ -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,
});
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2015/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 65872af

Please sign in to comment.