| commit | 47f4ff25b7639c765156df145b131373e24f6652 | [log] [tgz] |
|---|---|---|
| author | Agam Agarwal <agammaster@gmail.com> | Sun Oct 26 15:14:41 2025 -0700 |
| committer | Commit Queue <dart-scoped@luci-project-accounts.iam.gserviceaccount.com> | Sun Oct 26 15:14:41 2025 -0700 |
| tree | b5a58d32c3633f1448c4f62b13d2b206fa324bb7 | |
| parent | b668fc8d3f3db7cd1439f5fd2bb58fb4a5cdcc27 [diff] |
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() {