|
1 | | -use std::ops::ControlFlow; |
| 1 | +use std::{iter, ops::ControlFlow}; |
2 | 2 |
|
3 | 3 | use oxc_allocator::{Box, TakeIn, Vec}; |
4 | 4 | use oxc_ast::ast::*; |
@@ -39,7 +39,7 @@ impl<'a> PeepholeOptimizations { |
39 | 39 | let mut result: Vec<'a, Statement<'a>> = ctx.ast.vec_with_capacity(stmts.len()); |
40 | 40 | let mut is_control_flow_dead = false; |
41 | 41 | let mut keep_var = KeepVar::new(ctx.ast); |
42 | | - let mut new_stmts = ctx.ast.vec_from_iter(stmts.drain(..)); |
| 42 | + let mut new_stmts = stmts.take_in(ctx.ast.allocator); |
43 | 43 | for i in 0..new_stmts.len() { |
44 | 44 | let stmt = new_stmts[i].take_in(ctx.ast.allocator); |
45 | 45 | if is_control_flow_dead |
@@ -508,11 +508,12 @@ impl<'a> PeepholeOptimizations { |
508 | 508 | } |
509 | 509 |
|
510 | 510 | if can_move_branch_condition_outside_scope { |
511 | | - let mut body = ctx.ast.vec(); |
512 | | - if let Some(alternate) = if_stmt.alternate.take() { |
513 | | - body.push(alternate); |
514 | | - } |
515 | | - body.extend(stmts.drain(i + 1..)); |
| 511 | + let drained_stmts = stmts.drain(i + 1..); |
| 512 | + let mut body = if let Some(alternate) = if_stmt.alternate.take() { |
| 513 | + ctx.ast.vec_from_iter(iter::once(alternate).chain(drained_stmts)) |
| 514 | + } else { |
| 515 | + ctx.ast.vec_from_iter(drained_stmts) |
| 516 | + }; |
516 | 517 |
|
517 | 518 | self.minimize_statements(&mut body, state, ctx); |
518 | 519 | let span = |
|
0 commit comments