Skip to content

Commit

Permalink
[analyzer] RemoveTypeAnnotation to handle type arguments
Browse files Browse the repository at this point in the history
Fixes flutter#49227

Change-Id: I0f754d84e3e4e89800abdec26925229587202bca
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247968
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
  • Loading branch information
asashour authored and Commit Bot committed Jun 12, 2022
1 parent 4ecc7ff commit 57b192f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class RemoveTypeAnnotation extends CorrectionProducer {
Future<void> _removeFromDeclarationList(
ChangeBuilder builder, VariableDeclarationList declarationList) async {
// we need a type
var typeNode = declarationList.type;
if (typeNode == null) {
var type = declarationList.type;
if (type == null) {
return;
}
// ignore if an incomplete variable declaration
Expand All @@ -63,19 +63,60 @@ class RemoveTypeAnnotation extends CorrectionProducer {
if (selectionOffset > firstVariable.name.end) {
return;
}

var initializer = firstVariable.initializer;
// The variable must have an initializer, otherwise there is no other
// source for its type.
if (firstVariable.initializer == null) {
if (initializer == null) {
return;
}

String? typeArgumentsText;
int? typeArgumentsOffset;
if (type is NamedType) {
var typeArguments = type.typeArguments;
if (typeArguments != null) {
if (initializer is CascadeExpression) {
initializer = initializer.target;
}
if (initializer is TypedLiteral) {
if (initializer.typeArguments == null) {
typeArgumentsText = utils.getNodeText(typeArguments);
if (initializer is ListLiteral) {
typeArgumentsOffset = initializer.leftBracket.offset;
} else if (initializer is SetOrMapLiteral) {
typeArgumentsOffset = initializer.leftBracket.offset;
} else {
throw StateError('Unhandled subclass of TypedLiteral');
}
}
} else if (initializer is InstanceCreationExpression) {
if (initializer.constructorName.type.typeArguments == null) {
typeArgumentsText = utils.getNodeText(typeArguments);
typeArgumentsOffset = initializer.constructorName.type.end;
}
}
}
}
if (initializer is SetOrMapLiteral &&
initializer.typeArguments == null &&
typeArgumentsText == null) {
// This is to prevent the fix from converting a valid map or set literal
// into an ambiguous literal. We could apply this in more places
// by examining the elements of the collection.
return;
}
var keyword = declarationList.keyword;
await builder.addDartFileEdit(file, (builder) {
var typeRange = range.startStart(typeNode, firstVariable);
var typeRange = range.startStart(type, firstVariable);
if (keyword != null && keyword.lexeme != 'var') {
builder.addSimpleReplacement(typeRange, '');
} else {
builder.addSimpleReplacement(typeRange, 'var ');
}
if (typeArgumentsText != null && typeArgumentsOffset != null) {
builder.addSimpleInsertion(typeArgumentsOffset, typeArgumentsText);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class ReplaceWithVar extends CorrectionProducer {
typeArgumentsOffset = initializer.leftBracket.offset;
} else if (initializer is SetOrMapLiteral) {
typeArgumentsOffset = initializer.leftBracket.offset;
} else {
throw StateError('Unhandled subclass of TypedLiteral');
}
}
} else if (initializer is InstanceCreationExpression) {
Expand All @@ -77,9 +79,9 @@ class ReplaceWithVar extends CorrectionProducer {
if (initializer is SetOrMapLiteral &&
initializer.typeArguments == null &&
typeArgumentsText == null) {
// TODO(brianwilkerson) This is to prevent the fix from converting a
// valid map or set literal into an ambiguous literal. We could apply
// this in more places by examining the elements of the collection.
// This is to prevent the fix from converting a valid map or set literal
// into an ambiguous literal. We could apply this in more places
// by examining the elements of the collection.
return;
}
await builder.addDartFileEdit(file, (builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,45 @@ class A {
await assertNoAssistAt('v');
}

Future<void> test_generic_instanceCreation_withoutArguments() async {
await resolveTestCode('''
C<int> c = C();
class C<T> {}
''');
await assertHasAssistAt('c = ', '''
var c = C<int>();
class C<T> {}
''');
}

Future<void> test_generic_listLiteral() async {
await resolveTestCode('''
List<int> l = [];
''');
await assertHasAssistAt('l = ', '''
var l = <int>[];
''');
}

Future<void> test_generic_setLiteral_ambiguous() async {
await resolveTestCode('''
Set f() {
/*caret*/Set s = {};
return s;
}
''');
await assertNoAssist();
}

Future<void> test_generic_setLiteral_cascade() async {
await resolveTestCode('''
Set<String> s = {}..addAll([]);
''');
await assertHasAssistAt('s = ', '''
var s = <String>{}..addAll([]);
''');
}

Future<void> test_instanceCreation_freeStanding() async {
await resolveTestCode('''
class A {}
Expand Down

0 comments on commit 57b192f

Please sign in to comment.