Skip to content

Commit f4cfbf9

Browse files
jakemac53Commit Queue
authored andcommitted
Update create function fix to create an async function for await expressions
Bug: #59990 Change-Id: I9e89089ca5c60bea190060bc0c8e869c7e78e32e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408361 Commit-Queue: Brian Wilkerson <[email protected]> Auto-Submit: Jake Macdonald <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 50f96d6 commit f4cfbf9

File tree

6 files changed

+188
-12
lines changed

6 files changed

+188
-12
lines changed

pkg/analysis_server/lib/src/services/correction/dart/create_function.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CreateFunction extends ResolvedCorrectionProducer {
4040
if (target != null) {
4141
return;
4242
}
43+
4344
// prepare environment
4445
int insertOffset;
4546
String sourcePrefix;
@@ -54,19 +55,22 @@ class CreateFunction extends ResolvedCorrectionProducer {
5455
builder.addInsertion(insertOffset, (builder) {
5556
builder.write(sourcePrefix);
5657
// append return type
57-
{
58-
var type = inferUndefinedExpressionType(invocation);
59-
if (builder.writeType(type, groupName: 'RETURN_TYPE')) {
60-
builder.write(' ');
61-
}
58+
var type = inferUndefinedExpressionType(invocation);
59+
if (builder.writeType(type, groupName: 'RETURN_TYPE')) {
60+
builder.write(' ');
6261
}
62+
6363
// append name
6464
builder.addLinkedEdit('NAME', (builder) {
6565
builder.write(_functionName);
6666
});
6767
builder.write('(');
6868
builder.writeParametersMatchingArguments(invocation.argumentList);
69-
builder.write(') {$eol}');
69+
builder.write(')');
70+
if (type?.isDartAsyncFuture == true) {
71+
builder.write(' async');
72+
}
73+
builder.write(' {$eol}');
7074
});
7175
builder.addLinkedPosition(range.node(node), 'NAME');
7276
});

pkg/analysis_server/lib/src/services/correction/dart/create_method.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,23 @@ class CreateMethod extends ResolvedCorrectionProducer {
187187
builder.write('static ');
188188
}
189189
// Append return type.
190-
{
191-
var type = inferUndefinedExpressionType(invocation);
192-
if (builder.writeType(type, groupName: 'RETURN_TYPE')) {
193-
builder.write(' ');
194-
}
190+
191+
var type = inferUndefinedExpressionType(invocation);
192+
if (builder.writeType(type, groupName: 'RETURN_TYPE')) {
193+
builder.write(' ');
195194
}
195+
196196
// Append name.
197197
builder.addLinkedEdit('NAME', (builder) {
198198
builder.write(_memberName);
199199
});
200200
builder.write('(');
201201
builder.writeParametersMatchingArguments(invocation.argumentList);
202-
builder.write(') {}');
202+
builder.write(')');
203+
if (type?.isDartAsyncFuture == true) {
204+
builder.write(' async');
205+
}
206+
builder.write(' {}');
203207
});
204208
if (targetFile == file) {
205209
builder.addLinkedPosition(range.node(node), 'NAME');

pkg/analysis_server/lib/src/services/correction/dart/create_method_or_function.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class CreateMethodOrFunction extends ResolvedCorrectionProducer {
150150
});
151151
// append parameters
152152
builder.writeFormalParameters(functionType.formalParameters);
153+
if (functionType.returnType.isDartAsyncFuture) {
154+
builder.write(' async');
155+
}
153156
// close method
154157
builder.write(' {$eol$prefix}');
155158
builder.write(sourceSuffix);

pkg/analysis_server/test/src/services/correction/fix/create_function_test.dart

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,96 @@ bool test() {
3838
''');
3939
}
4040

41+
Future<void> test_await_infer_from_parent() async {
42+
await resolveTestCode('''
43+
Future<void> f() async {
44+
if (await myUndefinedFunction()) {}
45+
}
46+
''');
47+
await assertHasFix('''
48+
Future<void> f() async {
49+
if (await myUndefinedFunction()) {}
50+
}
51+
52+
Future<bool> myUndefinedFunction() async {
53+
}
54+
''');
55+
}
56+
57+
Future<void> test_await_no_assignment() async {
58+
await resolveTestCode('''
59+
Future<void> f() async {
60+
await myUndefinedFunction();
61+
}
62+
''');
63+
await assertHasFix('''
64+
Future<void> f() async {
65+
await myUndefinedFunction();
66+
}
67+
68+
Future<void> myUndefinedFunction() async {
69+
}
70+
''');
71+
}
72+
73+
Future<void> test_await_variable_assignment() async {
74+
await resolveTestCode('''
75+
int x = 1;
76+
Future<void> f() async {
77+
x = await myUndefinedFunction();
78+
print(x);
79+
}
80+
''');
81+
await assertHasFix('''
82+
int x = 1;
83+
Future<void> f() async {
84+
x = await myUndefinedFunction();
85+
print(x);
86+
}
87+
88+
Future<int> myUndefinedFunction() async {
89+
}
90+
''');
91+
}
92+
93+
Future<void> test_await_variable_declaration() async {
94+
await resolveTestCode('''
95+
Future<void> f() async {
96+
var x = await myUndefinedFunction();
97+
print(x);
98+
}
99+
''');
100+
await assertHasFix('''
101+
Future<void> f() async {
102+
var x = await myUndefinedFunction();
103+
print(x);
104+
}
105+
106+
Future myUndefinedFunction() async {
107+
}
108+
''');
109+
}
110+
111+
Future<void> test_await_variable_plusEq() async {
112+
await resolveTestCode('''
113+
String x = 'hello';
114+
Future<void> f() async {
115+
x += await myUndefinedFunction();
116+
print(x);
117+
}
118+
''');
119+
await assertHasFix('''
120+
String x = 'hello';
121+
Future<void> f() async {
122+
x += await myUndefinedFunction();
123+
print(x);
124+
}
125+
126+
Future<String> myUndefinedFunction() async {
127+
}
128+
''');
129+
}
130+
41131
Future<void> test_bottomArgument() async {
42132
await resolveTestCode('''
43133
void f() {

pkg/analysis_server/test/src/services/correction/fix/create_method_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,67 @@ class CreateMethodTest extends FixProcessorTest {
207207
@override
208208
FixKind get kind => DartFixKind.CREATE_METHOD;
209209

210+
Future<void> test_await_expression_statement() async {
211+
await resolveTestCode('''
212+
class A {
213+
Future<void> f() async {
214+
await myUndefinedFunction();
215+
}
216+
}
217+
''');
218+
await assertHasFix('''
219+
class A {
220+
Future<void> f() async {
221+
await myUndefinedFunction();
222+
}
223+
224+
Future<void> myUndefinedFunction() async {}
225+
}
226+
''');
227+
}
228+
229+
Future<void> test_await_field_assignment() async {
230+
await resolveTestCode('''
231+
class A {
232+
int x = 1;
233+
Future<void> f() async {
234+
x = await myUndefinedFunction();
235+
print(x);
236+
}
237+
}
238+
''');
239+
await assertHasFix('''
240+
class A {
241+
int x = 1;
242+
Future<void> f() async {
243+
x = await myUndefinedFunction();
244+
print(x);
245+
}
246+
247+
Future<int> myUndefinedFunction() async {}
248+
}
249+
''');
250+
}
251+
252+
Future<void> test_await_infer_from_parent() async {
253+
await resolveTestCode('''
254+
class A {
255+
Future<void> f() async {
256+
if (await myUndefinedFunction()) {}
257+
}
258+
}
259+
''');
260+
await assertHasFix('''
261+
class A {
262+
Future<void> f() async {
263+
if (await myUndefinedFunction()) {}
264+
}
265+
266+
Future<bool> myUndefinedFunction() async {}
267+
}
268+
''');
269+
}
270+
210271
Future<void> test_createQualified_emptyClassBody() async {
211272
await resolveTestCode('''
212273
class A {}

pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,20 @@ abstract class ResolvedCorrectionProducer
611611
}
612612
}
613613
}
614+
// Handle `await`, infer a `Future` type.
615+
if (parent is AwaitExpression) {
616+
var grandParent = parent.parent;
617+
// `await myFunction();`
618+
if (grandParent is ExpressionStatement) {
619+
return typeProvider.futureType(typeProvider.voidType);
620+
}
621+
var inferredParentType =
622+
inferUndefinedExpressionType(parent) ?? typeProvider.dynamicType;
623+
if (inferredParentType is InvalidType) {
624+
inferredParentType = typeProvider.dynamicType;
625+
}
626+
return typeProvider.futureType(inferredParentType);
627+
}
614628
// We don't know.
615629
return null;
616630
}

0 commit comments

Comments
 (0)