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 <pquitslund@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
committed by
commit-bot@chromium.org
parent
1befb89903
commit
bae7255c83
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
//
|
||||
|
||||
@@ -3197,9 +3197,16 @@ class FixProcessor extends BaseProcessor {
|
||||
}
|
||||
|
||||
Future<void> _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));
|
||||
|
||||
@@ -136,17 +136,17 @@ const double myDouble = 42.0;
|
||||
addTestFile('''
|
||||
class A<T> { A.from(Object obj) { } }
|
||||
main() {
|
||||
print(new A.from<String>([]));
|
||||
print(A.from<String>([]));
|
||||
}
|
||||
''');
|
||||
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<T> { A.from(Object obj) { } }
|
||||
main() {
|
||||
print(new A<String>.from([]));
|
||||
print(A<String>.from([]));
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user