Skip to content

Commit

Permalink
Format if … else if … without superfluous braces (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 14, 2025
1 parent 2a3018a commit abec307
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit abec307

Please sign in to comment.