dart-lang / dart_style

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

Prefer to split in `||` patterns in a switch expression instead of the value #1602

Open munificent opened 2 weeks ago

munificent commented 2 weeks ago

Currently, the formatter formats the following switch expression case like so:

    return switch (_advance().type) {
      TokenType.float || TokenType.int || TokenType.string => LiteralExpression(
        _previous.value!,
      ),
    };

We already have special formatting for || patterns in a switch expression to make them look sort of like a series of fallthrough cases in a switch statement. Given that, I think it makes sense to also tweak the splitting costs so that it prefers to split on the || instead of in the case expression. That way, you get:

    return switch (_advance().type) {
      TokenType.float ||
      TokenType.int ||
      TokenType.string => LiteralExpression(_previous.value!),
    };