Skip to content

Commit 32d3c44

Browse files
parloughCommit Queue
authored andcommitted
[analysis_server] Account for late keyword in convert to getter assist
Change-Id: I421ec8c9393ba1f602eee98010f9e5b6114dd7a0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391060 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent ec6dfd4 commit 32d3c44

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ConvertIntoGetter extends ResolvedCorrectionProducer {
4242
if (fieldDeclaration == null) {
4343
return;
4444
}
45-
// The field must be final and has only one variable.
45+
// The field must be final and have only one variable.
4646
var fieldList = fieldDeclaration.fields;
4747
var finalKeyword = fieldList.keyword;
4848
if (finalKeyword == null || fieldList.variables.length != 1) {
@@ -64,7 +64,9 @@ class ConvertIntoGetter extends ResolvedCorrectionProducer {
6464
code += ' ${field.name.lexeme}';
6565
code += ' => ${utils.getNodeText(initializer)}';
6666
code += ';';
67-
var replacementRange = range.startEnd(finalKeyword, fieldDeclaration);
67+
68+
var startingKeyword = fieldList.lateKeyword ?? finalKeyword;
69+
var replacementRange = range.startEnd(startingKeyword, fieldDeclaration);
6870
await builder.addDartFileEdit(file, (builder) {
6971
builder.addSimpleReplacement(replacementRange, code);
7072
});

pkg/analysis_server/test/src/services/correction/assist/convert_into_getter_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ class ConvertIntoGetterTest extends AssistProcessorTest {
1919
@override
2020
AssistKind get kind => DartAssistKind.CONVERT_INTO_GETTER;
2121

22+
Future<void> test_late() async {
23+
await resolveTestCode('''
24+
class A {
25+
late final int f = 1 + 2;
26+
}
27+
''');
28+
await assertHasAssistAt('f =', '''
29+
class A {
30+
int get f => 1 + 2;
31+
}
32+
''');
33+
}
34+
2235
Future<void> test_noInitializer() async {
2336
verifyNoTestUnitErrors = false;
2437
await resolveTestCode('''

0 commit comments

Comments
 (0)