mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 02:57:35 +00:00
[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:
parent
c39ac36ed3
commit
a6f4fbe17b
|
@ -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('{}');
|
||||
});
|
||||
|
|
|
@ -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 = {};
|
||||
''');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue