Skip to content

Commit

Permalink
Don't crash when formatting an empty source file.
Browse files Browse the repository at this point in the history
(Strangely, yes, I found several of these in the wild on pub.)
  • Loading branch information
munificent committed Aug 14, 2024
1 parent 8c0e44e commit f9a5617
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/src/front_end/sequence_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ class SequenceBuilder {
// BlockPiece.
if (_elements.isEmpty) {
return _visitor.pieces.build(() {
_visitor.pieces.add(_leftBracket!);
if (forceSplit) {
if (_leftBracket case var bracket?) _visitor.pieces.add(bracket);

if (forceSplit || _leftBracket == null) {
_visitor.pieces.add(NewlinePiece());
}
_visitor.pieces.add(_rightBracket!);

if (_rightBracket case var bracket?) _visitor.pieces.add(bracket);
});
}

Expand Down
6 changes: 6 additions & 0 deletions test/tall/top_level/whitespace.unit
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
40 columns |
>>> Empty compilation unit.



<<<

>>> Force at least one newline between directives.
import 'a.dart';import 'b.dart';export 'c.dart';
<<<
Expand Down

0 comments on commit f9a5617

Please sign in to comment.