@@ -8,6 +8,7 @@ import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
8
8
import 'package:analyzer/dart/ast/ast.dart' ;
9
9
import 'package:analyzer/dart/element/type.dart' ;
10
10
import 'package:analyzer/src/dart/ast/extensions.dart' ;
11
+ import 'package:analyzer/src/utilities/dot_shorthands.dart' ;
11
12
import 'package:analyzer_plugin/utilities/assist/assist.dart' ;
12
13
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart' ;
13
14
import 'package:analyzer_plugin/utilities/fixes/fixes.dart' ;
@@ -56,36 +57,42 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
56
57
return ;
57
58
}
58
59
var initializer = variables[0 ].initializer;
59
- String ? typeArgumentsText;
60
- int ? typeArgumentsOffset;
61
- if (type is NamedType ) {
60
+ String ? insertionText;
61
+ int ? insertionOffset;
62
+ if (initializer != null && isDotShorthand (initializer)) {
63
+ // Inserts the type before the dot shorthand (e.g. `E.a` where type is
64
+ // `E`) because we erase the required context type when we replace the
65
+ // declared type with `var`.
66
+ insertionText = utils.getNodeText (type);
67
+ insertionOffset = initializer.beginToken.offset;
68
+ } else if (type is NamedType ) {
62
69
var typeArguments = type.typeArguments;
63
70
if (typeArguments != null ) {
64
71
if (initializer is CascadeExpression ) {
65
72
initializer = initializer.target;
66
73
}
67
74
if (initializer is TypedLiteral ) {
68
75
if (initializer.typeArguments == null ) {
69
- typeArgumentsText = utils.getNodeText (typeArguments);
76
+ insertionText = utils.getNodeText (typeArguments);
70
77
if (initializer is ListLiteral ) {
71
- typeArgumentsOffset = initializer.leftBracket.offset;
78
+ insertionOffset = initializer.leftBracket.offset;
72
79
} else if (initializer is SetOrMapLiteral ) {
73
- typeArgumentsOffset = initializer.leftBracket.offset;
80
+ insertionOffset = initializer.leftBracket.offset;
74
81
} else {
75
82
throw StateError ('Unhandled subclass of TypedLiteral' );
76
83
}
77
84
}
78
85
} else if (initializer is InstanceCreationExpression ) {
79
86
if (initializer.constructorName.type.typeArguments == null ) {
80
- typeArgumentsText = utils.getNodeText (typeArguments);
81
- typeArgumentsOffset = initializer.constructorName.type.end;
87
+ insertionText = utils.getNodeText (typeArguments);
88
+ insertionOffset = initializer.constructorName.type.end;
82
89
}
83
90
}
84
91
}
85
92
}
86
93
if (initializer is SetOrMapLiteral &&
87
94
initializer.typeArguments == null &&
88
- typeArgumentsText == null ) {
95
+ insertionText == null ) {
89
96
// This is to prevent the fix from converting a valid map or set literal
90
97
// into an ambiguous literal. We could apply this in more places
91
98
// by examining the elements of the collection.
@@ -94,8 +101,8 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
94
101
await builder.addDartFileEdit (file, (builder) {
95
102
builder.addSimpleReplacement (range.node (type), 'var' );
96
103
97
- if (typeArgumentsText != null && typeArgumentsOffset != null ) {
98
- builder.addSimpleInsertion (typeArgumentsOffset, typeArgumentsText );
104
+ if (insertionText != null && insertionOffset != null ) {
105
+ builder.addSimpleInsertion (insertionOffset, insertionText );
99
106
}
100
107
});
101
108
} else if (parent is DeclaredIdentifier &&
@@ -105,15 +112,21 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
105
112
return ;
106
113
}
107
114
108
- String ? typeArgumentsText;
109
- int ? typeArgumentsOffset;
110
- if (type is NamedType ) {
115
+ String ? insertionText;
116
+ int ? insertionOffset;
117
+ var iterable = grandparent.iterable;
118
+ if (hasDependentDotShorthand (iterable) && iterable is TypedLiteral ) {
119
+ // If there's a dependent shorthand in the literal, we need to
120
+ // insert explicit type arguments to ensure we have an appropriate
121
+ // context type to resolve the dot shorthand.
122
+ insertionText = '<${utils .getNodeText (type )}>' ;
123
+ insertionOffset = iterable.beginToken.offset;
124
+ } else if (type is NamedType ) {
111
125
var typeArguments = type.typeArguments;
112
126
if (typeArguments != null ) {
113
- var iterable = grandparent.iterable;
114
127
if (iterable is TypedLiteral && iterable.typeArguments == null ) {
115
- typeArgumentsText = utils.getNodeText (typeArguments);
116
- typeArgumentsOffset = iterable.offset;
128
+ insertionText = utils.getNodeText (typeArguments);
129
+ insertionOffset = iterable.offset;
117
130
}
118
131
}
119
132
}
@@ -123,8 +136,8 @@ class ReplaceWithVar extends ResolvedCorrectionProducer {
123
136
} else {
124
137
builder.addSimpleReplacement (range.node (type), 'var' );
125
138
}
126
- if (typeArgumentsText != null && typeArgumentsOffset != null ) {
127
- builder.addSimpleInsertion (typeArgumentsOffset, typeArgumentsText );
139
+ if (insertionText != null && insertionOffset != null ) {
140
+ builder.addSimpleInsertion (insertionOffset, insertionText );
128
141
}
129
142
});
130
143
}
0 commit comments