Skip to content

Commit a1fcb10

Browse files
authored
Force a cascade to split if an indirect method call section splits. (#1509)
We always split a cascade if it has multiple sections. We don't require it to split if it has only one: ```dart cascadeTarget..method(argument); ``` We also don't require it to split if the section is a method call whose argument list splits: ```dart cascadeTarget..method( argument, ); ``` But the section might not be an immediate method call like `method()` here. It could be some longer call chain as in: ``` cascadeTarget..some.property.chain.method(argument); ``` In that case, if the ultimate trailing argument list splits, I think it's better to force the cascade section to split too: ``` cascadeTarget..some.property.chain.method( argument, ); // Better: cascadeTarget ..some.property.chain.method( argument, ); ``` Otherwise, I think the preceding call chain gets sort of buried in the line with the cascade target. This makes that change.
1 parent 76ae5ef commit a1fcb10

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

lib/src/front_end/chain_builder.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,25 @@ class ChainBuilder {
7878
var piece = _visitor.nodePiece(section);
7979

8080
var callType = switch (section) {
81+
// If the section is itself a method chain, then force the cascade to
82+
// split if the method does, as in:
83+
//
84+
// cascadeTarget
85+
// ..methodTarget.method(
86+
// argument,
87+
// );
88+
MethodInvocation(target: _?) => CallType.unsplittableCall,
89+
90+
// Otherwise, allow a direct method call in the cascade to not split
91+
// the cascade if the arguments can split, as in:
92+
//
93+
// cascadeTarget..method(
94+
// argument,
95+
// );
8196
MethodInvocation(argumentList: var args)
8297
when args.arguments.canSplit(args.rightParenthesis) =>
8398
CallType.splittableCall,
84-
MethodInvocation() => CallType.unsplittableCall,
85-
_ => CallType.property,
99+
_ => CallType.unsplittableCall,
86100
};
87101

88102
_calls.add(ChainCall(piece, callType));

test/tall/regression/0400/0407.unit

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ void main() {
1111
(new Account()
1212
..accountId = new Int64(111)
1313
..tags =
14-
(new Account_Tags()..accountHotlist.add(
15-
new Hotlist()..hotlistId = new Int64(10),
16-
)));
14+
(new Account_Tags()
15+
..accountHotlist.add(
16+
new Hotlist()..hotlistId = new Int64(10),
17+
)));
1718
}
1819
>>> (indent 4)
1920
main() {

test/tall/regression/0400/0489.stmt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ longerNamedFoo..items.add(new Foo()
33
..name = bar.toto
44
..placeUrl = place.toString());
55
<<<
6-
longerNamedFoo..items.add(
7-
new Foo()
8-
..name = bar.toto
9-
..placeUrl = place.toString(),
10-
);
6+
longerNamedFoo
7+
..items.add(
8+
new Foo()
9+
..name = bar.toto
10+
..placeUrl = place.toString(),
11+
);

test/tall/regression/0800/0881.unit

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void main() async {
2424
});
2525
}
2626
<<<
27-
### TODO(1466): Should allow the `FakeRequestHandler()` target after the `=>`.
27+
### TODO(1466): Should allow the `FakeRequestHandler()` target after the `=`.
2828
void main() async {
2929
group('my group', () {
3030
setUp(() async {
@@ -33,13 +33,14 @@ void main() async {
3333
..when(
3434
withServiceName(Ng2ProtoFooBarBazService.serviceName),
3535
).thenRespond(
36-
FooBarBazListResponse()..entities.add(
37-
FooBarBaz()
38-
..fooBarBazEntityId = entityId
39-
..name = 'Test entity'
40-
..baseFooBarBazId = baseFooBarBazId
41-
..entityFooBarBazId = entityFooBarBazId,
42-
),
36+
FooBarBazListResponse()
37+
..entities.add(
38+
FooBarBaz()
39+
..fooBarBazEntityId = entityId
40+
..name = 'Test entity'
41+
..baseFooBarBazId = baseFooBarBazId
42+
..entityFooBarBazId = entityFooBarBazId,
43+
),
4344
)
4445
..when(
4546
allOf(

test/tall/regression/other/misc.stmt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
40 columns |
2+
>>>
3+
someLongExpression..prop.cascade(
4+
argument,
5+
argument,
6+
argument,
7+
);
8+
<<<
9+
someLongExpression
10+
..prop.cascade(
11+
argument,
12+
argument,
13+
argument,
14+
);

0 commit comments

Comments
 (0)