Handle TopLevelVariableDeclaration in ConvertToFunctionDeclaration.

Closes https://github.com/dart-lang/sdk/pull/61819

GitOrigin-RevId: 6113f840d8179a92fd283830c1f417a05c3fdc80
Change-Id: I33d684af2ce9dd050679235c5bd2182fed1f1dfe
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/457560
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_function_declaration.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_function_declaration.dart
index 674fb9b..cc0093f 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/convert_to_function_declaration.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_to_function_declaration.dart
@@ -48,6 +48,8 @@
       grandParentSemicolon = grandParent.semicolon;
     } else if (grandParent is FieldDeclaration) {
       grandParentSemicolon = grandParent.semicolon;
+    } else if (grandParent is TopLevelVariableDeclaration) {
+      grandParentSemicolon = grandParent.semicolon;
     } else {
       return;
     }
diff --git a/pkg/analysis_server/test/src/services/correction/fix/convert_to_function_declaration_test.dart b/pkg/analysis_server/test/src/services/correction/fix/convert_to_function_declaration_test.dart
index 44aa9b2..0dc05dc 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/convert_to_function_declaration_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/convert_to_function_declaration_test.dart
@@ -270,6 +270,23 @@
 ''');
   }
 
+  Future<void> test_top_level_declaration() async {
+    await resolveTestCode('''
+final v1 = () => 42;
+
+void f() {
+  v1();
+}
+''');
+    await assertHasFix('''
+int v1() => 42;
+
+void f() {
+  v1();
+}
+''');
+  }
+
   Future<void> test_type() async {
     await resolveTestCode('''
 void f() {