Commit 7278eff
Preserve trailing commas enum with members (#1750)
Preserve trailing commas for enums with members when commas are preserved (#1703)
This PR changes the formatter to have it maintain trailing commas of last enum constants when there are members.
So:
```dart
enum E { a, b, c,; int x; }
```
Becomes:
```dart
enum E {
a,
b,
c,
;
int x;
}
```
Instead of:
```dart
enum E {
a,
b,
c;
int x;
}
```
Fixes #1678
Fixes #1729
Additionally, a similar style choice was presented in #1660 (comment)
**Affected users**:
- Those passing the `trailing_commas: preserve` config option and adding a trailing comma to their enum cases.
- Users who don't already pass a trailing comma, aren't affected. So this PR shouldn't have undesirable effects on existing users.
**Benefits**:
- Maintains developer's intent
- Less churn when adding additional member cases
(Personally, I always add a trailing semicolon to avoid churn and to also make it even clearer this is an advanced enum with members.)
**Downsides**:
- 7 additional logical lines of code to maintain
**Additional considerations**:
I also considered implementing this for `enum E { a, b,; }`, however, that would require more drastic changes to the formatter as far as I can tell.
Additionally, in this case the trailing semicolon provides fewer benefits:
- churn when adding new members is the same if there was a trailing semicolon or not
- giving the enum other members also only amounts to new lines being added rather than changed so again, no real churn.
**Notes**
- One test was changed due to this being a change in behavior.
- This was surprisingly easy and enjoyable to implement (compliment to the existing code base).
- I didn't make any changes to the `CHANGELOG.md` since this trailing commas preservation hasn't shipped yet so its behaviour is still undefined.
Co-authored-by: jellynoone <[email protected]>1 parent 5d2ad56 commit 7278eff
File tree
5 files changed
+84
-20
lines changed- lib/src/front_end
- test/tall
- declaration
- preserve_trailing_commas
5 files changed
+84
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
6 | 35 | | |
7 | 36 | | |
8 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
672 | 673 | | |
673 | 674 | | |
674 | 675 | | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
675 | 686 | | |
| 687 | + | |
676 | 688 | | |
677 | 689 | | |
678 | 690 | | |
679 | 691 | | |
680 | | - | |
681 | | - | |
| 692 | + | |
| 693 | + | |
682 | 694 | | |
683 | 695 | | |
684 | 696 | | |
685 | 697 | | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
686 | 704 | | |
687 | 705 | | |
688 | 706 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
387 | 387 | | |
388 | 388 | | |
389 | 389 | | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
394 | 395 | | |
395 | 396 | | |
396 | | - | |
| 397 | + | |
397 | 398 | | |
398 | 399 | | |
399 | 400 | | |
| |||
404 | 405 | | |
405 | 406 | | |
406 | 407 | | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
418 | 417 | | |
419 | 418 | | |
420 | 419 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
54 | 63 | | |
55 | 64 | | |
56 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
38 | 47 | | |
39 | 48 | | |
40 | 49 | | |
| |||
0 commit comments