Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a9f17d4

Browse files
pqcommit-bot@chromium.org
authored andcommitted
fix offset for var replacement in const collection literals
Fixes: dart-lang/sdk#43895 Change-Id: Iaea95178505859f33ed44d36b8a902cc135e5023 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168880 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 1f660a7 commit a9f17d4

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pkg/analysis_server/lib/src/services/correction/dart/replace_with_var.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ class ReplaceWithVar extends CorrectionProducer {
4545
if (initializer is TypedLiteral) {
4646
if (initializer.typeArguments == null) {
4747
typeArgumentsText = utils.getNodeText(type.typeArguments);
48-
typeArgumentsOffset = initializer.offset;
48+
if (initializer is ListLiteral) {
49+
typeArgumentsOffset = initializer.leftBracket.offset;
50+
} else if (initializer is SetOrMapLiteral) {
51+
typeArgumentsOffset = initializer.leftBracket.offset;
52+
}
4953
}
5054
} else if (initializer is InstanceCreationExpression) {
5155
if (initializer.constructorName.type.typeArguments == null) {

pkg/analysis_server/test/src/services/correction/fix/replace_with_var_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ List f() {
123123
''');
124124
}
125125

126+
Future<void> test_generic_listLiteral_const() async {
127+
await resolveTestUnit('''
128+
String f() {
129+
const List<String> values = const ['a'];
130+
return values[0];
131+
}
132+
''');
133+
await assertHasFix('''
134+
String f() {
135+
const values = const <String>['a'];
136+
return values[0];
137+
}
138+
''');
139+
}
140+
126141
Future<void> test_generic_mapLiteral() async {
127142
await resolveTestUnit('''
128143
Map f() {
@@ -138,6 +153,21 @@ Map f() {
138153
''');
139154
}
140155

156+
Future<void> test_generic_mapLiteral_const() async {
157+
await resolveTestUnit('''
158+
Map f() {
159+
const Map<String, int> m = const {};
160+
return m;
161+
}
162+
''');
163+
await assertHasFix('''
164+
Map f() {
165+
const m = const <String, int>{};
166+
return m;
167+
}
168+
''');
169+
}
170+
141171
Future<void> test_generic_setLiteral() async {
142172
await resolveTestUnit('''
143173
Set f() {
@@ -163,6 +193,21 @@ Set f() {
163193
await assertNoFix();
164194
}
165195

196+
Future<void> test_generic_setLiteral_const() async {
197+
await resolveTestUnit('''
198+
String f() {
199+
const Set<String> s = const {'a'};
200+
return s.first;
201+
}
202+
''');
203+
await assertHasFix('''
204+
String f() {
205+
const s = const <String>{'a'};
206+
return s.first;
207+
}
208+
''');
209+
}
210+
166211
Future<void> test_simple() async {
167212
await resolveTestUnit('''
168213
String f() {

0 commit comments

Comments
 (0)