Open FMorschel opened 2 weeks ago
Summary: The Dart switch expression conversion with OR operator removes comments associated with individual cases, leading to code that is less readable and harder to understand. The user suggests preserving comments by placing them after the OR operator, but this results in inconsistent formatting.
Issue created https://github.com/dart-lang/dart_style/issues/1551
This also inspired me to create https://github.com/dart-lang/linter/issues/5078.
Also:
return switch (value) {
0 || //case 0
1 || //case 1
2 //case 2
=>
2,
_ => 3,
};
Becomes:
switch (value) {
case 0 || //case 0
1 || //case 1
2:
return 2;
default:
return 3;
}
So we are still losing the last comment here.
So if you pick the first example and convert it twice you lose every comment there. Really bad if https://github.com/dart-lang/linter/issues/3673 ever happens.
Converting the following does keep the comments, but if converting back it loses the last comment as mentioned above:
switch (value) {
case 0 || //case 0
1 || //case 1
2: // case 2
return 2;
default:
return 3;
}
Just point it out so tests can also be added for this case.
Thanks for all of these examples @FMorschel!
/fyi @scheglov (who fixed the associated https://github.com/dart-lang/sdk/issues/54567)
NP!
He had already seen this issue since he added the priority.
Also, something to keep in mind would be https://github.com/dart-lang/sdk/issues/56602 because as you can see in the first example, it doesn't format the output. I completely ignored it first since it is so easy to format in the IDE but my colleague pointed that when fixing via CLI with the possible future lint this would not be expected.
From an example posted by @pq in https://github.com/dart-lang/linter/issues/3673#issuecomment-1242203656:
Turns into the following losing all comments:
After formatted:
A smaller example:
Turns into:
I believe it should become something along the lines of:
But I think the result being alone is weird in all of the above so I suggest doing:
Currently, in the formatter, if you have the above, it formats the
=>
in one line and the result in a new one. Will file an issue there and link here.