Skip to content

Commit 003cb15

Browse files
authored
Don't crash on an index cascade section followed by a method chain. (#1556)
Don't crash on an index cascade section followed by a method chain. I found this weird corner case in the wild when formatting a large corpus.
1 parent 7276774 commit 003cb15

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/src/front_end/chain_builder.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,13 @@ class ChainBuilder {
327327
});
328328
});
329329

330-
case IndexExpression():
331-
_unwrapPostfix(expression.target!, (target) {
330+
case IndexExpression(:var target?):
331+
// We check for a non-null target because the target may be `null` if
332+
// the chain we are building is itself in a cascade section that begins
333+
// with an index expression like:
334+
//
335+
// object..[index].chain();
336+
_unwrapPostfix(target, (target) {
332337
return _visitor.pieces.build(() {
333338
_visitor.pieces.add(target);
334339
_visitor.writeIndexExpression(expression);

test/tall/invocation/cascade_mixed.stmt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ object..cascade()!..cascade()[index]..cascade()(arg);
3636
object
3737
..cascade()!
3838
..cascade()[index]
39-
..cascade()(arg);
39+
..cascade()(arg);
40+
>>> Chain with index target.
41+
object..[index].method();
42+
<<<
43+
object..[index].method();

0 commit comments

Comments
 (0)