Skip to content

Commit 21fae5e

Browse files
authored
fix(fmt): preserve format of disabled item (#8633)
1 parent a520115 commit 21fae5e

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

crates/fmt/src/formatter.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,26 @@ impl<'a, W: Write> Formatter<'a, W> {
405405
while let Some((loc, item)) = items.next() {
406406
let chunk_next_byte_offset =
407407
items.peek().map(|(loc, _)| loc.start()).or(next_byte_offset);
408-
out.push(self.visit_to_chunk(loc.start(), chunk_next_byte_offset, item)?);
408+
409+
let chunk = if self.inline_config.is_disabled(loc) {
410+
// If item format is disabled, we determine last disabled line from item and create
411+
// chunk with raw src.
412+
let mut disabled_loc = loc;
413+
self.chunked(disabled_loc.start(), chunk_next_byte_offset, |fmt| {
414+
while fmt.inline_config.is_disabled(disabled_loc) {
415+
if let Some(next_line) = fmt.find_next_line(disabled_loc.end()) {
416+
disabled_loc = disabled_loc.with_end(next_line);
417+
} else {
418+
break;
419+
}
420+
}
421+
fmt.write_raw_src(disabled_loc)?;
422+
Ok(())
423+
})?
424+
} else {
425+
self.visit_to_chunk(loc.start(), chunk_next_byte_offset, item)?
426+
};
427+
out.push(chunk);
409428
}
410429
Ok(out)
411430
}

crates/fmt/testdata/Repros/fmt.sol

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,28 @@ contract TestContract {
106106
return true;
107107
}
108108
}
109+
110+
// https://github.com/foundry-rs/foundry/issues/5825
111+
library MyLib {
112+
bytes32 private constant TYPE_HASH = keccak256(
113+
// forgefmt: disable-start
114+
"MyStruct("
115+
"uint8 myEnum,"
116+
"address myAddress"
117+
")"
118+
// forgefmt: disable-end
119+
);
120+
121+
bytes32 private constant TYPE_HASH_1 = keccak256(
122+
"MyStruct(" "uint8 myEnum," "address myAddress" ")" // forgefmt: disable-line
123+
);
124+
125+
// forgefmt: disable-start
126+
bytes32 private constant TYPE_HASH_2 = keccak256(
127+
"MyStruct("
128+
"uint8 myEnum,"
129+
"address myAddress"
130+
")"
131+
);
132+
// forgefmt: disable-end
133+
}

crates/fmt/testdata/Repros/original.sol

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,28 @@ contract TestContract {
104104
}
105105
return true ; }
106106
}
107+
108+
// https://github.com/foundry-rs/foundry/issues/5825
109+
library MyLib {
110+
bytes32 private constant TYPE_HASH = keccak256(
111+
// forgefmt: disable-start
112+
"MyStruct("
113+
"uint8 myEnum,"
114+
"address myAddress"
115+
")"
116+
// forgefmt: disable-end
117+
);
118+
119+
bytes32 private constant TYPE_HASH_1 = keccak256(
120+
"MyStruct(" "uint8 myEnum," "address myAddress" ")" // forgefmt: disable-line
121+
);
122+
123+
// forgefmt: disable-start
124+
bytes32 private constant TYPE_HASH_2 = keccak256(
125+
"MyStruct("
126+
"uint8 myEnum,"
127+
"address myAddress"
128+
")"
129+
);
130+
// forgefmt: disable-end
131+
}

0 commit comments

Comments
 (0)