Skip to content

Commit 92b2fa8

Browse files
authored
Don't crash when formatting an empty source file. (#1524)
(Strangely, yes, I found several of these in the wild on pub.)
1 parent dd1f7d9 commit 92b2fa8

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/src/front_end/sequence_builder.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ class SequenceBuilder {
5353
// BlockPiece.
5454
if (_elements.isEmpty) {
5555
return _visitor.pieces.build(() {
56-
_visitor.pieces.add(_leftBracket!);
57-
if (forceSplit) {
56+
if (_leftBracket case var bracket?) _visitor.pieces.add(bracket);
57+
58+
if (forceSplit || _leftBracket == null) {
5859
_visitor.pieces.add(NewlinePiece());
5960
}
60-
_visitor.pieces.add(_rightBracket!);
61+
62+
if (_rightBracket case var bracket?) _visitor.pieces.add(bracket);
6163
});
6264
}
6365

test/tall/top_level/whitespace.unit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
40 columns |
2+
>>> Empty compilation unit.
3+
4+
5+
6+
<<<
7+
28
>>> Force at least one newline between directives.
39
import 'a.dart';import 'b.dart';export 'c.dart';
410
<<<

0 commit comments

Comments
 (0)