Skip to content

Commit 076a800

Browse files
committed
fix: loops
1 parent 35f229c commit 076a800

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

crates/tree_shaker/src/nodes/stmt/do_while_statement.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Data {
1414

1515
impl<'a> Analyzer<'a> {
1616
pub fn exec_do_while_statement(&mut self, node: &'a DoWhileStatement<'a>) {
17-
self.push_cf_scope(CfScopeKind::Loop, Some(false));
17+
self.push_cf_scope(CfScopeKind::LoopBreak, Some(false));
1818

1919
// Execute the first round.
2020
self.exec_statement(&node.body);
@@ -38,7 +38,7 @@ impl<'a> Analyzer<'a> {
3838
data.need_loop = true;
3939

4040
self.exec_loop(move |analyzer| {
41-
analyzer.push_cf_scope(CfScopeKind::Loop, None);
41+
analyzer.push_cf_scope(CfScopeKind::LoopContinue, None);
4242

4343
analyzer.exec_statement(&node.body);
4444
analyzer.exec_expression(&node.test).consume(analyzer);

crates/tree_shaker/src/nodes/stmt/for_in_statement.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ impl<'a> Analyzer<'a> {
1111
if let Some(keys) = right.get_own_keys(self) {
1212
let dep =
1313
right.get_destructable(self, self.factory.consumable(AstKind2::ForInStatement(node)));
14+
self.push_cf_scope_with_deps(CfScopeKind::LoopBreak, vec![dep], Some(false));
1415
for (definite, key) in keys {
1516
self.push_cf_scope_with_deps(
16-
CfScopeKind::Loop,
17-
vec![dep, self.factory.always_mangable_dep(key)],
17+
CfScopeKind::LoopContinue,
18+
vec![self.factory.always_mangable_dep(key)],
1819
if definite { Some(false) } else { None },
1920
);
2021
self.push_variable_scope();
@@ -26,11 +27,17 @@ impl<'a> Analyzer<'a> {
2627

2728
self.pop_variable_scope();
2829
self.pop_cf_scope();
30+
31+
if self.cf_scope().must_exited() {
32+
break;
33+
}
2934
}
35+
self.pop_cf_scope();
3036
} else {
3137
let dep = self.consumable((AstKind2::ForInStatement(node), right));
38+
self.push_cf_scope_with_deps(CfScopeKind::LoopBreak, vec![dep], Some(false));
3239
self.exec_loop(move |analyzer| {
33-
analyzer.push_cf_scope_with_deps(CfScopeKind::Loop, vec![dep], None);
40+
analyzer.push_cf_scope_with_deps(CfScopeKind::LoopContinue, vec![], None);
3441
analyzer.push_variable_scope();
3542

3643
analyzer.declare_for_statement_left(&node.left);
@@ -41,6 +48,7 @@ impl<'a> Analyzer<'a> {
4148
analyzer.pop_variable_scope();
4249
analyzer.pop_cf_scope();
4350
});
51+
self.pop_cf_scope();
4452
}
4553
}
4654
}

crates/tree_shaker/src/nodes/stmt/for_of_statement.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ impl<'a> Analyzer<'a> {
2525

2626
let dep = self.consumable((AstKind2::ForOfStatement(node), right));
2727

28-
self.push_cf_scope_with_deps(CfScopeKind::Loop, vec![dep], Some(false));
28+
self.push_cf_scope_with_deps(CfScopeKind::LoopBreak, vec![dep], Some(false));
2929
self.exec_loop(move |analyzer| {
3030
analyzer.declare_for_statement_left(&node.left);
3131
analyzer.init_for_statement_left(&node.left, iterated);
3232

33-
analyzer.push_cf_scope(CfScopeKind::Loop, None);
33+
analyzer.push_cf_scope(CfScopeKind::LoopContinue, None);
3434
analyzer.exec_statement(&node.body);
3535
analyzer.pop_cf_scope();
3636
});

crates/tree_shaker/src/nodes/stmt/for_statement.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ impl<'a> Analyzer<'a> {
2828
self.consumable(AstKind2::ForStatement(node))
2929
};
3030

31-
self.push_cf_scope_with_deps(CfScopeKind::Loop, vec![dep], Some(false));
31+
self.push_cf_scope_with_deps(CfScopeKind::LoopBreak, vec![dep], Some(false));
3232
self.exec_loop(move |analyzer| {
3333
if analyzer.cf_scope().must_exited() {
3434
return;
3535
}
3636

37-
analyzer.push_cf_scope(CfScopeKind::Loop, None);
37+
analyzer.push_cf_scope(CfScopeKind::LoopContinue, None);
3838
analyzer.exec_statement(&node.body);
3939
if let Some(update) = &node.update {
4040
analyzer.exec_expression(update);

crates/tree_shaker/src/nodes/stmt/while_statement.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ impl<'a> Analyzer<'a> {
1717

1818
let dep = self.consumable((AstKind2::WhileStatement(node), test));
1919

20-
self.push_cf_scope_with_deps(CfScopeKind::Loop, vec![dep], Some(false));
20+
self.push_cf_scope_with_deps(CfScopeKind::LoopBreak, vec![dep], Some(false));
2121
self.exec_loop(move |analyzer| {
22-
analyzer.push_cf_scope(CfScopeKind::Loop, None);
22+
analyzer.push_cf_scope(CfScopeKind::LoopContinue, None);
2323

2424
analyzer.exec_statement(&node.body);
2525
analyzer.exec_expression(&node.test).consume(analyzer);

crates/tree_shaker/src/scope/cf_scope.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ pub enum CfScopeKind<'a> {
2626
Module,
2727
Labeled(&'a LabeledStatement<'a>),
2828
Function,
29-
Loop,
29+
LoopBreak,
30+
LoopContinue,
3031
Switch,
3132

3233
Dependent,
@@ -41,11 +42,11 @@ impl<'a> CfScopeKind<'a> {
4142
}
4243

4344
pub fn is_breakable_without_label(&self) -> bool {
44-
matches!(self, CfScopeKind::Loop | CfScopeKind::Switch)
45+
matches!(self, CfScopeKind::LoopBreak | CfScopeKind::Switch)
4546
}
4647

4748
pub fn is_continuable(&self) -> bool {
48-
matches!(self, CfScopeKind::Loop)
49+
matches!(self, CfScopeKind::LoopContinue)
4950
}
5051

5152
pub fn matches_label(&self, label: &'a Atom<'a>) -> Option<&'a LabeledStatement<'a>> {

0 commit comments

Comments
 (0)