dart-lang / dart_style

An opinionated formatter/linter for Dart code
https://pub.dev/packages/dart_style
BSD 3-Clause "New" or "Revised" License
646 stars 121 forks source link

Prefer splitting at ".." instead of in the cascade target. #1427

Closed munificent closed 7 months ago

munificent commented 7 months ago

I'm working on migrating the regression tests and tweaking the new formatting heuristics as I go based on the results I see in those real-world examples.

In a method chain, if the target can be block formatted, we prefer to split in the target and not at the ".". So:

// Prefer:
[
  target,
  block,
].doStuff();

// Over:
[target, block]
    .doStuff();

Prior to this PR, it had the same priority for single-section cascades:

// Prefer:
[
  target,
  block,
]..doStuff();

// Over:
[target, block]
  ..doStuff();

But for cascades, I think the cascade operation is "looser" and generally looks better to put it on its own line and leave the target unsplit when possible. So this change flips priority and prefers:

[target, block]
  ..doStuff();

In larger more realistic examples, I think it looks better. In general, I think it's almost always better to move cascades onto their own lines. In fact, both the short and tall styles already have a rule that any cascade with more than one section will always unconditionally split every section even if they would otherwise fit. So I think this change is consistent with that.