[analysis_server] enable AddExplicitCast for FixInFile.

Bug #49896

Change-Id: Ife8f60d1ce13683e9ec1b12af5d27221f3f04aea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276160
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Ahmed Ashour 2023-01-24 18:44:11 +00:00 committed by Commit Queue
parent 0b5cd20ff2
commit 457aa53834
3 changed files with 231 additions and 223 deletions

View file

@ -15,9 +15,15 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
class AddExplicitCast extends CorrectionProducer {
@override
bool get canBeAppliedToFile => true;
@override
FixKind get fixKind => DartFixKind.ADD_EXPLICIT_CAST;
@override
FixKind get multiFixKind => DartFixKind.ADD_EXPLICIT_CAST_MULTI;
@override
Future<void> compute(ChangeBuilder builder) async {
var target = coveredNode;

View file

@ -163,6 +163,11 @@ class DartFixKind {
DartFixKindPriority.DEFAULT,
'Add cast',
);
static const ADD_EXPLICIT_CAST_MULTI = FixKind(
'dart.fix.add.explicitCast.multi',
DartFixKindPriority.IN_FILE,
'Add cast everywhere in file',
);
static const ADD_FIELD_FORMAL_PARAMETERS = FixKind(
'dart.fix.add.fieldFormalParameters',
70,
@ -1221,27 +1226,27 @@ class DartFixKind {
static const REMOVE_UNNECESSARY_CONST_MULTI = FixKind(
'dart.fix.remove.unnecessaryConst.multi',
DartFixKindPriority.IN_FILE,
'Remove unnecessary const keywords everywhere in file',
"Remove unnecessary 'const' keywords everywhere in file",
);
static const REMOVE_UNNECESSARY_LATE = FixKind(
'dart.fix.remove.unnecessaryLate',
DartFixKindPriority.DEFAULT,
'Remove unnecessary late keyword',
"Remove unnecessary 'late' keyword",
);
static const REMOVE_UNNECESSARY_LATE_MULTI = FixKind(
'dart.fix.remove.unnecessaryLate.multi',
DartFixKindPriority.IN_FILE,
'Remove unnecessary late keywords everywhere in file',
"Remove unnecessary 'late' keywords everywhere in file",
);
static const REMOVE_UNNECESSARY_NEW = FixKind(
'dart.fix.remove.unnecessaryNew',
DartFixKindPriority.DEFAULT,
'Remove unnecessary new keyword',
"Remove unnecessary 'new' keyword",
);
static const REMOVE_UNNECESSARY_NEW_MULTI = FixKind(
'dart.fix.remove.unnecessaryNew.multi',
DartFixKindPriority.IN_FILE,
'Remove unnecessary new keywords everywhere in file',
"Remove unnecessary 'new' keywords everywhere in file",
);
static const REMOVE_UNNECESSARY_CONTAINER = FixKind(
'dart.fix.remove.unnecessaryContainer',

View file

@ -11,10 +11,225 @@ import 'fix_processor.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(AddExplicitCastMultiTest);
defineReflectiveTests(AddExplicitCastTest);
});
}
@reflectiveTest
class AddExplicitCastMultiTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.ADD_EXPLICIT_CAST_MULTI;
Future<void> test_assignment_general() async {
await resolveTestCode('''
f(A a) {
B b, b2;
b = a;
b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b, b2;
b = a as B;
b2 = a as B;
}
class A {}
class B {}
''');
}
Future<void> test_assignment_list() async {
await resolveTestCode('''
f(List<A> a) {
List<B> b, b2;
b = a.where((e) => e is B).toList();
b2 = a.where((e) => e is B).toList();
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(List<A> a) {
List<B> b, b2;
b = a.where((e) => e is B).cast<B>().toList();
b2 = a.where((e) => e is B).cast<B>().toList();
}
class A {}
class B {}
''');
}
Future<void> test_assignment_map() async {
await resolveTestCode('''
f(Map<A, B> a) {
Map<B, A> b, b2;
b = a;
b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Map<A, B> a) {
Map<B, A> b, b2;
b = a.cast<B, A>();
b2 = a.cast<B, A>();
}
class A {}
class B {}
''');
}
Future<void> test_assignment_needsParens() async {
await resolveTestCode('''
f(A a) {
B b, b2;
b = a..m();
b2 = a..m();
}
class A {
int m() => 0;
}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b, b2;
b = (a..m()) as B;
b2 = (a..m()) as B;
}
class A {
int m() => 0;
}
class B {}
''');
}
Future<void> test_assignment_set() async {
await resolveTestCode('''
f(Set<A> a) {
Set<B> b, b2;
b = a;
b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Set<A> a) {
Set<B> b, b2;
b = a.cast<B>();
b2 = a.cast<B>();
}
class A {}
class B {}
''');
}
Future<void> test_declaration_general() async {
await resolveTestCode('''
f(A a) {
B b = a;
B b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b = a as B;
B b2 = a as B;
}
class A {}
class B {}
''');
}
Future<void> test_declaration_list() async {
await resolveTestCode('''
f(List<A> a) {
List<B> b = a.where((e) => e is B).toList();
List<B> b2 = a.where((e) => e is B).toList();
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(List<A> a) {
List<B> b = a.where((e) => e is B).cast<B>().toList();
List<B> b2 = a.where((e) => e is B).cast<B>().toList();
}
class A {}
class B {}
''');
}
Future<void> test_declaration_map() async {
await resolveTestCode('''
f(Map<A, B> a) {
Map<B, A> b = a;
Map<B, A> b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Map<A, B> a) {
Map<B, A> b = a.cast<B, A>();
Map<B, A> b2 = a.cast<B, A>();
}
class A {}
class B {}
''');
}
Future<void> test_declaration_needsParens() async {
await resolveTestCode('''
f(A a) {
B b = a..m();
B b2 = a..m();
}
class A {
int m() => 0;
}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b = (a..m()) as B;
B b2 = (a..m()) as B;
}
class A {
int m() => 0;
}
class B {}
''');
}
Future<void> test_declaration_set() async {
await resolveTestCode('''
f(Set<A> a) {
Set<B> b = a;
Set<B> b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Set<A> a) {
Set<B> b = a.cast<B>();
Set<B> b2 = a.cast<B>();
}
class A {}
class B {}
''');
}
}
@reflectiveTest
class AddExplicitCastTest extends FixProcessorTest {
@override
@ -83,28 +298,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_assignment_general_all() async {
await resolveTestCode('''
f(A a) {
B b, b2;
b = a;
b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b, b2;
b = a as B;
b2 = a as B;
}
class A {}
class B {}
''');
}
Future<void> test_assignment_iterable_cast() async {
await resolveTestCode('''
f(Set<A> a) {
@ -168,28 +361,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_assignment_list_all() async {
await resolveTestCode('''
f(List<A> a) {
List<B> b, b2;
b = a.where((e) => e is B).toList();
b2 = a.where((e) => e is B).toList();
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(List<A> a) {
List<B> b, b2;
b = a.where((e) => e is B).cast<B>().toList();
b2 = a.where((e) => e is B).cast<B>().toList();
}
class A {}
class B {}
''');
}
Future<void> test_assignment_map() async {
await resolveTestCode('''
f(Map<A, B> a) {
@ -211,28 +382,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_assignment_map_all() async {
await resolveTestCode('''
f(Map<A, B> a) {
Map<B, A> b, b2;
b = a;
b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Map<A, B> a) {
Map<B, A> b, b2;
b = a.cast<B, A>();
b2 = a.cast<B, A>();
}
class A {}
class B {}
''');
}
Future<void> test_assignment_map_cast() async {
await resolveTestCode('''
f(Map<A, B> a) {
@ -279,32 +428,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_assignment_needsParens_all() async {
await resolveTestCode('''
f(A a) {
B b, b2;
b = a..m();
b2 = a..m();
}
class A {
int m() => 0;
}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b, b2;
b = (a..m()) as B;
b2 = (a..m()) as B;
}
class A {
int m() => 0;
}
class B {}
''');
}
Future<void> test_assignment_null() async {
await resolveTestCode('''
void f(int x) {
@ -344,28 +467,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_assignment_set_all() async {
await resolveTestCode('''
f(Set<A> a) {
Set<B> b, b2;
b = a;
b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Set<A> a) {
Set<B> b, b2;
b = a.cast<B>();
b2 = a.cast<B>();
}
class A {}
class B {}
''');
}
Future<void> test_cast() async {
await resolveTestCode('''
f(List<A> a) {
@ -397,26 +498,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_declaration_general_all() async {
await resolveTestCode('''
f(A a) {
B b = a;
B b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b = a as B;
B b2 = a as B;
}
class A {}
class B {}
''');
}
Future<void> test_declaration_list() async {
await resolveTestCode('''
f(List<A> a) {
@ -436,26 +517,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_declaration_list_all() async {
await resolveTestCode('''
f(List<A> a) {
List<B> b = a.where((e) => e is B).toList();
List<B> b2 = a.where((e) => e is B).toList();
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(List<A> a) {
List<B> b = a.where((e) => e is B).cast<B>().toList();
List<B> b2 = a.where((e) => e is B).cast<B>().toList();
}
class A {}
class B {}
''');
}
Future<void> test_declaration_map() async {
await resolveTestCode('''
f(Map<A, B> a) {
@ -475,26 +536,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_declaration_map_all() async {
await resolveTestCode('''
f(Map<A, B> a) {
Map<B, A> b = a;
Map<B, A> b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Map<A, B> a) {
Map<B, A> b = a.cast<B, A>();
Map<B, A> b2 = a.cast<B, A>();
}
class A {}
class B {}
''');
}
Future<void> test_declaration_needsParens() async {
await resolveTestCode('''
f(A a) {
@ -518,30 +559,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_declaration_needsParens_all() async {
await resolveTestCode('''
f(A a) {
B b = a..m();
B b2 = a..m();
}
class A {
int m() => 0;
}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(A a) {
B b = (a..m()) as B;
B b2 = (a..m()) as B;
}
class A {
int m() => 0;
}
class B {}
''');
}
Future<void> test_declaration_set() async {
await resolveTestCode('''
f(Set<A> a) {
@ -561,26 +578,6 @@ class B {}
''');
}
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_declaration_set_all() async {
await resolveTestCode('''
f(Set<A> a) {
Set<B> b = a;
Set<B> b2 = a;
}
class A {}
class B {}
''');
await assertHasFixAllFix(CompileTimeErrorCode.INVALID_ASSIGNMENT, '''
f(Set<A> a) {
Set<B> b = a.cast<B>();
Set<B> b2 = a.cast<B>();
}
class A {}
class B {}
''');
}
Future<void> test_notExpression_incomplete() async {
await resolveTestCode(r'''
void foo(int a) {