Skip to content

Commit

Permalink
Handle trailing commas in for-loop updaters.
Browse files Browse the repository at this point in the history
Fix #1354.
  • Loading branch information
munificent committed Dec 6, 2024
1 parent 1208f9e commit 4dc2e97
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.0.1-wip

* Ensure comment formatting is idempotent (#1606).
* Handle trailing commas in for-loop updaters (#1354).

## 3.0.0

Expand Down
16 changes: 8 additions & 8 deletions lib/src/front_end/delimited_list_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,18 @@ final class DelimitedListBuilder {
_commentsBeforeComma = CommentSequence.empty;
}

/// Adds the contents of [lineBuilder] to this outer [DelimitedListBuilder].
/// Adds the contents of [inner] to this outer [DelimitedListBuilder].
///
/// This is used when preserving newlines inside a collection literal. The
/// [lineBuilder] will be used for the elements that should be packed onto a
/// single line, and this builder is for the rows that are each on their own
/// line.
void addLineBuilder(DelimitedListBuilder lineBuilder) {
/// This is used when a [DelimiterListBuilder] is building a piece that will
/// then become an element in a surrounding [DelimitedListBuilder]. It ensures
/// that any comments around a trailing comma after [inner] don't get lost and
/// are instead hoisted up to be captured by this builder.
void addInnerBuilder(DelimitedListBuilder inner) {
// Add the elements of the line to this builder.
add(lineBuilder.build());
add(inner.build());

// Make sure that any trailing comments on the line aren't lost.
_commentsBeforeComma = lineBuilder._commentsBeforeComma;
_commentsBeforeComma = inner._commentsBeforeComma;
}

/// Writes any comments appearing before [token] to the list.
Expand Down
15 changes: 12 additions & 3 deletions lib/src/front_end/piece_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,16 @@ mixin PieceFactory {
// The update clauses.
if (forParts.updaters.isNotEmpty) {
partsList.addCommentsBefore(forParts.updaters.first.beginToken);
partsList.add(createCommaSeparated(forParts.updaters));

// Create a nested list builder for the updaters so that they can
// remain unsplit even while the clauses split.
var updaterBuilder = DelimitedListBuilder(
this, const ListStyle(commas: Commas.nonTrailing));
forParts.updaters.forEach(updaterBuilder.visit);

// Add the updater builder to the clause builder so that any comments
// around a trailing comma after the updaters don't get dropped.
partsList.addInnerBuilder(updaterBuilder);
}

partsList.rightBracket(rightParenthesis);
Expand Down Expand Up @@ -1083,7 +1092,7 @@ mixin PieceFactory {
elements[i - 1].endToken, element.beginToken)) {
// This element begins a new line. Add the elements on the previous
// line to the list builder and start a new line.
builder.addLineBuilder(lineBuilder);
builder.addInnerBuilder(lineBuilder);
lineBuilder = DelimitedListBuilder(this, lineStyle);
atLineStart = true;
}
Expand All @@ -1099,7 +1108,7 @@ mixin PieceFactory {
}

// Finish the last line if there is anything on it.
if (!atLineStart) builder.addLineBuilder(lineBuilder);
if (!atLineStart) builder.addInnerBuilder(lineBuilder);
}

/// Writes a [VariablePiece] for a named or wildcard variable pattern.
Expand Down
14 changes: 14 additions & 0 deletions test/tall/expression/collection_for.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,18 @@ var list = [
inc
)
element,
];
>>> Trailing comma in increments.
var list = [
for (
x = 1;
true;
x += 1, x += 2,
)
element,
];
<<<
var list = [
for (x = 1; true; x += 1, x += 2)
element,
];
12 changes: 12 additions & 0 deletions test/tall/regression/1300/1354.stmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
>>>
void main() {
for (int i; i < 10; print("foo"), ++i, print("bar"),) {
break;
}
}
<<<
void main() {
for (int i; i < 10; print("foo"), ++i, print("bar")) {
break;
}
}
16 changes: 16 additions & 0 deletions test/tall/statement/for_comment.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ for (
) {
body;
}
>>> Preserve comments around discarded increment trailing comma.
for (
init;
cond;
incr /* c1 */ , /* c2 */
) {
body;
}
<<<
for (
init;
cond;
incr /* c1 */ /* c2 */
) {
body;
}
>>> Line comment before first `;` in fully empty clauses.
for ( // comment
; ; ) { body; }
Expand Down
17 changes: 16 additions & 1 deletion test/tall/statement/for_initializer.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,19 @@ for (
second = 2,
third = 3,
fourth = 4
) {}
) {}
>>> Discard trailing comma in unsplit increments.
for (foo; bar; first = 1, second = 2,) {}
<<<
for (foo; bar; first = 1, second = 2) {}
>>> Discard trailing comma in split increments.
for (foo; bar; first = 1, second = 2, third = 3, fourth = 4,) {}
<<<
for (
foo;
bar;
first = 1,
second = 2,
third = 3,
fourth = 4
) {}

0 comments on commit 4dc2e97

Please sign in to comment.