Skip to content

Commit 2aa26f6

Browse files
committed
fix: Fix ast::IfExpr child accessors
1 parent 1787c14 commit 2aa26f6

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

crates/syntax/src/ast/expr_ext.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,24 @@ impl From<ast::IfExpr> for ElseBranch {
4949

5050
impl ast::IfExpr {
5151
pub fn then_branch(&self) -> Option<ast::BlockExpr> {
52-
self.children_after_condition().next()
52+
let mut exprs = support::children(self.syntax());
53+
let first = exprs.next();
54+
let second = exprs.next();
55+
second.or(first)
5356
}
5457

5558
pub fn else_branch(&self) -> Option<ElseBranch> {
56-
let res = match self.children_after_condition().nth(1) {
57-
Some(block) => ElseBranch::Block(block),
58-
None => {
59-
let elif = self.children_after_condition().next()?;
60-
ElseBranch::IfExpr(elif)
61-
}
62-
};
63-
Some(res)
59+
let mut exprs = support::children(self.syntax()).nth(2)?;
60+
61+
match support::children(self.syntax()).nth(2)? {
62+
ast::Expr::BlockExpr(block) => Some(ElseBranch::Block(block)),
63+
ast::Expr::IfExpr(elif) => Some(ElseBranch::IfExpr(elif)),
64+
_ => None,
65+
}
6466
}
6567

6668
fn children_after_condition<N: AstNode>(&self) -> impl Iterator<Item = N> {
67-
self.syntax().children().skip(1).filter_map(N::cast)
69+
self.syntax().children().filter_map(N::cast).skip(1)
6870
}
6971
}
7072

crates/syntax/src/ast/node_ext.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,14 @@ impl From<ast::Adt> for ast::Item {
939939

940940
impl ast::IfExpr {
941941
pub fn condition(&self) -> Option<ast::Expr> {
942-
support::child(&self.syntax)
942+
// If the condition is a BlockExpr, check if the then body is missing.
943+
// If it is assume the condition is the expression that is missing instead.
944+
let mut exprs = support::children(self.syntax());
945+
let first = exprs.next();
946+
match first {
947+
Some(ast::Expr::BlockExpr(_)) => exprs.next().and(first),
948+
first => first,
949+
}
943950
}
944951
}
945952

0 commit comments

Comments
 (0)