quick fix for prefer_void_to_null

Fixes: https://github.com/dart-lang/sdk/issues/45927

Change-Id: Iac9d5427ef89d51d082ea967febe9cb5597d6525
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/206103
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
pq 2021-07-09 00:01:44 +00:00 committed by commit-bot@chromium.org
parent 7804da29be
commit 427e51697d
6 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,37 @@
// Copyright (c) 2021, 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/error/error.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
class ReplaceNullWithVoid extends CorrectionProducer {
@override
bool get canBeAppliedInBulk => true;
@override
bool get canBeAppliedToFile => true;
@override
FixKind get fixKind => DartFixKind.REPLACE_NULL_WITH_VOID;
@override
FixKind? get multiFixKind => DartFixKind.REPLACE_NULL_WITH_VOID_MULTI;
@override
Future<void> compute(ChangeBuilder builder) async {
final diagnostic = this.diagnostic;
if (diagnostic is AnalysisError) {
await builder.addDartFileEdit(file, (builder) {
builder.addSimpleReplacement(range.error(diagnostic), 'void');
});
}
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static ReplaceNullWithVoid newInstance() => ReplaceNullWithVoid();
}

View file

@ -724,6 +724,12 @@ class DartFixKind {
'dart.fix.replace.finalWithVar.multi',
DartFixKindPriority.IN_FILE,
"Replace 'final' with 'var' where possible in file");
static const REPLACE_NULL_WITH_VOID = FixKind('dart.fix.replace.nullWithVoid',
DartFixKindPriority.DEFAULT, "Replace 'Null' with 'void'");
static const REPLACE_NULL_WITH_VOID_MULTI = FixKind(
'dart.fix.replace.nullWithVoid.multi',
DartFixKindPriority.DEFAULT,
"Replace 'Null' with 'void' everywhere in file");
static const REPLACE_RETURN_TYPE_FUTURE = FixKind(
'dart.fix.replace.returnTypeFuture',
DartFixKindPriority.DEFAULT,

View file

@ -130,6 +130,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_unused_label
import 'package:analysis_server/src/services/correction/dart/remove_unused_local_variable.dart';
import 'package:analysis_server/src/services/correction/dart/remove_unused_parameter.dart';
import 'package:analysis_server/src/services/correction/dart/rename_to_camel_case.dart';
import 'package:analysis_server/src/services/correction/dart/replace_Null_with_void.dart';
import 'package:analysis_server/src/services/correction/dart/replace_boolean_with_bool.dart';
import 'package:analysis_server/src/services/correction/dart/replace_cascade_with_dot.dart';
import 'package:analysis_server/src/services/correction/dart/replace_colon_with_equals.dart';
@ -513,6 +514,9 @@ class FixProcessor extends BaseProcessor {
LintNames.prefer_typing_uninitialized_variables: [
AddTypeAnnotation.newInstance,
],
LintNames.prefer_void_to_null: [
ReplaceNullWithVoid.newInstance,
],
LintNames.slash_for_doc_comments: [
ConvertDocumentationIntoLine.newInstance,
],

View file

@ -93,6 +93,7 @@ class LintNames {
static const String prefer_spread_collections = 'prefer_spread_collections';
static const String prefer_typing_uninitialized_variables =
'prefer_typing_uninitialized_variables';
static const String prefer_void_to_null = 'prefer_void_to_null';
static const String slash_for_doc_comments = 'slash_for_doc_comments';
static const String sort_child_properties_last = 'sort_child_properties_last';
static const String sort_constructors_first = 'sort_constructors_first';

View file

@ -0,0 +1,62 @@
// Copyright (c) 2021, 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:analysis_server/src/services/linter/lint_names.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(ReplaceNullWithVoidTest);
defineReflectiveTests(ReplaceNullWithVoidBulkTest);
});
}
@reflectiveTest
class ReplaceNullWithVoidBulkTest extends BulkFixProcessorTest {
@override
String get lintCode => LintNames.prefer_void_to_null;
Future<void> test_singleFile() async {
await resolveTestCode('''
Future<Null> f() async {
await Future.value();
}
Future<Null>? future_null;
''');
await assertHasFix('''
Future<void> f() async {
await Future.value();
}
Future<void>? future_null;
''');
}
}
@reflectiveTest
class ReplaceNullWithVoidTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.REPLACE_NULL_WITH_VOID;
@override
String get lintCode => LintNames.prefer_void_to_null;
Future<void> test_simple() async {
await resolveTestCode('''
Future<Null> f() async {
await Future.value();
}
''');
await assertHasFix('''
Future<void> f() async {
await Future.value();
}
''');
}
}

View file

@ -156,6 +156,7 @@ import 'remove_unused_label_test.dart' as remove_unused_label;
import 'remove_unused_local_variable_test.dart' as remove_unused_local_variable;
import 'remove_unused_parameter_test.dart' as remove_unused_parameter;
import 'rename_to_camel_case_test.dart' as rename_to_camel_case;
import 'replace_Null_with_void_test.dart' as replace_null_with_void;
import 'replace_boolean_with_bool_test.dart' as replace_boolean_with_bool;
import 'replace_cascade_with_dot_test.dart' as replace_cascade_with_dot;
import 'replace_colon_with_equals_test.dart' as replace_colon_with_equals;
@ -334,6 +335,7 @@ void main() {
replace_final_with_var.main();
replace_new_with_const.main();
replace_null_with_closure.main();
replace_null_with_void.main();
replace_return_type_future.main();
replace_var_with_dynamic.main();
replace_with_brackets.main();