Skip to content

Commit 8d08bbf

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Add a fix for default_list_constructor
Change-Id: Ia7b9c88cfe9528ddddd68935009736c494e57130 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151098 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 32a93e2 commit 8d08bbf

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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/dart/element/type.dart';
9+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
10+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
11+
12+
class ReplaceWithFilled extends CorrectionProducer {
13+
@override
14+
FixKind get fixKind => DartFixKind.REPLACE_WITH_FILLED;
15+
16+
@override
17+
Future<void> compute(DartChangeBuilder builder) async {
18+
var typeName = node is SimpleIdentifier ? node.parent : node;
19+
var creation = typeName?.parent?.parent;
20+
if (typeName is TypeName && creation is InstanceCreationExpression) {
21+
var elementType = (typeName.type as InterfaceType).typeArguments[0];
22+
if (typeSystem.isNullable(elementType)) {
23+
var argumentList = creation.argumentList;
24+
if (argumentList.arguments.length == 1) {
25+
await builder.addFileEdit(file, (builder) {
26+
builder.addSimpleInsertion(argumentList.offset, '.filled');
27+
builder.addSimpleInsertion(
28+
argumentList.arguments[0].end, ', null, growable: false');
29+
});
30+
}
31+
}
32+
}
33+
}
34+
35+
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
36+
static ReplaceWithFilled newInstance() => ReplaceWithFilled();
37+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ class DartFixKind {
405405
appliedTogetherMessage: "Replace all 'boolean' with 'bool' in file");
406406
static const REPLACE_COLON_WITH_EQUALS =
407407
FixKind('dart.fix.replace.colonWithEquals', 50, "Replace ':' with '='");
408+
static const REPLACE_WITH_FILLED = FixKind(
409+
'dart.fix.replace.finalWithListFilled', 50, "Replace with 'List.filled'");
408410
static const REPLACE_FINAL_WITH_CONST = FixKind(
409411
'dart.fix.replace.finalWithConst', 50, "Replace 'final' with 'const'");
410412
static const REPLACE_NEW_WITH_CONST = FixKind(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import 'package:analysis_server/src/services/correction/dart/replace_with_bracke
123123
import 'package:analysis_server/src/services/correction/dart/replace_with_conditional_assignment.dart';
124124
import 'package:analysis_server/src/services/correction/dart/replace_with_eight_digit_hex.dart';
125125
import 'package:analysis_server/src/services/correction/dart/replace_with_extension_name.dart';
126+
import 'package:analysis_server/src/services/correction/dart/replace_with_filled.dart';
126127
import 'package:analysis_server/src/services/correction/dart/replace_with_identifier.dart';
127128
import 'package:analysis_server/src/services/correction/dart/replace_with_interpolation.dart';
128129
import 'package:analysis_server/src/services/correction/dart/replace_with_is_empty.dart';
@@ -579,6 +580,9 @@ class FixProcessor extends BaseProcessor {
579580
CompileTimeErrorCode.CONST_WITH_NON_CONST: [
580581
RemoveConst.newInstance,
581582
],
583+
CompileTimeErrorCode.DEFAULT_LIST_CONSTRUCTOR: [
584+
ReplaceWithFilled.newInstance,
585+
],
582586
CompileTimeErrorCode.CONST_WITH_NON_TYPE: [
583587
ChangeTo.classOrMixin,
584588
],
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/fix.dart';
6+
import 'package:analyzer/src/dart/analysis/experiments.dart';
7+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import 'fix_processor.dart';
11+
12+
void main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(ReplaceWithFilledTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class ReplaceWithFilledTest extends FixProcessorTest {
20+
@override
21+
List<String> get experiments => [EnableString.non_nullable];
22+
23+
@override
24+
FixKind get kind => DartFixKind.REPLACE_WITH_FILLED;
25+
26+
Future<void> test_nonNullableElements() async {
27+
await resolveTestUnit('''
28+
var l = new List<int>(3);
29+
''');
30+
await assertNoFix();
31+
}
32+
33+
Future<void> test_nonNullableElements_inferred() async {
34+
await resolveTestUnit('''
35+
List<int> l = List(3);
36+
''');
37+
await assertNoFix();
38+
}
39+
40+
Future<void> test_nullableElements() async {
41+
await resolveTestUnit('''
42+
var l = new List<int?>(3);
43+
''');
44+
await assertHasFix('''
45+
var l = new List<int?>.filled(3, null, growable: false);
46+
''');
47+
}
48+
49+
Future<void> test_nullableElements_inferred() async {
50+
await resolveTestUnit('''
51+
List<int?> l = List(5);
52+
''');
53+
await assertHasFix('''
54+
List<int?> l = List.filled(5, null, growable: false);
55+
''');
56+
}
57+
58+
Future<void> test_trailingComma() async {
59+
await resolveTestUnit('''
60+
var l = List<int?>(3,);
61+
''');
62+
await assertHasFix('''
63+
var l = List<int?>.filled(3, null, growable: false,);
64+
''');
65+
}
66+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ import 'replace_with_conditional_assignment_test.dart'
140140
as replace_with_conditional_assignment;
141141
import 'replace_with_eight_digit_hex_test.dart' as replace_with_eight_digit_hex;
142142
import 'replace_with_extension_name_test.dart' as replace_with_extension_name;
143+
import 'replace_with_filled_test.dart' as replace_with_filled;
143144
import 'replace_with_identifier_test.dart' as replace_with_identifier;
144145
import 'replace_with_interpolation_test.dart' as replace_with_interpolation;
145146
import 'replace_with_is_empty_test.dart' as replace_with_is_empty;
@@ -283,6 +284,7 @@ void main() {
283284
replace_with_conditional_assignment.main();
284285
replace_with_eight_digit_hex.main();
285286
replace_with_extension_name.main();
287+
replace_with_filled.main();
286288
replace_with_identifier.main();
287289
replace_with_interpolation.main();
288290
replace_with_is_empty.main();

0 commit comments

Comments
 (0)