Skip to content

Commit 3dbf85e

Browse files
authored
fix(fmt): refine logic over comments between uninformed commasep (#12055)
1 parent f085357 commit 3dbf85e

File tree

6 files changed

+72
-15
lines changed

6 files changed

+72
-15
lines changed

crates/fmt/src/state/common.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ impl<'ast> State<'_, 'ast> {
285285
return false;
286286
};
287287

288+
// If first item is uninformed (just a comma), and it has its own comment, skip it.
289+
// It will be dealt with when printing the item in the main loop of `commasep`.
290+
if span.is_dummy()
291+
&& let Some(next_pos) = values.get(1).map(|v| get_span(v).lo())
292+
&& self.peek_comment_before(next_pos).is_some()
293+
{
294+
return true;
295+
}
296+
288297
// Check for comments before the first item.
289298
if let Some((cmnt_span, cmnt_style)) =
290299
self.peek_comment_before(span.lo()).map(|c| (c.span, c.style))
@@ -406,7 +415,10 @@ impl<'ast> State<'_, 'ast> {
406415
self.hardbreak(); // trailing and isolated comments already hardbreak
407416
}
408417

409-
print(self, value);
418+
// Avoid printing the last uninformed item, so that we can handle line breaks.
419+
if !(is_last && get_span(value).is_dummy()) {
420+
print(self, value);
421+
}
410422

411423
let next_span = if is_last { None } else { Some(get_span(&values[i + 1])) };
412424
let next_pos = next_span.map(Span::lo).unwrap_or(pos_hi);
@@ -463,13 +475,9 @@ impl<'ast> State<'_, 'ast> {
463475
if let Some(next_span) = next_span
464476
&& !self.is_bol_or_only_ind()
465477
&& !self.inline_config.is_disabled(next_span)
478+
&& !next_span.is_dummy()
466479
{
467-
if next_span.is_dummy() && !matches!(format.kind, ListFormatKind::AlwaysBreak) {
468-
// Don't add spaces between uninformed items (commas)
469-
self.zerobreak();
470-
} else {
471-
format.print_break(false, values.len(), &mut self.s);
472-
}
480+
format.print_break(false, values.len(), &mut self.s);
473481
}
474482
}
475483

crates/fmt/src/state/mod.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,15 @@ impl<'sess> State<'sess, '_> {
546546
// Handle mixed with follow-up comment
547547
if cmnt.style.is_mixed() {
548548
if let Some(cmnt) = self.peek_comment_before(pos) {
549-
config.mixed_no_break = true;
549+
config.mixed_no_break_prev = true;
550+
config.mixed_no_break_post = true;
550551
config.mixed_post_nbsp = cmnt.style.is_mixed();
551552
}
552553

553554
// Ensure consecutive mixed comments don't have a double-space
554555
if last_style.is_some_and(|s| s.is_mixed()) {
555-
config.mixed_no_break = true;
556+
config.mixed_no_break_prev = true;
557+
config.mixed_no_break_post = true;
556558
config.mixed_prev_space = false;
557559
}
558560
} else if config.offset != 0
@@ -693,7 +695,7 @@ impl<'sess> State<'sess, '_> {
693695
let Some(prefix) = cmnt.prefix() else { return };
694696
let never_break = self.last_token_is_neverbreak();
695697
if !self.is_bol_or_only_ind() {
696-
match (never_break || config.mixed_no_break, config.mixed_prev_space) {
698+
match (never_break || config.mixed_no_break_prev, config.mixed_prev_space) {
697699
(false, true) => config.space(&mut self.s),
698700
(false, false) => config.zerobreak(&mut self.s),
699701
(true, true) => self.nbsp(),
@@ -721,7 +723,7 @@ impl<'sess> State<'sess, '_> {
721723
if config.mixed_post_nbsp {
722724
config.nbsp_or_space(self.config.wrap_comments, &mut self.s);
723725
self.cursor.advance(1);
724-
} else if !config.mixed_no_break {
726+
} else if !config.mixed_no_break_post {
725727
config.space(&mut self.s);
726728
self.cursor.advance(1);
727729
}
@@ -996,7 +998,8 @@ pub(crate) struct CommentConfig {
996998
// Config: mixed comments
997999
mixed_prev_space: bool,
9981000
mixed_post_nbsp: bool,
999-
mixed_no_break: bool,
1001+
mixed_no_break_prev: bool,
1002+
mixed_no_break_post: bool,
10001003
}
10011004

10021005
impl CommentConfig {
@@ -1020,7 +1023,8 @@ impl CommentConfig {
10201023
pub(crate) fn no_breaks(mut self) -> Self {
10211024
self.iso_no_break = true;
10221025
self.trailing_no_break = true;
1023-
self.mixed_no_break = true;
1026+
self.mixed_no_break_prev = true;
1027+
self.mixed_no_break_post = true;
10241028
self
10251029
}
10261030

@@ -1030,7 +1034,13 @@ impl CommentConfig {
10301034
}
10311035

10321036
pub(crate) fn mixed_no_break(mut self) -> Self {
1033-
self.mixed_no_break = true;
1037+
self.mixed_no_break_prev = true;
1038+
self.mixed_no_break_post = true;
1039+
self
1040+
}
1041+
1042+
pub(crate) fn mixed_no_break_post(mut self) -> Self {
1043+
self.mixed_no_break_post = true;
10341044
self
10351045
}
10361046

crates/fmt/src/state/sol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ impl<'ast> State<'_, 'ast> {
18231823
|this, var| match var {
18241824
SpannedOption::Some(var) => this.print_var(var, true),
18251825
SpannedOption::None(span) => {
1826-
this.print_comments(span.hi(), CommentConfig::skip_ws().no_breaks());
1826+
this.print_comments(span.hi(), CommentConfig::skip_ws().mixed_no_break_post());
18271827
}
18281828
},
18291829
|var| match var {

crates/fmt/testdata/SimpleComments/fmt.sol

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ contract SimpleComments {
6565
/* hashMod */
6666
) = _swapPre(2, TOTAL_SUPPLY / 1_000, false, zeroForOne1);
6767
}
68+
69+
// https://github.com/foundry-rs/foundry/issues/12045
70+
function test6() {
71+
(
72+
// uint80 roundID
73+
,
74+
int256 dataFeedAnswer,
75+
// uint startedAt
76+
,
77+
uint256 updatedAt,
78+
// uint80 answeredInRound
79+
) = dataFeedContract.latestRoundData();
80+
}
6881
}
6982

7083
/*

crates/fmt/testdata/SimpleComments/original.sol

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ contract SimpleComments {
6666
/* hashMod */
6767
) = _swapPre(2, TOTAL_SUPPLY / 1_000, false, zeroForOne1);
6868
}
69+
70+
// https://github.com/foundry-rs/foundry/issues/12045
71+
function test6() {
72+
(
73+
// uint80 roundID
74+
,
75+
int256 dataFeedAnswer,
76+
// uint startedAt
77+
,
78+
uint256 updatedAt,
79+
// uint80 answeredInRound
80+
) = dataFeedContract.latestRoundData();
81+
}
6982
}
7083

7184
/*

crates/fmt/testdata/SimpleComments/wrap-comments.fmt.sol

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ contract SimpleComments {
7878
2, TOTAL_SUPPLY / 1_000, false, zeroForOne1
7979
);
8080
}
81+
82+
// https://github.com/foundry-rs/foundry/issues/12045
83+
function test6() {
84+
(
85+
// uint80 roundID
86+
,
87+
int256 dataFeedAnswer,
88+
// uint startedAt
89+
,
90+
uint256 updatedAt,
91+
// uint80 answeredInRound
92+
) = dataFeedContract.latestRoundData();
93+
}
8194
}
8295

8396
/*

0 commit comments

Comments
 (0)