Add a fix for default_list_constructor

Change-Id: Ia7b9c88cfe9528ddddd68935009736c494e57130
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151098
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Brian Wilkerson 2020-06-13 16:29:35 +00:00 committed by commit-bot@chromium.org
parent 32a93e2147
commit 8d08bbf40a
5 changed files with 111 additions and 0 deletions

View file

@ -0,0 +1,37 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
class ReplaceWithFilled extends CorrectionProducer {
@override
FixKind get fixKind => DartFixKind.REPLACE_WITH_FILLED;
@override
Future<void> compute(DartChangeBuilder builder) async {
var typeName = node is SimpleIdentifier ? node.parent : node;
var creation = typeName?.parent?.parent;
if (typeName is TypeName && creation is InstanceCreationExpression) {
var elementType = (typeName.type as InterfaceType).typeArguments[0];
if (typeSystem.isNullable(elementType)) {
var argumentList = creation.argumentList;
if (argumentList.arguments.length == 1) {
await builder.addFileEdit(file, (builder) {
builder.addSimpleInsertion(argumentList.offset, '.filled');
builder.addSimpleInsertion(
argumentList.arguments[0].end, ', null, growable: false');
});
}
}
}
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static ReplaceWithFilled newInstance() => ReplaceWithFilled();
}

View file

@ -405,6 +405,8 @@ class DartFixKind {
appliedTogetherMessage: "Replace all 'boolean' with 'bool' in file");
static const REPLACE_COLON_WITH_EQUALS =
FixKind('dart.fix.replace.colonWithEquals', 50, "Replace ':' with '='");
static const REPLACE_WITH_FILLED = FixKind(
'dart.fix.replace.finalWithListFilled', 50, "Replace with 'List.filled'");
static const REPLACE_FINAL_WITH_CONST = FixKind(
'dart.fix.replace.finalWithConst', 50, "Replace 'final' with 'const'");
static const REPLACE_NEW_WITH_CONST = FixKind(

View file

@ -123,6 +123,7 @@ import 'package:analysis_server/src/services/correction/dart/replace_with_bracke
import 'package:analysis_server/src/services/correction/dart/replace_with_conditional_assignment.dart';
import 'package:analysis_server/src/services/correction/dart/replace_with_eight_digit_hex.dart';
import 'package:analysis_server/src/services/correction/dart/replace_with_extension_name.dart';
import 'package:analysis_server/src/services/correction/dart/replace_with_filled.dart';
import 'package:analysis_server/src/services/correction/dart/replace_with_identifier.dart';
import 'package:analysis_server/src/services/correction/dart/replace_with_interpolation.dart';
import 'package:analysis_server/src/services/correction/dart/replace_with_is_empty.dart';
@ -579,6 +580,9 @@ class FixProcessor extends BaseProcessor {
CompileTimeErrorCode.CONST_WITH_NON_CONST: [
RemoveConst.newInstance,
],
CompileTimeErrorCode.DEFAULT_LIST_CONSTRUCTOR: [
ReplaceWithFilled.newInstance,
],
CompileTimeErrorCode.CONST_WITH_NON_TYPE: [
ChangeTo.classOrMixin,
],

View file

@ -0,0 +1,66 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/src/dart/analysis/experiments.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'fix_processor.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(ReplaceWithFilledTest);
});
}
@reflectiveTest
class ReplaceWithFilledTest extends FixProcessorTest {
@override
List<String> get experiments => [EnableString.non_nullable];
@override
FixKind get kind => DartFixKind.REPLACE_WITH_FILLED;
Future<void> test_nonNullableElements() async {
await resolveTestUnit('''
var l = new List<int>(3);
''');
await assertNoFix();
}
Future<void> test_nonNullableElements_inferred() async {
await resolveTestUnit('''
List<int> l = List(3);
''');
await assertNoFix();
}
Future<void> test_nullableElements() async {
await resolveTestUnit('''
var l = new List<int?>(3);
''');
await assertHasFix('''
var l = new List<int?>.filled(3, null, growable: false);
''');
}
Future<void> test_nullableElements_inferred() async {
await resolveTestUnit('''
List<int?> l = List(5);
''');
await assertHasFix('''
List<int?> l = List.filled(5, null, growable: false);
''');
}
Future<void> test_trailingComma() async {
await resolveTestUnit('''
var l = List<int?>(3,);
''');
await assertHasFix('''
var l = List<int?>.filled(3, null, growable: false,);
''');
}
}

View file

@ -140,6 +140,7 @@ import 'replace_with_conditional_assignment_test.dart'
as replace_with_conditional_assignment;
import 'replace_with_eight_digit_hex_test.dart' as replace_with_eight_digit_hex;
import 'replace_with_extension_name_test.dart' as replace_with_extension_name;
import 'replace_with_filled_test.dart' as replace_with_filled;
import 'replace_with_identifier_test.dart' as replace_with_identifier;
import 'replace_with_interpolation_test.dart' as replace_with_interpolation;
import 'replace_with_is_empty_test.dart' as replace_with_is_empty;
@ -283,6 +284,7 @@ void main() {
replace_with_conditional_assignment.main();
replace_with_eight_digit_hex.main();
replace_with_extension_name.main();
replace_with_filled.main();
replace_with_identifier.main();
replace_with_interpolation.main();
replace_with_is_empty.main();