Repository: casey/just
Pull request #2573 (2025-01-14 01:49:44)

Format `if … else if …` without superfluous braces


Patch:
diff --git a/src/expression.rs b/src/expression.rs
index c7c8eada..20b666b9 100644
--- a/src/expression.rs
+++ b/src/expression.rs
@@ -72,7 +72,13 @@ impl Display for Expression<'_> {
         condition,
         then,
         otherwise,
-      } => write!(f, "if {condition} {{ {then} }} else {{ {otherwise} }}"),
+      } => {
+        if let Self::Conditional { .. } = **otherwise {
+          write!(f, "if {condition} {{ {then} }} else {otherwise}")
+        } else {
+          write!(f, "if {condition} {{ {then} }} else {{ {otherwise} }}")
+        }
+      }
       Self::Group { contents } => write!(f, "({contents})"),
       Self::Join { lhs: None, rhs } => write!(f, "/ {rhs}"),
       Self::Join {
diff --git a/src/lexer.rs b/src/lexer.rs
index afef81c2..93bad7e0 100644
--- a/src/lexer.rs
+++ b/src/lexer.rs
@@ -259,7 +259,7 @@ impl<'src> Lexer<'src> {
 
   /// True if `text` could be an identifier
   pub(crate) fn is_identifier(text: &str) -> bool {
-    if !text.chars().next().map_or(false, Self::is_identifier_start) {
+    if !text.chars().next().is_some_and(Self::is_identifier_start) {
       return false;
     }
 
diff --git a/src/parser.rs b/src/parser.rs
index eea3e6c7..348032a0 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -942,7 +942,7 @@ impl<'run, 'src> Parser<'run, 'src> {
 
     let body = self.parse_body()?;
 
-    let shebang = body.first().map_or(false, Line::is_shebang);
+    let shebang = body.first().is_some_and(Line::is_shebang);
     let script = attributes.contains(AttributeDiscriminant::Script);
 
     if shebang && script {
@@ -1041,7 +1041,7 @@ impl<'run, 'src> Parser<'run, 'src> {
       }
     }
 
-    while lines.last().map_or(false, Line::is_empty) {
+    while lines.last().is_some_and(Line::is_empty) {
       lines.pop();
     }
 
diff --git a/src/recipe.rs b/src/recipe.rs
index d53a44bb..11365377 100644
--- a/src/recipe.rs
+++ b/src/recipe.rs
@@ -204,11 +204,11 @@ impl<'src, D> Recipe<'src, D> {
       }
       let mut evaluated = String::new();
       let mut continued = false;
-      let quiet_line = lines.peek().map_or(false, |line| line.is_quiet());
-      let infallible_line = lines.peek().map_or(false, |line| line.is_infallible());
+      let quiet_line = lines.peek().is_some_and(|line| line.is_quiet());
+      let infallible_line = lines.peek().is_some_and(|line| line.is_infallible());
 
       let comment_line = context.module.settings.ignore_comments
-        && lines.peek().map_or(false, |line| line.is_comment());
+        && lines.peek().is_some_and(|line| line.is_comment());
 
       loop {
         if lines.peek().is_none() {


Test patch:
diff --git a/tests/format.rs b/tests/format.rs
index 04d7d87e..93ccd996 100644
--- a/tests/format.rs
+++ b/tests/format.rs
@@ -1138,3 +1138,20 @@ fn unchanged_justfiles_are_not_written_to_disk() {
     .args(["--fmt", "--unstable"])
     .run();
 }
+
+#[test]
+fn if_else() {
+  Test::new()
+    .justfile(
+      "
+        x := if '' == '' { '' } else if '' == '' { '' } else { '' }
+      ",
+    )
+    .arg("--dump")
+    .stdout(
+      "
+        x := if '' == '' { '' } else if '' == '' { '' } else { '' }
+      ",
+    )
+    .run();
+}