From a6f4fbe17b59b824e65e3e75d670909d2d785cb1 Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Thu, 15 Dec 2022 22:09:46 +0000 Subject: [PATCH] [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 Commit-Queue: Brian Wilkerson --- .../dart/convert_to_map_literal.dart | 16 ++++++++++++++ .../fix/convert_to_map_literal_test.dart | 22 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart index df9e567109e..2946e196144 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_map_literal.dart @@ -52,6 +52,18 @@ class ConvertToMapLiteral extends CorrectionProducer { // Extract the information needed to build the edit. // var constructorTypeArguments = creation.constructorName.type.typeArguments; + List? staticTypeArguments; + if (constructorTypeArguments == null) { + var variableDeclarationList = + creation.thisOrAncestorOfType(); + 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('{}'); }); diff --git a/pkg/analysis_server/test/src/services/correction/fix/convert_to_map_literal_test.dart b/pkg/analysis_server/test/src/services/correction/fix/convert_to_map_literal_test.dart index f29794a7c9a..3b1ea9f76b1 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/convert_to_map_literal_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/convert_to_map_literal_test.dart @@ -85,6 +85,28 @@ var m = Map(); '''); await assertHasFix(''' var m = {}; +'''); + } + + Future test_typedef() async { + await resolveTestCode(''' +typedef M = Map; +var m = M(); +'''); + await assertHasFix(''' +typedef M = Map; +var m = {}; +'''); + } + + Future test_typedef_declaredType() async { + await resolveTestCode(''' +typedef M = Map; +Map m = M(); +'''); + await assertHasFix(''' +typedef M = Map; +Map m = {}; '''); } }