diff --git a/crates/oxc_semantic/src/builder.rs b/crates/oxc_semantic/src/builder.rs index ea8b5ec776784..a1f7dc229d25d 100644 --- a/crates/oxc_semantic/src/builder.rs +++ b/crates/oxc_semantic/src/builder.rs @@ -1546,13 +1546,13 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> { self.visit_statement(&stmt.body); /* cfg - after body basic block */ - let after_body_graph_ix = self.cfg.new_basic_block(); + let after_body_graph_ix = self.cfg.current_node_ix; + let after_while_graph_ix = self.cfg.new_basic_block(); self.cfg.add_edge(before_while_stmt_graph_ix, condition_graph_ix, EdgeType::Normal); self.cfg.add_edge(condition_graph_ix, body_graph_ix, EdgeType::Normal); - self.cfg.add_edge(body_graph_ix, after_body_graph_ix, EdgeType::Normal); - self.cfg.add_edge(body_graph_ix, condition_graph_ix, EdgeType::Backedge); - self.cfg.add_edge(condition_graph_ix, after_body_graph_ix, EdgeType::Normal); + self.cfg.add_edge(after_body_graph_ix, condition_graph_ix, EdgeType::Backedge); + self.cfg.add_edge(condition_graph_ix, after_while_graph_ix, EdgeType::Normal); self.cfg.restore_state( &statement_state, diff --git a/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_in_while.js-2.snap b/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_in_while.js-2.snap index 9654709a5938d..6a2c05f207d7c 100644 --- a/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_in_while.js-2.snap +++ b/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@switch_in_while.js-2.snap @@ -26,8 +26,7 @@ digraph { 7 -> 8 [ label = Normal ] 0 -> 1 [ label = Normal ] 1 -> 2 [ label = Normal ] - 2 -> 9 [ label = Normal ] - 2 -> 1 [ label = Backedge ] + 8 -> 1 [ label = Backedge ] 1 -> 9 [ label = Normal ] 5 -> 1 [ label = Normal ] } diff --git a/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@while.js-2.snap b/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@while.js-2.snap index c97e9dbe19184..aec39ff589d2d 100644 --- a/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@while.js-2.snap +++ b/crates/oxc_semantic/tests/integration/snapshots/integration__cfg__cfg_files@while.js-2.snap @@ -21,8 +21,7 @@ digraph { 5 -> 6 [ label = Unreachable , style = "dotted" ] 1 -> 2 [ label = Normal ] 2 -> 5 [ label = Normal ] - 5 -> 7 [ label = Normal ] - 5 -> 2 [ label = Backedge ] + 6 -> 2 [ label = Backedge ] 2 -> 7 [ label = Normal ] 7 -> 8 [ label = Unreachable , style = "dotted" ] 0 -> 9 [ label = Normal ]