Skip to content

Commit 6825ee5

Browse files
committed
Update CHANGELOG and test preserve trailing commas on 3.7.
Since the configuration has to be explicitly opted into, it seems worth supporting it in 3.7 code too.
1 parent 2af2bbc commit 6825ee5

21 files changed

+108
-113
lines changed

CHANGELOG.md

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
## 3.1.0-wip
22

3-
* Format null-aware elements.
3+
### Features
4+
5+
* Allow preserving trailing commas and forcing the surrounding construct to
6+
split even when it would otherwise fit on one line. This is off by default
7+
(because it breaks [reversibility][] among other reasons) but can be enabled
8+
by adding this to a surrounding `analysis_options.yaml` file:
9+
10+
```yaml
11+
formatter:
12+
trailing_commas: preserve
13+
```
14+
15+
This is similar to how trailing commas work in the old short style formatter
16+
applied to code before language version 3.7.
17+
18+
[reversibility]: https://github.com/dart-lang/dart_style/wiki/Reversibility-principle
19+
20+
### Bug fixes
21+
22+
* Don't add a trailing comma in lists that don't allow it, even when there is
23+
a trailing comment (#1639).
24+
25+
### Style changes
26+
27+
The following style changes are language versioned and only affect code whose
28+
language version is 3.8 or later. Dart code at 3.7 or earlier is formatted the
29+
same as it was before.
430
531
* Allow more code on the same line as a named argument or `=>` (#1536, #1545,
632
#1668, #1679).
@@ -175,26 +201,6 @@
175201
);
176202
```
177203

178-
* Allow preserving trailing commas and forcing the surrounding construct to
179-
split even when it would otherwise fit on one line. This is off by default
180-
(because it breaks [reversibility][] among other reasons) but can be enabled
181-
by adding this to a surrounding `analysis_options.yaml` file:
182-
183-
```yaml
184-
formatter:
185-
trailing_commas: preserve
186-
```
187-
188-
This is similar to how trailing commas worked in the old short style
189-
formatter.
190-
191-
* Don't add a trailing comma in lists that don't allow it, even when there is
192-
a trailing comment (#1639).
193-
194-
* Add tests for digit separators.
195-
196-
[reversibility]: https://github.com/dart-lang/dart_style/wiki/Reversibility-principle
197-
198204
## 3.0.1
199205

200206
* Handle trailing commas in for-loop updaters (#1354).

lib/src/front_end/piece_factory.dart

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -510,30 +510,20 @@ mixin PieceFactory {
510510
if (forParts.updaters.isNotEmpty) {
511511
partsList.addCommentsBefore(forParts.updaters.first.beginToken);
512512

513-
DelimitedListBuilder updaterBuilder;
514-
if (isVersion37) {
515-
// Create a nested list builder for the updaters so that they can
516-
// remain unsplit even while the clauses split.
517-
updaterBuilder = DelimitedListBuilder(
518-
this,
519-
const ListStyle(commas: Commas.nonTrailing),
520-
);
521-
} else {
522-
// Unlike most places in the language, if the updaters split, we
523-
// don't want to add a trailing comma. But if the user has preserve
524-
// trailing commas on, we should preserve the comma if there is one
525-
// but not add one if there isn't and it splits.
526-
var style = const ListStyle(commas: Commas.nonTrailing);
527-
if (formatter.trailingCommas == TrailingCommas.preserve &&
528-
rightParenthesis.hasCommaBefore) {
529-
style = const ListStyle(commas: Commas.trailing);
530-
}
531-
532-
// Create a nested list builder for the updaters so that they can
533-
// remain unsplit even while the clauses split.
534-
updaterBuilder = DelimitedListBuilder(this, style);
513+
// Unlike most places in the language, if the updaters split, we
514+
// don't want to add a trailing comma. But if the user has preserve
515+
// trailing commas on, we should preserve the comma if there is one
516+
// but not add one if there isn't and it splits.
517+
var style = const ListStyle(commas: Commas.nonTrailing);
518+
if (formatter.trailingCommas == TrailingCommas.preserve &&
519+
rightParenthesis.hasCommaBefore) {
520+
style = const ListStyle(commas: Commas.trailing);
535521
}
536522

523+
// Create a nested list builder for the updaters so that they can
524+
// remain unsplit even while the clauses split.
525+
var updaterBuilder = DelimitedListBuilder(this, style);
526+
537527
forParts.updaters.forEach(updaterBuilder.visit);
538528

539529
// Add the updater builder to the clause builder so that any comments
@@ -1793,7 +1783,6 @@ mixin PieceFactory {
17931783
/// Whether there is a trailing comma at the end of the list delimited by
17941784
/// [rightBracket] which should be preserved.
17951785
bool hasPreservedTrailingComma(Token rightBracket) =>
1796-
!isVersion37 &&
17971786
formatter.trailingCommas == TrailingCommas.preserve &&
17981787
rightBracket.hasCommaBefore;
17991788

test/tall/preserve_trailing_commas/collection_for.stmt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
list = [
55
for (x = 1;true;x += 1, x += 2,) e
66
];
7-
<<< 3.8
7+
<<<
88
list = [
99
for (
1010
x = 1;
@@ -18,7 +18,7 @@ list = [
1818
list = [
1919
for (x = 1;true;variable += longValue, another += value) e
2020
];
21-
<<< 3.8
21+
<<<
2222
list = [
2323
for (
2424
x = 1;

test/tall/preserve_trailing_commas/constructor.unit

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class A {
55
A(int x,);
66
}
7-
<<< 3.8
7+
<<<
88
class A {
99
A(
1010
int x,
@@ -14,15 +14,15 @@ class A {
1414
class A {
1515
A(int x, int y);
1616
}
17-
<<< 3.8
17+
<<<
1818
class A {
1919
A(int x, int y);
2020
}
2121
>>> May still split without trailing comma if doesn't fit.
2222
class A {
2323
A(int parameter1, int parameter2, int parameter3);
2424
}
25-
<<< 3.8
25+
<<<
2626
class A {
2727
A(
2828
int parameter1,
@@ -34,7 +34,7 @@ class A {
3434
class A {
3535
A(int x, [int y,]);
3636
}
37-
<<< 3.8
37+
<<<
3838
class A {
3939
A(
4040
int x, [
@@ -45,7 +45,7 @@ class A {
4545
class A {
4646
A(int x, {int y,});
4747
}
48-
<<< 3.8
48+
<<<
4949
class A {
5050
A(
5151
int x, {
@@ -57,7 +57,7 @@ class A {
5757
A(int x) : this.named(x,);
5858
A.named(int x) : this(x,);
5959
}
60-
<<< 3.8
60+
<<<
6161
class A {
6262
A(int x)
6363
: this.named(
@@ -73,7 +73,7 @@ class A {
7373
A(int x) : this.named(x);
7474
A.named(int x) : this(x);
7575
}
76-
<<< 3.8
76+
<<<
7777
class A {
7878
A(int x) : this.named(x);
7979
A.named(int x) : this(x);

test/tall/preserve_trailing_commas/enum.unit

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
(trailing_commas preserve)
33
>>> Forces split with trailing comma.
44
enum E {e,}
5-
<<< 3.8
5+
<<<
66
enum E {
77
e,
88
}
99
>>> Doesn't force split without trailing comma.
1010
enum E {e,f,g}
11-
<<< 3.8
11+
<<<
1212
enum E { e, f, g }
1313
>>> May still split without trailing comma if doesn't fit.
1414
enum E { value1, value2, value3, value4 }
15-
<<< 3.8
15+
<<<
1616
enum E {
1717
value1,
1818
value2,
@@ -21,13 +21,13 @@ enum E {
2121
}
2222
>>> Preserve trailing comma but remove semicolon if no members.
2323
enum E {e,;}
24-
<<< 3.8
24+
<<<
2525
enum E {
2626
e,
2727
}
2828
>>> Remove trailing comma and split if there are members.
2929
enum E { a, b, c,; int x; }
30-
<<< 3.8
30+
<<<
3131
enum E {
3232
a,
3333
b,
@@ -40,7 +40,7 @@ enum Args {
4040
value(a,b,),
4141
another(named:1,)
4242
}
43-
<<< 3.8
43+
<<<
4444
enum Args {
4545
value(
4646
a,

test/tall/preserve_trailing_commas/for.stmt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(trailing_commas preserve)
33
>>> Trailing comma in increments forces them to split.
44
for (x = 1;true;x += 1, x += 2,) {stmt;}
5-
<<< 3.8
5+
<<<
66
for (
77
x = 1;
88
true;
@@ -13,7 +13,7 @@ for (
1313
}
1414
>>> Don't add trailing comma if updaters split.
1515
for (x = 1;true;variable += longValue, another += value) {stmt;}
16-
<<< 3.8
16+
<<<
1717
for (
1818
x = 1;
1919
true;

test/tall/preserve_trailing_commas/function_call.stmt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
(trailing_commas preserve)
33
>>> Forces split with trailing comma.
44
function(1,);
5-
<<< 3.8
5+
<<<
66
function(
77
1,
88
);
99
>>> Doesn't force split without trailing comma.
1010
function(1,2,3);
11-
<<< 3.8
11+
<<<
1212
function(1, 2, 3);
1313
>>> May still split without trailing comma if doesn't fit.
1414
function(argument1, argument2, argument3);
15-
<<< 3.8
15+
<<<
1616
function(
1717
argument1,
1818
argument2,
1919
argument3,
2020
);
2121
>>> With named argument.
2222
function(name: 1,);
23-
<<< 3.8
23+
<<<
2424
function(
2525
name: 1,
2626
);

test/tall/preserve_trailing_commas/list_literal.stmt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
(trailing_commas preserve)
33
>>> Forces split with trailing comma.
44
[1,];
5-
<<< 3.8
5+
<<<
66
[
77
1,
88
];
99
>>> Doesn't force split without trailing comma.
1010
[1,2,3];
11-
<<< 3.8
11+
<<<
1212
[1, 2, 3];
1313
>>> May still split without trailing comma if doesn't fit.
1414
[element1, element2, element3, element4];
15-
<<< 3.8
15+
<<<
1616
[
1717
element1,
1818
element2,

test/tall/preserve_trailing_commas/list_pattern.stmt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
(trailing_commas preserve)
33
>>> Forces split with trailing comma.
44
var [x,] = list;
5-
<<< 3.8
5+
<<<
66
var [
77
x,
88
] = list;
99
>>> Doesn't force split without trailing comma.
1010
var [x,y,z] = list;
11-
<<< 3.8
11+
<<<
1212
var [x, y, z] = list;
1313
>>> May still split without trailing comma if doesn't fit.
1414
var [element1, element2, element3, element4] = list;
15-
<<< 3.8
15+
<<<
1616
var [
1717
element1,
1818
element2,

test/tall/preserve_trailing_commas/map_literal.stmt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
(trailing_commas preserve)
33
>>> Forces split with trailing comma.
44
map = {a:1,};
5-
<<< 3.8
5+
<<<
66
map = {
77
a: 1,
88
};
99
>>> Doesn't force split without trailing comma.
1010
map = {a:1,b:2,c:3};
11-
<<< 3.8
11+
<<<
1212
map = {a: 1, b: 2, c: 3};
1313
>>> May still split without trailing comma if doesn't fit.
1414
map = {a: value1, b: value2, c: value3, d: value4};
15-
<<< 3.8
15+
<<<
1616
map = {
1717
a: value1,
1818
b: value2,

0 commit comments

Comments
 (0)