You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the RHS of => is a function call, prefer to split at the =>. (#1523)
We use the same AssignPiece rule for `=`, `=>`, and `:`. And that rule uniformly handles all kinds of block-formattable right hand sides: collection literals, switch expressions, and function calls. For the most part, that works well and provides nice consistent formatting. Users generally like:
```dart
// Better:
variable = [
long,
list,
literal,
];
// Worse:
variable =
[long, list, literal];
```
And:
```dart
// Better:
SomeWidget(
children: [
long,
list,
literal,
],
);
// Worse:
SomeWidget(
children:
[long, list, literal],
);
```
And (with somewhat less consensus):
```dart
// Better:
variable = function(
long,
argument,
list,
);
// Worse:
variable =
function(long, argument, list);
```
Also, users have long requested and seem to like:
```dart
// Better:
class C {
List<String> makeStuff() => [
long,
list,
literal,
];
}
// Worse:
class C {
List<String> makeStuff() =>
[long, list, literal];
}
```
So based on all that, I just used uniform rules for all combinations of assignment constructs and delimited constructs. That means that this behavior falls out implicitly:
```dart
// Better:
class C {
String doThing() => function(
long,
argument,
list,
);
}
// Worse:
class C {
String doThing() =>
function(long, argument, list);
}
```
But it turns out that that particular combination of `=>` with a function call on the right doesn't get the same user sentiment. Instead, most (including me) prefer:
```dart
class C {
String doThing() =>
function(long, argument, list);
}
```
I think it's because this keeps the function name next to its arguments. With the other block-like forms: list literals, etc. there isn't really anything particularly interesting going on in the opening delimiter, so it makes sense to keep it on the same line as the `=>` since it's pretty much just punctuation. But a function call is a single coherent operation including the function and its arguments.
So this PR tweaks the cost rule for AssignPiece. When the operator is `=>` and the RHS is a function call, we prefer to split at the `=>` if that lets the RHS stay unsplit.
0 commit comments