Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: set up if #3110

Closed
wants to merge 9 commits into from
14 changes: 14 additions & 0 deletions tooling/nargo_fmt/src/visitor/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ impl FmtVisitor<'_> {
self.format_expr(infix.rhs)
)
}
ExpressionKind::If(if_expr) => {
let condition_str = self.format_expr(if_expr.condition);
let consequence_str = self.format_expr(if_expr.consequence);

if let Some(alternative_expr) = &if_expr.alternative {
let alternative_str = self.format_expr(alternative_expr.clone());
format!(
"if {} {{\n {}\n}} else {{\n {}\n}}",
condition_str, consequence_str, alternative_str
)
} else {
format!("if {} {{\n {}\n}}", condition_str, consequence_str)
}
}
ExpressionKind::MemberAccess(member_access_expr) => {
let lhs_str = self.format_expr(member_access_expr.lhs);
format!("{}.{}", lhs_str, member_access_expr.rhs)
Expand Down
11 changes: 11 additions & 0 deletions tooling/nargo_fmt/tests/expected/if.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
if true {
println!("True branch");
}

if x > 5 {
println!("X is greater than 5");
} else {
println!("X is not greater than 5");
}
}
8 changes: 8 additions & 0 deletions tooling/nargo_fmt/tests/input/if.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
if true { println!("True branch");}

if x > 5 {println!("X is greater than 5");} else
{
println!("X is not greater than 5");
}
}
Loading