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): write files on disk only if they're not perfect match #8775

Merged
merged 4 commits into from
Aug 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading