[DAS] Makes convert into field assist consider extensions correctly

R=helinx@google.com

Fixes: https://github.com/dart-lang/sdk/issues/60060
Change-Id: I596a473a293fee83f8eb0de0cc30a90aeef1ff51
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412501
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Auto-Submit: Felipe Morschel <git@fmorschel.dev>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart
index 532058f..72666e4 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_final_field.dart
@@ -17,9 +17,8 @@
 
   @override
   CorrectionApplicability get applicability =>
-          // TODO(applicability): comment on why.
-          CorrectionApplicability
-          .singleLocation;
+      // TODO(applicability): comment on why.
+      CorrectionApplicability.singleLocation;
 
   @override
   AssistKind get assistKind => DartAssistKind.CONVERT_INTO_FINAL_FIELD;
@@ -54,6 +53,15 @@
       return;
     }
 
+    // The getter must not be in an extension or extension type unless it is
+    // static.
+    if (!getterElement.isStatic) {
+      switch (getterElement.enclosingElement2) {
+        case ExtensionElement2() || ExtensionTypeElement2():
+          return;
+      }
+    }
+
     var variable = getterElement.variable3;
     if (variable == null) {
       return;
diff --git a/pkg/analysis_server/test/src/services/correction/assist/convert_into_final_field_test.dart b/pkg/analysis_server/test/src/services/correction/assist/convert_into_final_field_test.dart
index c80b319..bb94ead 100644
--- a/pkg/analysis_server/test/src/services/correction/assist/convert_into_final_field_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/assist/convert_into_final_field_test.dart
@@ -34,6 +34,50 @@
 ''');
   }
 
+  Future<void> test_extension_instance() async {
+    await resolveTestCode('''
+extension E on int {
+  int get foo => 0;
+}
+''');
+    await assertNoAssistAt('get foo');
+  }
+
+  Future<void> test_extension_static() async {
+    await resolveTestCode('''
+extension E on int {
+  static int get foo => 0;
+}
+''');
+    await assertHasAssistAt('get foo', '''
+extension E on int {
+  static final int foo = 0;
+}
+''');
+  }
+
+  Future<void> test_extensionType_instance() async {
+    await resolveTestCode('''
+extension type E(int v) {
+  int get foo => 0;
+}
+''');
+    await assertNoAssistAt('get foo');
+  }
+
+  Future<void> test_extensionType_static() async {
+    await resolveTestCode('''
+extension type E(int v) {
+  static int get foo => 0;
+}
+''');
+    await assertHasAssistAt('get foo', '''
+extension type E(int v) {
+  static final int foo = 0;
+}
+''');
+  }
+
   Future<void> test_hasOverride() async {
     await resolveTestCode('''
 const myAnnotation = const Object();