dart-lang / dart_style

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

Tall style: Put multiple assignments on the same line #1536

Open nex3 opened 1 month ago

nex3 commented 1 month ago

Rather than

void main() {
  DeprecationProcessingLogger deprecationLogger =
      logger = DeprecationProcessingLogger(
        logger ?? Logger.stderr(),
        silenceDeprecations: {...?silenceDeprecations},
        fatalDeprecations: {...?fatalDeprecations},
        futureDeprecations: {...?futureDeprecations},
        limitRepetition: !verbose,
      );
}

I'd prefer

void main() {
  DeprecationProcessingLogger deprecationLogger = logger =
      DeprecationProcessingLogger(
        logger ?? Logger.stderr(),
        silenceDeprecations: {...?silenceDeprecations},
        fatalDeprecations: {...?fatalDeprecations},
        futureDeprecations: {...?futureDeprecations},
        limitRepetition: !verbose,
      );
}

While the AST thinks of the second assignment as an expression, as a user I think of it as an addendum to the LHS, and so I expect it to bind more tightly there than to the value of the assignment.

lrhn commented 3 weeks ago

It feels like there is a kind of expression that has a a header and a body, and it's OK to keep the header on the same line as something else, and move the body to new lines. (Making up terminology on the spot.)

That's what collection literals do:

List<Map<BigType, LongType> longName = <Map<BigType, LongType>[
     element1,
     element2,
];

The expression <Map<...>[....] can be split over multiple lines while keeping the header (<Map<...>>[) on the same line as other context. It's probably even preferred to split that way, if possible.

This issue is suggesting that the LHS and = of an assignment can be such a header, and that it's preferred to keep it on the same line as something else, if possible.

Another example could be:

var banana = longFunctionName("args", target =
    biggerAssignedExpression(...));

(Although that may just put every argument on its own line.)