Skip to content

Commit 64a9566

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Makes convert into field assist consider extensions correctly
[email protected] Fixes: #60060 Change-Id: I596a473a293fee83f8eb0de0cc30a90aeef1ff51 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412501 Commit-Queue: Phil Quitslund <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent a30dc89 commit 64a9566

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ class ConvertIntoFinalField extends ResolvedCorrectionProducer {
1717

1818
@override
1919
CorrectionApplicability get applicability =>
20-
// TODO(applicability): comment on why.
21-
CorrectionApplicability
22-
.singleLocation;
20+
// TODO(applicability): comment on why.
21+
CorrectionApplicability.singleLocation;
2322

2423
@override
2524
AssistKind get assistKind => DartAssistKind.CONVERT_INTO_FINAL_FIELD;
@@ -54,6 +53,15 @@ class ConvertIntoFinalField extends ResolvedCorrectionProducer {
5453
return;
5554
}
5655

56+
// The getter must not be in an extension or extension type unless it is
57+
// static.
58+
if (!getterElement.isStatic) {
59+
switch (getterElement.enclosingElement2) {
60+
case ExtensionElement2() || ExtensionTypeElement2():
61+
return;
62+
}
63+
}
64+
5765
var variable = getterElement.variable3;
5866
if (variable == null) {
5967
return;

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,50 @@ class A {
3434
''');
3535
}
3636

37+
Future<void> test_extension_instance() async {
38+
await resolveTestCode('''
39+
extension E on int {
40+
int get foo => 0;
41+
}
42+
''');
43+
await assertNoAssistAt('get foo');
44+
}
45+
46+
Future<void> test_extension_static() async {
47+
await resolveTestCode('''
48+
extension E on int {
49+
static int get foo => 0;
50+
}
51+
''');
52+
await assertHasAssistAt('get foo', '''
53+
extension E on int {
54+
static final int foo = 0;
55+
}
56+
''');
57+
}
58+
59+
Future<void> test_extensionType_instance() async {
60+
await resolveTestCode('''
61+
extension type E(int v) {
62+
int get foo => 0;
63+
}
64+
''');
65+
await assertNoAssistAt('get foo');
66+
}
67+
68+
Future<void> test_extensionType_static() async {
69+
await resolveTestCode('''
70+
extension type E(int v) {
71+
static int get foo => 0;
72+
}
73+
''');
74+
await assertHasAssistAt('get foo', '''
75+
extension type E(int v) {
76+
static final int foo = 0;
77+
}
78+
''');
79+
}
80+
3781
Future<void> test_hasOverride() async {
3882
await resolveTestCode('''
3983
const myAnnotation = const Object();

0 commit comments

Comments
 (0)