Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit de84784

Browse files
author
Dart CI
committed
Version 2.16.0-132.0.dev
Merge commit 'eaef692c3fcc36547c4e9d80bbddb070c7c0ee13' into 'dev'
2 parents ce29699 + eaef692 commit de84784

20 files changed

+277
-83
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dar
99
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1010

1111
class ReplaceReturnTypeFuture extends CorrectionProducer {
12+
/// The text for the type argument to 'Future'.
13+
String _typeArgument = '';
14+
15+
@override
16+
List<Object>? get fixArguments => [_typeArgument];
17+
1218
@override
1319
FixKind get fixKind => DartFixKind.REPLACE_RETURN_TYPE_FUTURE;
1420

@@ -19,6 +25,7 @@ class ReplaceReturnTypeFuture extends CorrectionProducer {
1925
if (typeAnnotation == null) {
2026
return;
2127
}
28+
_typeArgument = utils.getNodeText(typeAnnotation);
2229

2330
await builder.addDartFileEdit(file, (builder) {
2431
builder.replaceTypeWithFuture(typeAnnotation, typeProvider);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
6+
import 'package:analysis_server/src/services/correction/fix.dart';
7+
import 'package:analyzer/dart/ast/ast.dart';
8+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
9+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
10+
import 'package:analyzer_plugin/utilities/range_factory.dart';
11+
12+
class ReplaceReturnTypeIterable extends CorrectionProducer {
13+
/// The text for the type argument to 'Iterable'.
14+
String _typeArgument = '';
15+
16+
@override
17+
List<Object>? get fixArguments => [_typeArgument];
18+
19+
@override
20+
FixKind get fixKind => DartFixKind.REPLACE_RETURN_TYPE_ITERABLE;
21+
22+
@override
23+
Future<void> compute(ChangeBuilder builder) async {
24+
// prepare the existing type
25+
var typeAnnotation = node.thisOrAncestorOfType<TypeAnnotation>();
26+
if (typeAnnotation == null) {
27+
return;
28+
}
29+
var type = typeAnnotation.type;
30+
if (type == null || type.isDynamic || type.isDartCoreIterable) {
31+
return;
32+
}
33+
_typeArgument = utils.getNodeText(typeAnnotation);
34+
35+
await builder.addDartFileEdit(file, (builder) {
36+
builder.addReplacement(range.node(typeAnnotation), (builder) {
37+
builder.writeType(typeProvider.iterableType(type));
38+
});
39+
});
40+
}
41+
42+
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
43+
static ReplaceReturnTypeIterable newInstance() => ReplaceReturnTypeIterable();
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
6+
import 'package:analysis_server/src/services/correction/fix.dart';
7+
import 'package:analyzer/dart/ast/ast.dart';
8+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
9+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
10+
import 'package:analyzer_plugin/utilities/range_factory.dart';
11+
12+
class ReplaceReturnTypeStream extends CorrectionProducer {
13+
/// The text for the type argument to 'Stream'.
14+
String _typeArgument = '';
15+
16+
@override
17+
List<Object>? get fixArguments => [_typeArgument];
18+
19+
@override
20+
FixKind get fixKind => DartFixKind.REPLACE_RETURN_TYPE_STREAM;
21+
22+
@override
23+
Future<void> compute(ChangeBuilder builder) async {
24+
// prepare the existing type
25+
var typeAnnotation = node.thisOrAncestorOfType<TypeAnnotation>();
26+
if (typeAnnotation == null) {
27+
return;
28+
}
29+
var type = typeAnnotation.type;
30+
if (type == null || type.isDynamic || type.isDartAsyncStream) {
31+
return;
32+
}
33+
_typeArgument = utils.getNodeText(typeAnnotation);
34+
35+
await builder.addDartFileEdit(file, (builder) {
36+
builder.addReplacement(range.node(typeAnnotation), (builder) {
37+
builder.writeType(typeProvider.streamType(type));
38+
});
39+
});
40+
}
41+
42+
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
43+
static ReplaceReturnTypeStream newInstance() => ReplaceReturnTypeStream();
44+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,17 @@ class DartFixKind {
12611261
static const REPLACE_RETURN_TYPE_FUTURE = FixKind(
12621262
'dart.fix.replace.returnTypeFuture',
12631263
DartFixKindPriority.DEFAULT,
1264-
"Return 'Future' from 'async' function",
1264+
"Return 'Future<{0}>'",
1265+
);
1266+
static const REPLACE_RETURN_TYPE_ITERABLE = FixKind(
1267+
'dart.fix.replace.returnTypeIterable',
1268+
DartFixKindPriority.DEFAULT,
1269+
"Return 'Iterable<{0}>'",
1270+
);
1271+
static const REPLACE_RETURN_TYPE_STREAM = FixKind(
1272+
'dart.fix.replace.returnTypeStream',
1273+
DartFixKindPriority.DEFAULT,
1274+
"Return 'Stream<{0}>'",
12651275
);
12661276
static const REPLACE_CONTAINER_WITH_SIZED_BOX = FixKind(
12671277
'dart.fix.replace.containerWithSizedBox',

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ import 'package:analysis_server/src/services/correction/dart/replace_new_with_co
151151
import 'package:analysis_server/src/services/correction/dart/replace_null_with_closure.dart';
152152
import 'package:analysis_server/src/services/correction/dart/replace_return_type.dart';
153153
import 'package:analysis_server/src/services/correction/dart/replace_return_type_future.dart';
154+
import 'package:analysis_server/src/services/correction/dart/replace_return_type_iterable.dart';
155+
import 'package:analysis_server/src/services/correction/dart/replace_return_type_stream.dart';
154156
import 'package:analysis_server/src/services/correction/dart/replace_var_with_dynamic.dart';
155157
import 'package:analysis_server/src/services/correction/dart/replace_with_brackets.dart';
156158
import 'package:analysis_server/src/services/correction/dart/replace_with_conditional_assignment.dart';
@@ -842,9 +844,15 @@ class FixProcessor extends BaseProcessor {
842844
CompileTimeErrorCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_3_PLUS: [
843845
AddFieldFormalParameters.newInstance,
844846
],
847+
CompileTimeErrorCode.ILLEGAL_ASYNC_GENERATOR_RETURN_TYPE: [
848+
ReplaceReturnTypeStream.newInstance,
849+
],
845850
CompileTimeErrorCode.ILLEGAL_ASYNC_RETURN_TYPE: [
846851
ReplaceReturnTypeFuture.newInstance,
847852
],
853+
CompileTimeErrorCode.ILLEGAL_SYNC_GENERATOR_RETURN_TYPE: [
854+
ReplaceReturnTypeIterable.newInstance,
855+
],
848856
CompileTimeErrorCode.IMPLEMENTS_NON_CLASS: [
849857
ChangeTo.classOrMixin,
850858
CreateClass.newInstance,

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

Lines changed: 10 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,36 @@ class ReplaceReturnTypeFutureTest extends FixProcessorTest {
2020
@override
2121
FixKind get kind => DartFixKind.REPLACE_RETURN_TYPE_FUTURE;
2222

23-
Future<void> test_adjacentNodes_withImport() async {
24-
await resolveTestCode('''
25-
import 'dart:async';
26-
var v;int main() async => 0;
27-
''');
28-
await assertHasFix('''
29-
import 'dart:async';
30-
var v;Future<int> main() async => 0;
31-
''', errorFilter: (error) {
32-
return error.errorCode == CompileTimeErrorCode.ILLEGAL_ASYNC_RETURN_TYPE;
33-
});
34-
}
35-
36-
Future<void> test_adjacentNodes_withoutImport() async {
37-
await resolveTestCode('''
38-
var v;int main() async => 0;
39-
''');
40-
await assertHasFix('''
41-
var v;Future<int> main() async => 0;
42-
''');
43-
}
44-
4523
Future<void> test_complexTypeName_withImport() async {
4624
await resolveTestCode('''
4725
import 'dart:async';
48-
List<int> main() async {
49-
}
26+
List<int> f() async {}
5027
''');
5128
await assertHasFix('''
5229
import 'dart:async';
53-
Future<List<int>> main() async {
54-
}
30+
Future<List<int>> f() async {}
5531
''', errorFilter: (error) {
5632
return error.errorCode == CompileTimeErrorCode.ILLEGAL_ASYNC_RETURN_TYPE;
5733
});
5834
}
5935

6036
Future<void> test_complexTypeName_withoutImport() async {
6137
await resolveTestCode('''
62-
List<int> main() async {
63-
}
38+
List<int> f() async {}
6439
''');
6540
await assertHasFix('''
66-
Future<List<int>> main() async {
67-
}
41+
Future<List<int>> f() async {}
6842
''');
6943
}
7044

7145
Future<void> test_importedWithPrefix() async {
7246
await resolveTestCode('''
7347
import 'dart:async' as al;
74-
int main() async {
75-
}
48+
int f() async {}
7649
''');
7750
await assertHasFix('''
7851
import 'dart:async' as al;
79-
al.Future<int> main() async {
80-
}
52+
al.Future<int> f() async {}
8153
''', errorFilter: (error) {
8254
return error.errorCode == CompileTimeErrorCode.ILLEGAL_ASYNC_RETURN_TYPE;
8355
});
@@ -86,52 +58,22 @@ al.Future<int> main() async {
8658
Future<void> test_simpleTypeName_withImport() async {
8759
await resolveTestCode('''
8860
import 'dart:async';
89-
int main() async => 0;
61+
int f() async {}
9062
''');
9163
await assertHasFix('''
9264
import 'dart:async';
93-
Future<int> main() async => 0;
65+
Future<int> f() async {}
9466
''', errorFilter: (error) {
9567
return error.errorCode == CompileTimeErrorCode.ILLEGAL_ASYNC_RETURN_TYPE;
9668
});
9769
}
9870

9971
Future<void> test_simpleTypeName_withoutImport() async {
10072
await resolveTestCode('''
101-
int main() async => 0;
73+
int f() async {}
10274
''');
10375
await assertHasFix('''
104-
Future<int> main() async => 0;
105-
''');
106-
}
107-
108-
Future<void> test_withLibraryDirective_withImport() async {
109-
await resolveTestCode('''
110-
library main;
111-
import 'dart:async';
112-
int main() async {
113-
}
114-
''');
115-
await assertHasFix('''
116-
library main;
117-
import 'dart:async';
118-
Future<int> main() async {
119-
}
120-
''', errorFilter: (error) {
121-
return error.errorCode == CompileTimeErrorCode.ILLEGAL_ASYNC_RETURN_TYPE;
122-
});
123-
}
124-
125-
Future<void> test_withLibraryDirective_withoutImport() async {
126-
await resolveTestCode('''
127-
library main;
128-
int main() async {
129-
}
130-
''');
131-
await assertHasFix('''
132-
library main;
133-
Future<int> main() async {
134-
}
76+
Future<int> f() async {}
13577
''');
13678
}
13779
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
7+
import 'package:test_reflective_loader/test_reflective_loader.dart';
8+
9+
import 'fix_processor.dart';
10+
11+
void main() {
12+
defineReflectiveSuite(() {
13+
defineReflectiveTests(ReplaceReturnTypeIterableTest);
14+
});
15+
}
16+
17+
@reflectiveTest
18+
class ReplaceReturnTypeIterableTest extends FixProcessorTest {
19+
@override
20+
FixKind get kind => DartFixKind.REPLACE_RETURN_TYPE_ITERABLE;
21+
22+
Future<void> test_complexTypeName() async {
23+
await resolveTestCode('''
24+
List<int> f() sync* {}
25+
''');
26+
await assertHasFix('''
27+
Iterable<List<int>> f() sync* {}
28+
''');
29+
}
30+
31+
Future<void> test_importedWithPrefix() async {
32+
await resolveTestCode('''
33+
import 'dart:core' as c;
34+
c.int f() sync* {}
35+
''');
36+
await assertHasFix('''
37+
import 'dart:core' as c;
38+
c.Iterable<c.int> f() sync* {}
39+
''');
40+
}
41+
42+
Future<void> test_simpleTypeName() async {
43+
await resolveTestCode('''
44+
int f() sync* {}
45+
''');
46+
await assertHasFix('''
47+
Iterable<int> f() sync* {}
48+
''');
49+
}
50+
}

0 commit comments

Comments
 (0)