use react_compiler_hir::{BlockId, HirFunction, Terminal};
pub fn flatten_reactive_loops_hir(func: &mut HirFunction) {
let mut active_loops: Vec<BlockId> = Vec::new();
let block_ids: Vec<BlockId> = func.body.blocks.keys().copied().collect();
for block_id in block_ids {
active_loops.retain(|id| *id != block_id);
let block = &func.body.blocks[&block_id];
let terminal = &block.terminal;
match terminal {
Terminal::DoWhile { fallthrough, .. }
| Terminal::For { fallthrough, .. }
| Terminal::ForIn { fallthrough, .. }
| Terminal::ForOf { fallthrough, .. }
| Terminal::While { fallthrough, .. } => {
active_loops.push(*fallthrough);
}
Terminal::Scope {
block,
fallthrough,
scope,
id,
loc,
} => {
if !active_loops.is_empty() {
let new_terminal = Terminal::PrunedScope {
block: *block,
fallthrough: *fallthrough,
scope: *scope,
id: *id,
loc: *loc,
};
let block_mut = func.body.blocks.get_mut(&block_id).unwrap();
block_mut.terminal = new_terminal;
}
}
_ => {}
}
}
}