Skip to content

Commit

Permalink
fix(fmt): write files on disk only if they're not perfect match (#8775)
Browse files Browse the repository at this point in the history
* fix(fmt): write files on disk only if they're not perfect match

* Cleanup
  • Loading branch information
grandizzy committed Aug 30, 2024
1 parent 98ab45e commit 818eeb9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ impl<'a, W: Write> Formatter<'a, W> {
})?;

write_chunk!(self, "}}")?;
return Ok(true)
return Ok(false)
}

// Determine writable statements by excluding statements from disabled start / end lines.
Expand Down
3 changes: 1 addition & 2 deletions crates/fmt/testdata/Repros/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ contract IfElseTest {
number = 1;
} else if (newNumber = 2) {
// number = 2;
}
else {
} else {
newNumber = 3;
}
}
Expand Down
11 changes: 8 additions & 3 deletions crates/forge/bin/cmd/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,22 @@ impl FmtArgs {
)
})?;

let diff = TextDiff::from_lines(&source, &output);
let new_format = diff.ratio() < 1.0;
if self.check || path.is_none() {
if self.raw {
print!("{output}");
}

let diff = TextDiff::from_lines(&source, &output);
if diff.ratio() < 1.0 {
// If new format then compute diff summary.
if new_format {
return Ok(Some(format_diff_summary(&name, &diff)))
}
} else if let Some(path) = path {
fs::write(path, output)?;
// If new format then write it on disk.
if new_format {
fs::write(path, output)?;
}
}
Ok(None)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ contract Handler is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function thisFunctionReverts() external {
if (block.number < 10) {}
else revert();
if (block.number < 10) {} else {
revert();
}
}

function advanceTime(uint256 blocks) external {
Expand Down

0 comments on commit 818eeb9

Please sign in to comment.