mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 23:39:48 +00:00
quick fix for unnecessary_constructor_name
Fixes: https://github.com/dart-lang/sdk/issues/47102 Change-Id: Idd8b44379d49f416eb4d671ed95ed5f9433af00c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212800 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
d2bde7a29d
commit
e5c659b55f
|
@ -0,0 +1,39 @@
|
|||
// 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/dart/ast/ast.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 RemoveConstructorName extends CorrectionProducer {
|
||||
@override
|
||||
bool get canBeAppliedInBulk => true;
|
||||
|
||||
@override
|
||||
bool get canBeAppliedToFile => true;
|
||||
|
||||
@override
|
||||
FixKind get fixKind => DartFixKind.REMOVE_CONSTRUCTOR_NAME;
|
||||
|
||||
@override
|
||||
FixKind get multiFixKind => DartFixKind.REMOVE_CONSTRUCTOR_NAME_MULTI;
|
||||
|
||||
@override
|
||||
Future<void> compute(ChangeBuilder builder) async {
|
||||
final identifier = node;
|
||||
if (identifier is! SimpleIdentifier) return;
|
||||
|
||||
// The '.' in ".new"
|
||||
var dotToken = identifier.token.previous!;
|
||||
await builder.addDartFileEdit(file, (builder) {
|
||||
builder.addDeletion(range.startStart(dotToken, identifier.token.next!));
|
||||
});
|
||||
}
|
||||
|
||||
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
|
||||
static RemoveConstructorName newInstance() => RemoveConstructorName();
|
||||
}
|
|
@ -755,6 +755,16 @@ class DartFixKind {
|
|||
DartFixKindPriority.DEFAULT,
|
||||
'Remove const',
|
||||
);
|
||||
static const REMOVE_CONSTRUCTOR_NAME = FixKind(
|
||||
'dart.fix.remove.constructorName',
|
||||
DartFixKindPriority.DEFAULT,
|
||||
"Remove 'new'",
|
||||
);
|
||||
static const REMOVE_CONSTRUCTOR_NAME_MULTI = FixKind(
|
||||
'dart.fix.remove.constructorName.multi',
|
||||
DartFixKindPriority.IN_FILE,
|
||||
'Remove constructor names in file',
|
||||
);
|
||||
static const REMOVE_DEAD_CODE = FixKind(
|
||||
'dart.fix.remove.deadCode',
|
||||
DartFixKindPriority.DEFAULT,
|
||||
|
|
|
@ -97,6 +97,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_argument.dar
|
|||
import 'package:analysis_server/src/services/correction/dart/remove_await.dart';
|
||||
import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart';
|
||||
import 'package:analysis_server/src/services/correction/dart/remove_const.dart';
|
||||
import 'package:analysis_server/src/services/correction/dart/remove_constructor_name.dart';
|
||||
import 'package:analysis_server/src/services/correction/dart/remove_dead_code.dart';
|
||||
import 'package:analysis_server/src/services/correction/dart/remove_dead_if_null.dart';
|
||||
import 'package:analysis_server/src/services/correction/dart/remove_duplicate_case.dart';
|
||||
|
@ -557,6 +558,9 @@ class FixProcessor extends BaseProcessor {
|
|||
LintNames.unnecessary_const: [
|
||||
RemoveUnnecessaryConst.newInstance,
|
||||
],
|
||||
LintNames.unnecessary_constructor_name: [
|
||||
RemoveConstructorName.newInstance,
|
||||
],
|
||||
LintNames.unnecessary_final: [
|
||||
ReplaceFinalWithVar.newInstance,
|
||||
],
|
||||
|
|
|
@ -111,6 +111,8 @@ class LintNames {
|
|||
static const String unnecessary_brace_in_string_interps =
|
||||
'unnecessary_brace_in_string_interps';
|
||||
static const String unnecessary_const = 'unnecessary_const';
|
||||
static const String unnecessary_constructor_name =
|
||||
'unnecessary_constructor_name';
|
||||
static const String unnecessary_final = 'unnecessary_final';
|
||||
static const String unnecessary_lambdas = 'unnecessary_lambdas';
|
||||
static const String unnecessary_new = 'unnecessary_new';
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
// 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/expect.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import 'fix_processor.dart';
|
||||
|
||||
void main() {
|
||||
defineReflectiveSuite(() {
|
||||
defineReflectiveTests(RemoveConstructorNameBulkTest);
|
||||
defineReflectiveTests(RemoveConstructorNameInFileTest);
|
||||
defineReflectiveTests(RemoveConstructorNameTest);
|
||||
});
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class RemoveConstructorNameBulkTest extends BulkFixProcessorTest {
|
||||
@override
|
||||
String get lintCode => LintNames.unnecessary_constructor_name;
|
||||
|
||||
Future<void> test_singleFile() async {
|
||||
await resolveTestCode(r'''
|
||||
class A {
|
||||
A.new(int x) {
|
||||
print('new: $x');
|
||||
}
|
||||
}
|
||||
var a = A.new(3);
|
||||
''');
|
||||
await assertHasFix(r'''
|
||||
class A {
|
||||
A(int x) {
|
||||
print('new: $x');
|
||||
}
|
||||
}
|
||||
var a = A(3);
|
||||
''');
|
||||
}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class RemoveConstructorNameInFileTest extends FixInFileProcessorTest {
|
||||
Future<void> test_File() async {
|
||||
createAnalysisOptionsFile(lints: [LintNames.unnecessary_constructor_name]);
|
||||
await resolveTestCode(r'''
|
||||
class A {
|
||||
A.new(int x) {
|
||||
print('new: $x');
|
||||
}
|
||||
}
|
||||
var a = A.new(3);
|
||||
''');
|
||||
var fixes = await getFixesForFirstError();
|
||||
expect(fixes, hasLength(1));
|
||||
assertProduces(fixes.first, r'''
|
||||
class A {
|
||||
A(int x) {
|
||||
print('new: $x');
|
||||
}
|
||||
}
|
||||
var a = A(3);
|
||||
''');
|
||||
}
|
||||
}
|
||||
|
||||
@reflectiveTest
|
||||
class RemoveConstructorNameTest extends FixProcessorLintTest {
|
||||
@override
|
||||
FixKind get kind => DartFixKind.REMOVE_CONSTRUCTOR_NAME;
|
||||
|
||||
@override
|
||||
String get lintCode => LintNames.unnecessary_constructor_name;
|
||||
|
||||
Future<void> test_constructorDeclaration() async {
|
||||
await resolveTestCode(r'''
|
||||
class A {
|
||||
A.new(int x) {
|
||||
print('new: $x');
|
||||
}
|
||||
}
|
||||
''');
|
||||
await assertHasFix(r'''
|
||||
class A {
|
||||
A(int x) {
|
||||
print('new: $x');
|
||||
}
|
||||
}
|
||||
''');
|
||||
}
|
||||
|
||||
Future<void> test_constructorInvocation() async {
|
||||
await resolveTestCode(r'''
|
||||
class A { }
|
||||
var a = A.new();
|
||||
''');
|
||||
await assertHasFix(r'''
|
||||
class A { }
|
||||
var a = A();
|
||||
''');
|
||||
}
|
||||
}
|
|
@ -117,6 +117,7 @@ import 'remove_argument_test.dart' as remove_argument;
|
|||
import 'remove_await_test.dart' as remove_await;
|
||||
import 'remove_comparison_test.dart' as remove_comparison;
|
||||
import 'remove_const_test.dart' as remove_const;
|
||||
import 'remove_constructor_name_test.dart' as remove_constructor_name;
|
||||
import 'remove_dead_code_test.dart' as remove_dead_code;
|
||||
import 'remove_duplicate_case_test.dart' as remove_duplicate_case;
|
||||
import 'remove_empty_catch_test.dart' as remove_empty_catch;
|
||||
|
@ -300,6 +301,7 @@ void main() {
|
|||
remove_await.main();
|
||||
remove_comparison.main();
|
||||
remove_const.main();
|
||||
remove_constructor_name.main();
|
||||
remove_dead_code.main();
|
||||
remove_duplicate_case.main();
|
||||
remove_empty_catch.main();
|
||||
|
|
Loading…
Reference in a new issue