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

fix(fmt): multiline ternary expression in assignment #3289

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 10 additions & 15 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,7 @@ impl<'a, W: Write> Formatter<'a, W> {
self.write_postfix_comments_before(expr.loc().start())?;
self.write_prefix_comments_before(expr.loc().start())?;

let fits_on_single_line =
self.try_on_single_line(|fmt| fmt.indented(1, |fmt| expr.visit(fmt)))?;
if self.is_beginning_of_line() && fits_on_single_line {
if self.try_on_single_line(|fmt| fmt.indented(1, |fmt| expr.visit(fmt)))? {
return Ok(())
}

Expand Down Expand Up @@ -1954,26 +1952,23 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
self.visit_assignment(right)?;
}
Expression::Ternary(loc, cond, first_expr, second_expr) => {
let mut chunks = vec![];
cond.visit(self)?;

chunks.push(
self.chunked(loc.start(), Some(first_expr.loc().start()), |fmt| {
cond.visit(fmt)
})?,
);
chunks.push(self.chunked(
let first_expr = self.chunked(
first_expr.loc().start(),
Some(second_expr.loc().start()),
|fmt| {
write_chunk!(fmt, "?")?;
first_expr.visit(fmt)
},
)?);
chunks.push(self.chunked(second_expr.loc().start(), Some(loc.end()), |fmt| {
write_chunk!(fmt, ":")?;
second_expr.visit(fmt)
})?);
)?;
let second_expr =
self.chunked(second_expr.loc().start(), Some(loc.end()), |fmt| {
write_chunk!(fmt, ":")?;
second_expr.visit(fmt)
})?;

let chunks = vec![first_expr, second_expr];
if !self.try_on_single_line(|fmt| fmt.write_chunks_separated(&chunks, "", false))? {
self.grouped(|fmt| fmt.write_chunks_separated(&chunks, "", true))?;
}
Expand Down
14 changes: 14 additions & 0 deletions fmt/testdata/TernaryExpression/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,19 @@ contract TernaryExpression {
// comment6
// comment7
: 0; // comment8

uint256 amount = msg.value > 0
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint256 amount = msg.value > 0
? msg.value
// comment9
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint256 amount = msg.value > 0
// comment10
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);
}
}
14 changes: 14 additions & 0 deletions fmt/testdata/TernaryExpression/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ contract TernaryExpression {
:
// comment7
0; // comment8

uint256 amount = msg.value > 0
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint256 amount = msg.value > 0
? msg.value
// comment9
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint amount = msg.value > 0
// comment10
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);
}
}