[analysis_server] ConvertToMapLiteral to handle types with typedef

Fixes #50581

Change-Id: Ieeb4c6730036e112bcf37353a18535f342fd28a5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/275900
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Ahmed Ashour 2022-12-15 22:09:46 +00:00 committed by Commit Queue
parent c39ac36ed3
commit a6f4fbe17b
2 changed files with 38 additions and 0 deletions

View file

@ -52,6 +52,18 @@ class ConvertToMapLiteral extends CorrectionProducer {
// Extract the information needed to build the edit.
//
var constructorTypeArguments = creation.constructorName.type.typeArguments;
List<DartType>? staticTypeArguments;
if (constructorTypeArguments == null) {
var variableDeclarationList =
creation.thisOrAncestorOfType<VariableDeclarationList>();
if (variableDeclarationList?.type == null) {
staticTypeArguments = type.typeArguments;
if (staticTypeArguments.first.isDynamic &&
staticTypeArguments.last.isDynamic) {
staticTypeArguments = null;
}
}
}
//
// Build the edit.
//
@ -59,6 +71,10 @@ class ConvertToMapLiteral extends CorrectionProducer {
builder.addReplacement(range.node(creation), (builder) {
if (constructorTypeArguments != null) {
builder.write(utils.getNodeText(constructorTypeArguments));
} else if (staticTypeArguments?.isNotEmpty ?? false) {
builder.write('<');
builder.writeTypes(staticTypeArguments);
builder.write('>');
}
builder.write('{}');
});

View file

@ -85,6 +85,28 @@ var m = Map<String, int>();
''');
await assertHasFix('''
var m = <String, int>{};
''');
}
Future<void> test_typedef() async {
await resolveTestCode('''
typedef M = Map<String, int>;
var m = M();
''');
await assertHasFix('''
typedef M = Map<String, int>;
var m = <String, int>{};
''');
}
Future<void> test_typedef_declaredType() async {
await resolveTestCode('''
typedef M = Map<String, int>;
Map<String, int> m = M();
''');
await assertHasFix('''
typedef M = Map<String, int>;
Map<String, int> m = {};
''');
}
}