Skip to content

Commit 5cb5627

Browse files
authored
Make preserved trailing commas apply to record types too. (#1751)
I missed this part of the language when I added support of preserving trailing commas. They should have always been preserved here, so I didn't language version this change. If someone has preserve trailing commas on but isn't on 3.10 yet, I think they will still want this change. Fix #1721.
1 parent a1e405f commit 5cb5627

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* Support dot shorthand syntax.
44
* Enable language version 3.10.
55

6+
### Bug fixes
7+
8+
* Preserved trailing commas (`trailing_commas: preserve`) applies to record
9+
type annotations too (#1721).
10+
611
### Style changes
712

813
This change only applies to code whose language version is 3.10 or higher:

lib/src/front_end/ast_node_visitor.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,9 +1814,13 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
18141814
var namedFields = node.namedFields;
18151815
var positionalFields = node.positionalFields;
18161816

1817-
// Single positional record types always have a trailing comma.
1817+
// Single positional record types always have a trailing comma and are not
1818+
// forced to split.
1819+
var isSinglePositional =
1820+
positionalFields.length == 1 && namedFields == null;
1821+
18181822
var listStyle =
1819-
positionalFields.length == 1 && namedFields == null
1823+
isSinglePositional
18201824
? const ListStyle(commas: Commas.alwaysTrailing)
18211825
: const ListStyle(commas: Commas.trailing);
18221826
var builder = DelimitedListBuilder(this, listStyle);
@@ -1849,7 +1853,13 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
18491853
}
18501854

18511855
builder.rightBracket(node.rightParenthesis, delimiter: rightDelimiter);
1852-
pieces.add(builder.build());
1856+
pieces.add(
1857+
builder.build(
1858+
forceSplit:
1859+
!isSinglePositional &&
1860+
hasPreservedTrailingComma(rightDelimiter ?? node.rightParenthesis),
1861+
),
1862+
);
18531863
pieces.token(node.question);
18541864
}
18551865

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
40 columns |
2+
(trailing_commas preserve)
3+
>>> Forces split with multiple positional fields and trailing comma.
4+
typedef R = (int,int,);
5+
<<< 3.7
6+
typedef R =
7+
(
8+
int,
9+
int,
10+
);
11+
<<< 3.8
12+
typedef R = (
13+
int,
14+
int,
15+
);
16+
>>> Doesn't force split with one positional field.
17+
typedef R = (int,);
18+
<<<
19+
typedef R = (int,);
20+
>>> Doesn't force split without trailing comma.
21+
typedef R = (int,int,int);
22+
<<<
23+
typedef R = (int, int, int);
24+
>>> Forces split with named fields and trailing comma.
25+
typedef R = ({int name,});
26+
<<< 3.7
27+
typedef R =
28+
({
29+
int name,
30+
});
31+
<<< 3.8
32+
typedef R = ({
33+
int name,
34+
});
35+
>>> May still split without trailing comma if doesn't fit.
36+
typedef R = (int element1, int element2, int element3);
37+
<<< 3.7
38+
typedef R =
39+
(
40+
int element1,
41+
int element2,
42+
int element3,
43+
);
44+
<<< 3.8
45+
typedef R = (
46+
int element1,
47+
int element2,
48+
int element3,
49+
);

test/tall/regression/1700/1721.unit

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(trailing_commas preserve)
2+
>>>
3+
typedef RawPhoto = ({DateTime addedAt, String name, String url,});
4+
<<< 3.8
5+
typedef RawPhoto = ({
6+
DateTime addedAt,
7+
String name,
8+
String url,
9+
});

0 commit comments

Comments
 (0)