Skip to content

Commit

Permalink
Don't crash on an index cascade section followed by a method chain. (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
munificent authored Sep 5, 2024
1 parent 7276774 commit 003cb15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/src/front_end/chain_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,13 @@ class ChainBuilder {
});
});

case IndexExpression():
_unwrapPostfix(expression.target!, (target) {
case IndexExpression(:var target?):
// We check for a non-null target because the target may be `null` if
// the chain we are building is itself in a cascade section that begins
// with an index expression like:
//
// object..[index].chain();
_unwrapPostfix(target, (target) {
return _visitor.pieces.build(() {
_visitor.pieces.add(target);
_visitor.writeIndexExpression(expression);
Expand Down
6 changes: 5 additions & 1 deletion test/tall/invocation/cascade_mixed.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ object..cascade()!..cascade()[index]..cascade()(arg);
object
..cascade()!
..cascade()[index]
..cascade()(arg);
..cascade()(arg);
>>> Chain with index target.
object..[index].method();
<<<
object..[index].method();

0 comments on commit 003cb15

Please sign in to comment.