Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

* Format null-aware elements.

* Avoid splitting chains containing only properties.

```dart
// Before:
variable = target
.property
.another;

// After:
variable =
target.property.another;
```

Note that this only applies to `.` chains that are only properties. If there
are method calls in the chain, then it prefers to split the chain instead of
splitting at `=`, `:`, or `=>`.

* Allow the target or property chain part of a split method chain on the RHS of
`=`, `:`, and `=>` (#1466).

Expand Down
40 changes: 27 additions & 13 deletions lib/src/piece/chain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,33 @@ final class ChainPiece extends Piece {

@override
int stateCost(State state) {
// If the chain is a cascade, lower the cost so that we prefer splitting
// the cascades instead of the target. Prefers:
//
// [element1, element2]
// ..cascade();
//
// Over:
//
// [
// element1,
// element2,
// ]..cascade();
if (state == State.split) return _isCascade ? 0 : 1;
if (state == State.split) {
// If the chain is a cascade, lower the cost so that we prefer splitting
// the cascades instead of the target. Prefers:
//
// [element1, element2]
// ..cascade();
//
// Over:
//
// [
// element1,
// element2,
// ]..cascade();
if (_isCascade) return 0;

// If the chain is only properties, try to keep them together. Prefers:
//
// variable =
// target.property.another;
//
// Over:
//
// variable = target
// .property
// .another;
if (_leadingProperties == _calls.length) return 2;
}

return super.stateCost(state);
}
Expand Down
20 changes: 19 additions & 1 deletion test/tall/invocation/chain_property.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,22 @@ avian
.equine
.feline
.piscine
.method();
.method();
>>> Prefer splitting an assignment over splitting a pure property chain.
variable = avian.bovine.canine.equine.feline;
<<<
variable =
avian.bovine.canine.equine.feline;
>>> Don't prefer splitting an assignment if there are methods in the chain.
variable = avian.bovine.canine.equine.feline();
<<<
variable = avian.bovine.canine.equine
.feline();
>>> Don't prefer splitting an assignment if there are methods in the chain.
variable = avian.bovine().canine.equine().feline;
<<<
variable = avian
.bovine()
.canine
.equine()
.feline;