-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format
||
patterns like fallthrough cases in switch expressions. (#…
…1620) Switch statements allow multiple cases to share a body like: ```dart switch (obj) { case pattern1: case pattern2: body; } ``` Switch expressions don't support that, but `||` patterns are the idiomatic way to accomplish the same thing. Because of that, the formatter has some special formatting when the outermost pattern in a switch expression case is `||`: ```dart x = switch (obj) { pattern1 || pattern2 => body, }; ``` Note how the `pattern2` operand isn't indented. This PR extends that special handling to allow the `=>` on the same line as the `=>` even if the pattern is a split `||` pattern, like: ```dart x = switch (obj) { pattern1 || pattern2 => body, }; ``` And it prefers to split the `||` over the body when the body is block formatted: ```dart // Prefer: x = switch (obj) { pattern1 || pattern2 => function(argument), }; // Over: x = switch (obj) { pattern1 || pattern2 => function( argument, ), }; ``` This is one of those rules that's mostly a matter of taste, but I ran this on a large corpus and most of the diffs look better to me. Here are a few examples: ```dart // Before: typeName = switch (targetType) { DriftSqlType.int || DriftSqlType.bigInt || DriftSqlType.bool => 'INTEGER', DriftSqlType.string => 'CHAR', DriftSqlType.double => 'DOUBLE', DriftSqlType.blob => 'BINARY', DriftSqlType.dateTime => 'DATETIME', DriftSqlType.any => '', CustomSqlType() || DialectAwareSqlType() => targetType.sqlTypeName( context, ), }; // After: typeName = switch (targetType) { DriftSqlType.int || DriftSqlType.bigInt || DriftSqlType.bool => 'INTEGER', DriftSqlType.string => 'CHAR', DriftSqlType.double => 'DOUBLE', DriftSqlType.blob => 'BINARY', DriftSqlType.dateTime => 'DATETIME', DriftSqlType.any => '', CustomSqlType() || DialectAwareSqlType() => targetType.sqlTypeName(context), }; // Before: return switch (side) { AxisSide.right || AxisSide.left => titlesPadding.vertical + borderPadding.vertical, AxisSide.top || AxisSide.bottom => titlesPadding.horizontal + borderPadding.horizontal, }; // After: return switch (side) { AxisSide.right || AxisSide.left => titlesPadding.vertical + borderPadding.vertical, AxisSide.top || AxisSide.bottom => titlesPadding.horizontal + borderPadding.horizontal, }; // Before: final defaultConstraints = switch (side) { ShadSheetSide.top || ShadSheetSide.bottom => BoxConstraints( minWidth: mSize.width, ), ShadSheetSide.left || ShadSheetSide.right => BoxConstraints( minHeight: mSize.height, ), }; final defaultConstraints = switch (side) { ShadSheetSide.top || ShadSheetSide.bottom => BoxConstraints(minWidth: mSize.width), ShadSheetSide.left || ShadSheetSide.right => BoxConstraints(minHeight: mSize.height), }; ``` Fix #1602.
- Loading branch information
1 parent
47bcc07
commit 7c4a960
Showing
6 changed files
with
69 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
>>> (indent 4) | ||
return switch (_advance().type) { | ||
TokenType.float || TokenType.int || TokenType.string => LiteralExpression( | ||
_previous.value!, | ||
), | ||
}; | ||
<<< | ||
return switch (_advance().type) { | ||
TokenType.float || | ||
TokenType.int || | ||
TokenType.string => LiteralExpression(_previous.value!), | ||
}; |