From bae7255c83bbd9f37dd2ae4593de0ff1f3abb4e2 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Mon, 16 Dec 2019 15:33:11 +0000 Subject: [PATCH] Add support to dartfix to fix unnecessary new and const lints Change-Id: I8345ea6afd5aa43bda4855b24d101c04b177ae80 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128522 Reviewed-by: Phil Quitslund Commit-Queue: Brian Wilkerson --- .../edit/fix/basic_fix_lint_error_task.dart | 16 +++++++ .../lib/src/edit/fix/dartfix_info.dart | 26 +++++++++++ .../src/services/correction/fix_internal.dart | 13 ++++-- .../test/domain_edit_dartfix_test.dart | 6 +-- .../src/services/correction/fix/test_all.dart | 2 + .../fix/unnecessary_const_test.dart | 43 +++++++++++++++++++ 6 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 pkg/analysis_server/test/src/services/correction/fix/unnecessary_const_test.dart diff --git a/pkg/analysis_server/lib/src/edit/fix/basic_fix_lint_error_task.dart b/pkg/analysis_server/lib/src/edit/fix/basic_fix_lint_error_task.dart index 8275cf0fc30..32995ce7bb2 100644 --- a/pkg/analysis_server/lib/src/edit/fix/basic_fix_lint_error_task.dart +++ b/pkg/analysis_server/lib/src/edit/fix/basic_fix_lint_error_task.dart @@ -50,4 +50,20 @@ class BasicFixLintErrorTask extends FixLintTask { DartFixKind.REPLACE_WITH_IS_NOT_EMPTY, listener), ); } + + static void unnecessaryConst(DartFixRegistrar registrar, + DartFixListener listener, EditDartfixParams params) { + registrar.registerLintTask( + Registry.ruleRegistry['unnecessary_const'], + new BasicFixLintErrorTask(DartFixKind.REMOVE_UNNECESSARY_CONST, listener), + ); + } + + static void unnecessaryNew(DartFixRegistrar registrar, + DartFixListener listener, EditDartfixParams params) { + registrar.registerLintTask( + Registry.ruleRegistry['unnecessary_new'], + new BasicFixLintErrorTask(DartFixKind.REMOVE_UNNECESSARY_NEW, listener), + ); + } } diff --git a/pkg/analysis_server/lib/src/edit/fix/dartfix_info.dart b/pkg/analysis_server/lib/src/edit/fix/dartfix_info.dart index 987a5bc56ee..298ae296bc5 100644 --- a/pkg/analysis_server/lib/src/edit/fix/dartfix_info.dart +++ b/pkg/analysis_server/lib/src/edit/fix/dartfix_info.dart @@ -106,6 +106,32 @@ will be converted to isDefault: false, isPedantic: true, ), + const DartFixInfo( + 'unnecessary-const', + ''' +Remove unnecessary `const` keywords. + +For example, this + static const digits = const ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + +will be converted to + static const digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];''', + BasicFixLintErrorTask.unnecessaryConst, + isPedantic: true, + ), + const DartFixInfo( + 'unnecessary-new', + ''' +Remove unnecessary `new` keywords. + +For example, this + var marker = new Object(); + +will be converted to + var marker = Object();''', + BasicFixLintErrorTask.unnecessaryNew, + isPedantic: true, + ), // // Other fixes // diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index 96d03a283f0..0c2a7c45b23 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -3197,9 +3197,16 @@ class FixProcessor extends BaseProcessor { } Future _addFix_removeConstKeyword() async { - final instanceCreationExpression = node; - if (instanceCreationExpression is InstanceCreationExpression) { - final constToken = instanceCreationExpression.keyword; + final expression = node; + if (expression is InstanceCreationExpression) { + final constToken = expression.keyword; + var changeBuilder = _newDartChangeBuilder(); + await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) { + builder.addDeletion(range.startStart(constToken, constToken.next)); + }); + _addFixFromBuilder(changeBuilder, DartFixKind.REMOVE_UNNECESSARY_CONST); + } else if (expression is TypedLiteralImpl) { + final constToken = expression.constKeyword; var changeBuilder = _newDartChangeBuilder(); await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) { builder.addDeletion(range.startStart(constToken, constToken.next)); diff --git a/pkg/analysis_server/test/domain_edit_dartfix_test.dart b/pkg/analysis_server/test/domain_edit_dartfix_test.dart index c2c1246ce20..2b1a8585b7c 100644 --- a/pkg/analysis_server/test/domain_edit_dartfix_test.dart +++ b/pkg/analysis_server/test/domain_edit_dartfix_test.dart @@ -136,17 +136,17 @@ const double myDouble = 42.0; addTestFile(''' class A { A.from(Object obj) { } } main() { - print(new A.from([])); + print(A.from([])); } '''); createProject(); EditDartfixResult result = await performFix(); expect(result.suggestions, hasLength(1)); - expectSuggestion(result.suggestions[0], 'type arguments', 65, 8); + expectSuggestion(result.suggestions[0], 'type arguments', 61, 8); expectEdits(result.edits, ''' class A { A.from(Object obj) { } } main() { - print(new A.from([])); + print(A.from([])); } '''); } diff --git a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart index 43df03aac54..08d6388d52a 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart @@ -128,6 +128,7 @@ import 'replace_with_is_not_empty_test.dart' as replace_with_is_not_empty; import 'replace_with_null_aware_test.dart' as replace_with_null_aware; import 'replace_with_tear_off_test.dart' as replace_with_tear_off; import 'sort_child_property_last_test.dart' as sort_properties_last; +import 'unnecessary_const_test.dart' as unnecessary_const; import 'update_sdk_constraints_test.dart' as update_sdk_constraints; import 'use_const_test.dart' as use_const; import 'use_effective_integer_division_test.dart' @@ -249,6 +250,7 @@ main() { replace_with_null_aware.main(); replace_with_tear_off.main(); sort_properties_last.main(); + unnecessary_const.main(); update_sdk_constraints.main(); use_const.main(); use_effective_integer_division.main(); diff --git a/pkg/analysis_server/test/src/services/correction/fix/unnecessary_const_test.dart b/pkg/analysis_server/test/src/services/correction/fix/unnecessary_const_test.dart new file mode 100644 index 00000000000..5217ebe122e --- /dev/null +++ b/pkg/analysis_server/test/src/services/correction/fix/unnecessary_const_test.dart @@ -0,0 +1,43 @@ +// Copyright (c) 2019, 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'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(UnnecessaryConstTest); + }); +} + +@reflectiveTest +class UnnecessaryConstTest extends FixProcessorLintTest { + @override + FixKind get kind => DartFixKind.REMOVE_UNNECESSARY_CONST; + + @override + String get lintCode => LintNames.unnecessary_const; + + test_instanceCreation() async { + await resolveTestUnit(''' +const list = /*LINT*/const List(); +'''); + await assertHasFix(''' +const list = /*LINT*/List(); +''', length: 5); + } + + test_typedLiteral() async { + await resolveTestUnit(''' +const list = /*LINT*/const []; +'''); + await assertHasFix(''' +const list = /*LINT*/[]; +''', length: 5); + } +}