fix: Fix ast::IfExpr child accessors

This commit is contained in:
Lukas Wirth 2023-03-15 12:53:39 +01:00
parent 1787c14e72
commit 9fe206956f
3 changed files with 21 additions and 21 deletions

View file

@ -636,9 +636,8 @@ fn foo() {
if {} if {}
} }
"#, "#,
// the {} gets parsed as the condition, I think?
expect![[r#" expect![[r#"
fn foo () {if {} {}} fn foo () {if __ra_fixup {} {}}
"#]], "#]],
) )
} }

View file

@ -48,23 +48,30 @@ impl From<ast::IfExpr> for ElseBranch {
} }
impl ast::IfExpr { impl ast::IfExpr {
pub fn condition(&self) -> Option<ast::Expr> {
// If the condition is a BlockExpr, check if the then body is missing.
// If it is assume the condition is the expression that is missing instead.
let mut exprs = support::children(self.syntax());
let first = exprs.next();
match first {
Some(ast::Expr::BlockExpr(_)) => exprs.next().and(first),
first => first,
}
}
pub fn then_branch(&self) -> Option<ast::BlockExpr> { pub fn then_branch(&self) -> Option<ast::BlockExpr> {
self.children_after_condition().next() match support::children(self.syntax()).nth(1)? {
ast::Expr::BlockExpr(block) => Some(block),
_ => None,
}
} }
pub fn else_branch(&self) -> Option<ElseBranch> { pub fn else_branch(&self) -> Option<ElseBranch> {
let res = match self.children_after_condition().nth(1) { match support::children(self.syntax()).nth(2)? {
Some(block) => ElseBranch::Block(block), ast::Expr::BlockExpr(block) => Some(ElseBranch::Block(block)),
None => { ast::Expr::IfExpr(elif) => Some(ElseBranch::IfExpr(elif)),
let elif = self.children_after_condition().next()?; _ => None,
ElseBranch::IfExpr(elif) }
}
};
Some(res)
}
fn children_after_condition<N: AstNode>(&self) -> impl Iterator<Item = N> {
self.syntax().children().skip(1).filter_map(N::cast)
} }
} }

View file

@ -937,12 +937,6 @@ impl From<ast::Adt> for ast::Item {
} }
} }
impl ast::IfExpr {
pub fn condition(&self) -> Option<ast::Expr> {
support::child(&self.syntax)
}
}
impl ast::MatchGuard { impl ast::MatchGuard {
pub fn condition(&self) -> Option<ast::Expr> { pub fn condition(&self) -> Option<ast::Expr> {
support::child(&self.syntax) support::child(&self.syntax)