Skip to content

Commit 80ecef5

Browse files
authored
Block-format await expressions if the inner expression allows it. (#1538)
Block-format await expressions if the inner expression allows it. In assignment-like context, if the RHS is an await expression and the inner expression can be block formatted, then let the whole await expression be block formatted. This comes into play in assignments, but not in argument lists which are the other place where block formatting is a thing. That's because in argument lists, we *don't* treat function calls as block formattable. (This is both for performance and style reasons). There's no point in awaiting any of the other kinds of block-formattable expressions: collection literals or function expressions. So this really just benefits assignments, named argument expressions, and `=>` bodies. But it definitely makes those look better. Fix #1531.
1 parent 5749a94 commit 80ecef5

File tree

6 files changed

+101
-7
lines changed

6 files changed

+101
-7
lines changed

lib/src/ast_extensions.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ extension ExpressionExtensions on Expression {
220220

221221
// Parenthesized expressions unwrap the inner expression.
222222
ParenthesizedExpression(:var expression) => expression.blockFormatType,
223+
224+
// Await expressions unwrap the inner expression.
225+
AwaitExpression(:var expression) => expression.blockFormatType,
223226
_ => BlockFormat.none,
224227
};
225228
}

test/tall/regression/0400/0466.unit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
Future get identifier________ async {
1111
var id = identifier________.identifier__;
1212

13-
return identifier_____________[id] ??=
14-
await identifier_______.identifier____________________(
13+
return identifier_____________[id] ??= await identifier_______
14+
.identifier____________________(
1515
identifier___________________________.create()..identifier____ = id,
1616
);
1717
}

test/tall/regression/1400/1463.unit

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ class C {
4747
final chatApi = ChatsApi(di<ApiClient>());
4848
if (_target == null) {
4949
assert(_chatPartner != null);
50-
_target =
51-
await chatApi.sendMessageToUserAndCreateChatIfNeeded(
52-
_chatPartner!.id.toString(),
53-
messageUpdateRequest: newMessage,
54-
);
50+
_target = await chatApi.sendMessageToUserAndCreateChatIfNeeded(
51+
_chatPartner!.id.toString(),
52+
messageUpdateRequest: newMessage,
53+
);
5554
} else {
5655
await chatApi.addMessageToChat(
5756
chatId,

test/tall/regression/1500/1526.stmt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
>>> (indent 2)
2+
final character =
3+
await repository.fetchCharacter(id, cancelToken: cancelToken);
4+
<<<
5+
final character = await repository.fetchCharacter(
6+
id,
7+
cancelToken: cancelToken,
8+
);
9+
>>> (indent 6)
10+
final charactersResponse =
11+
await repository.fetchCharacters(
12+
offset: meta.page * kCharactersPageLimit,
13+
limit: kCharactersPageLimit,
14+
nameStartsWith: meta.name,
15+
cancelToken: cancelToken,
16+
);
17+
<<<
18+
final charactersResponse = await repository.fetchCharacters(
19+
offset: meta.page * kCharactersPageLimit,
20+
limit: kCharactersPageLimit,
21+
nameStartsWith: meta.name,
22+
cancelToken: cancelToken,
23+
);
24+
>>> (indent 4)
25+
final response =
26+
await _get('characters', queryParameters: <String, Object?>{
27+
'offset': offset,
28+
if (limit != null) 'limit': limit,
29+
if (cleanNameFilter != null && cleanNameFilter.isNotEmpty)
30+
'nameStartsWith': cleanNameFilter,
31+
}, cancelToken: cancelToken);
32+
<<<
33+
final response = await _get('characters', queryParameters:
34+
<String, Object?>{
35+
'offset': offset,
36+
if (limit != null) 'limit': limit,
37+
if (cleanNameFilter != null && cleanNameFilter.isNotEmpty)
38+
'nameStartsWith': cleanNameFilter,
39+
}, cancelToken: cancelToken);

test/tall/regression/1500/1531.stmt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
>>>
2+
variable =
3+
await function(
4+
argument, // Force split in argument list.
5+
another,
6+
);
7+
<<<
8+
variable = await function(
9+
argument, // Force split in argument list.
10+
another,
11+
);
12+
>>> (indent 4)
13+
final response =
14+
await post(Uri.parse(webhookUrl), headers: {
15+
HttpHeaders.contentTypeHeader: ContentType.json.value,
16+
}, body: json.encode(discordPayload));
17+
<<<
18+
final response = await post(Uri.parse(webhookUrl), headers: {
19+
HttpHeaders.contentTypeHeader: ContentType.json.value,
20+
}, body: json.encode(discordPayload));
21+
>>> (indent 2)
22+
final token =
23+
await jwt.verify('<TOKEN>', issuer: '<ISSUER>', audience: {
24+
'<AUDIENCE>',
25+
}, publicKeysUrl: '<PUBLIC_KEYS_URL>');
26+
<<<
27+
final token = await jwt.verify('<TOKEN>', issuer: '<ISSUER>', audience: {
28+
'<AUDIENCE>',
29+
}, publicKeysUrl: '<PUBLIC_KEYS_URL>');
30+
>>> (indent 6)
31+
final connection =
32+
await connectSocket(
33+
uri.host,
34+
port: uri.port,
35+
timeout: _socketOptions.timeout,
36+
);
37+
<<<
38+
final connection = await connectSocket(
39+
uri.host,
40+
port: uri.port,
41+
timeout: _socketOptions.timeout,
42+
);

test/tall/variable/local.stmt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ var variableName = (notDelimited + expression);
244244
<<<
245245
var variableName =
246246
(notDelimited + expression);
247+
>>> Use block-like splitting for await whose inner expression is block-like.
248+
main() async {
249+
var variableName = await function(argument, argument);
250+
}
251+
<<<
252+
main() async {
253+
var variableName = await function(
254+
argument,
255+
argument,
256+
);
257+
}
247258
>>> Split all variables if an initializer has a split internally.
248259
var a = 1, b = [element, element, element, element];
249260
<<<

0 commit comments

Comments
 (0)