Skip to content

Commit 7db6e44

Browse files
authored
fix(fmt): handle trailing coments between base contracts (#12127)
* fix(fmt): account for ternary operators when estimating size * fix(fmt): handle comments between inherited base contracts * test: layout + base inheritance
1 parent 28caeb1 commit 7db6e44

File tree

5 files changed

+72
-12
lines changed

5 files changed

+72
-12
lines changed

crates/fmt/src/state/sol.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@ impl<'ast> State<'_, 'ast> {
288288
self.print_ident(name);
289289
self.nbsp();
290290

291+
if let Some(layout) = layout
292+
&& !self.handle_span(layout.span, false)
293+
{
294+
self.word("layout at ");
295+
self.print_expr(layout.slot);
296+
self.print_sep(Separator::Space);
297+
}
298+
291299
if let Some(first) = bases.first().map(|base| base.span())
292300
&& let Some(last) = bases.last().map(|base| base.span())
293301
&& self.inline_config.is_disabled(first.to(last))
@@ -296,26 +304,30 @@ impl<'ast> State<'_, 'ast> {
296304
} else if !bases.is_empty() {
297305
self.word("is");
298306
self.space();
299-
for (pos, base) in bases.iter().delimited() {
307+
let last = bases.len() - 1;
308+
for (i, base) in bases.iter().enumerate() {
300309
if !self.handle_span(base.span(), false) {
301310
self.print_modifier_call(base, false);
302-
if !pos.is_last {
311+
if i != last {
303312
self.word(",");
304-
self.space();
313+
if self
314+
.print_comments(
315+
bases[i + 1].span().lo(),
316+
CommentConfig::skip_ws().mixed_prev_space().mixed_post_nbsp(),
317+
)
318+
.is_none()
319+
{
320+
self.space();
321+
}
305322
}
306323
}
307324
}
308-
self.space();
325+
if !self.print_trailing_comment(bases.last().unwrap().span().hi(), None) {
326+
self.space();
327+
}
309328
self.s.offset(-self.ind);
310329
}
311330
self.end();
312-
if let Some(layout) = layout
313-
&& !self.handle_span(layout.span, false)
314-
{
315-
self.word("layout at ");
316-
self.print_expr(layout.slot);
317-
self.print_sep(Separator::Space);
318-
}
319331

320332
self.print_word("{");
321333
self.end();

crates/fmt/testdata/ContractDefinition/bracket-spacing.fmt.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,15 @@ contract ERC20DecimalsMock is ERC20 {
3939
_decimals = decimals_;
4040
}
4141
}
42+
43+
contract SomeContract is
44+
ERC165Upgradeable, // 1 inherited component
45+
ISomeContract // 4 inherited components
46+
{ }
47+
48+
contract AnotherContract is
49+
Adminable, /* 1 inherited components */
50+
UUPSUpgradeable /* 1 inherited component */
51+
{ }
52+
53+
contract WithLayoutAndBase layout at 69 is Base { }

crates/fmt/testdata/ContractDefinition/contract-new-lines.fmt.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ contract ERC20DecimalsMock is ERC20 {
5050
}
5151

5252
}
53+
54+
contract SomeContract is
55+
ERC165Upgradeable, // 1 inherited component
56+
ISomeContract // 4 inherited components
57+
{}
58+
59+
contract AnotherContract is
60+
Adminable, /* 1 inherited components */
61+
UUPSUpgradeable /* 1 inherited component */
62+
{}
63+
64+
contract WithLayoutAndBase layout at 69 is Base {}

crates/fmt/testdata/ContractDefinition/fmt.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ contract ERC20DecimalsMock is ERC20 {
4545
_decimals = decimals_;
4646
}
4747
}
48+
49+
contract SomeContract is
50+
ERC165Upgradeable, // 1 inherited component
51+
ISomeContract // 4 inherited components
52+
{}
53+
54+
contract AnotherContract is
55+
Adminable, /* 1 inherited components */
56+
UUPSUpgradeable /* 1 inherited component */
57+
{}
58+
59+
contract WithLayoutAndBase layout at 69 is Base {}

crates/fmt/testdata/ContractDefinition/original.sol

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ contract SampleContract {
1717
// comment 16
1818
external /* comment 17 */
1919
pure
20-
returns(uint256)
20+
returns(uint256)
2121
// comment 18
2222
{ // comment 19
2323
return arg1 > arg2 ? arg1 : arg2;
@@ -38,3 +38,15 @@ contract ERC20DecimalsMock is ERC20 {
3838
_decimals = decimals_;
3939
}
4040
}
41+
42+
contract SomeContract is
43+
ERC165Upgradeable, // 1 inherited component
44+
ISomeContract // 4 inherited components
45+
{ }
46+
47+
contract AnotherContract is
48+
Adminable, /* 1 inherited components */
49+
UUPSUpgradeable /* 1 inherited component */
50+
{ }
51+
52+
contract WithLayoutAndBase layout at 69 is Base {}

0 commit comments

Comments
 (0)