Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

feat: Improve control flow handling #652

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions crates/stc_ts_file_analyzer/src/analyzer/stmt/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ enum ForHeadKind {
Of { is_awaited: bool },
}

#[derive(Debug, Clone, Copy)]
enum LoopKind {
While,
DoWhile,
ForIn,
ForOf { is_awaited: bool },
}

impl Analyzer<'_, '_> {
/// We evaluate loop bodies multiple time.
/// But actually we don't report errors
///
/// If type does not change due to a loop, we evaluate
fn validate_loop_body_with_scope(&mut self, test: Option<&RExpr>, body: &RStmt) -> VResult<()> {
fn validate_loop_body_with_scope(&mut self, test: Option<&RExpr>, body: &RStmt, kind: LoopKind) -> VResult<()> {
let mut orig_facts = self.cur_facts.take();

let mut prev_facts = orig_facts.true_facts.take();
Expand All @@ -42,10 +50,8 @@ impl Analyzer<'_, '_> {
let mut orig_vars = Some(self.scope.vars.clone());

loop {
let mut facts_from_body: CondFacts = self.with_child_with_hook(
ScopeKind::LoopBody { last },
prev_facts.clone(),
|child: &mut Analyzer| {
let facts_from_cond: CondFacts =
self.with_child(ScopeKind::LoopBody { last }, prev_facts.clone(), |child: &mut Analyzer| {
child.ctx.ignore_errors |= !last;

{
Expand All @@ -56,6 +62,18 @@ impl Analyzer<'_, '_> {
test.visit_with(&mut *child.with_ctx(ctx));
}

Ok(child.cur_facts.true_facts.take())
})?;

let mut facts_for_body = prev_facts.clone();
facts_for_body += facts_from_cond.clone();

let mut facts_from_body: CondFacts = self.with_child_with_hook(
ScopeKind::LoopBody { last },
facts_from_cond.clone(),
|child: &mut Analyzer| {
child.ctx.ignore_errors |= !last;

body.visit_with(child);

Ok(child.cur_facts.true_facts.take())
Expand All @@ -70,7 +88,22 @@ impl Analyzer<'_, '_> {
facts_from_body.excludes.clear();

if last {
prev_facts += facts_from_body;
match kind {
LoopKind::DoWhile => {
prev_facts += facts_from_body;
prev_facts += facts_from_cond;
}

LoopKind::While => {
prev_facts += facts_from_cond;
prev_facts += facts_from_body;
}

_ => {
prev_facts += facts_from_body;
}
}

break;
}

Expand Down Expand Up @@ -373,7 +406,16 @@ impl Analyzer<'_, '_> {

child.validate_lhs_of_for_loop(left, &elem_ty, kind);

child.validate_loop_body_with_scope(None, body).report(&mut child.storage);
child
.validate_loop_body_with_scope(
None,
body,
match kind {
ForHeadKind::Of { is_awaited } => LoopKind::ForOf { is_awaited },
ForHeadKind::In => LoopKind::ForIn,
},
)
.report(&mut child.storage);

Ok(())
})?;
Expand Down Expand Up @@ -409,7 +451,7 @@ impl Analyzer<'_, '_> {
#[validator]
impl Analyzer<'_, '_> {
fn validate(&mut self, node: &RWhileStmt) {
self.validate_loop_body_with_scope(Some(&node.test), &node.body)
self.validate_loop_body_with_scope(Some(&node.test), &node.body, LoopKind::While)
.report(&mut self.storage);

Ok(())
Expand All @@ -421,7 +463,7 @@ impl Analyzer<'_, '_> {
fn validate(&mut self, node: &RDoWhileStmt) {
node.body.visit_with(self);

self.validate_loop_body_with_scope(Some(&node.test), &node.body)
self.validate_loop_body_with_scope(Some(&node.test), &node.body, LoopKind::DoWhile)
.report(&mut self.storage);

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ let obj: any;

x = true;
(x = "", obj).foo = (x = x.length);
x; // number

declare function assertNumber(n: number): void;
assertNumber(x)

export { }
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ do {
x; // number
x = "";
} while (x = x.length)
x; // number
declare function assertNumber(n: number): void;
assertNumber(x)