From 50b2943a1b3879897840257bccbd5a678435c57f Mon Sep 17 00:00:00 2001 From: Agam Agarwal Date: Sat, 25 Oct 2025 01:00:53 +0530 Subject: [PATCH 1/2] Handle FieldDeclaration in ConvertToFunctionDeclaration --- .../dart/convert_to_function_declaration.dart | 12 +++++++++-- .../convert_to_function_declaration_test.dart | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) 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 70a41839defe..674fb9b74cb7 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 @@ -4,6 +4,7 @@ import 'package:analysis_server/src/services/correction/fix.dart'; import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; +import 'package:analyzer/dart/ast/token.dart'; import 'package:analyzer/dart/ast/visitor.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; @@ -42,7 +43,14 @@ class ConvertToFunctionDeclaration extends ResolvedCorrectionProducer { var variables = parent.variables; var grandParent = parent.parent; - if (grandParent is! VariableDeclarationStatement) return; + Token grandParentSemicolon; + if (grandParent is VariableDeclarationStatement) { + grandParentSemicolon = grandParent.semicolon; + } else if (grandParent is FieldDeclaration) { + grandParentSemicolon = grandParent.semicolon; + } else { + return; + } var previous = _previous(variables, node); var next = _next(variables, node); @@ -152,7 +160,7 @@ class ConvertToFunctionDeclaration extends ResolvedCorrectionProducer { } } else if (initializer is FunctionExpression && initializer.body is BlockFunctionBody) { - builder.addDeletion(range.token(grandParent.semicolon)); + builder.addDeletion(range.token(grandParentSemicolon)); } }); } 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 ff8caf2474c2..ba1f7355b0a9 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 @@ -320,6 +320,27 @@ void f() { } v1(0); } +'''); + } + + Future test_classField() async { + await resolveTestCode(''' +class F { + final v1 = () => 42; + + void f() { + v1(); + } +} +'''); + await assertHasFix(''' +class F { + int v1() => 42; + + void f() { + v1(); + } +} '''); } } From cf8b55d8926d860c444f7931f70985531850f1bf Mon Sep 17 00:00:00 2001 From: Agam Agarwal Date: Sat, 25 Oct 2025 03:57:24 +0530 Subject: [PATCH 2/2] Sort test functions in convert_to_function_declaration_test to satisfy verify_sorted_test --- .../convert_to_function_declaration_test.dart | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) 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 ba1f7355b0a9..44aa9b283337 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 @@ -113,6 +113,27 @@ void f() { '''); } + Future test_class_field() async { + await resolveTestCode(''' +class F { + final v1 = () => 42; + + void f() { + v1(); + } +} +'''); + await assertHasFix(''' +class F { + int v1() => 42; + + void f() { + v1(); + } +} +'''); + } + Future test_declaration_different() async { await resolveTestCode(''' void f() { @@ -320,27 +341,6 @@ void f() { } v1(0); } -'''); - } - - Future test_classField() async { - await resolveTestCode(''' -class F { - final v1 = () => 42; - - void f() { - v1(); - } -} -'''); - await assertHasFix(''' -class F { - int v1() => 42; - - void f() { - v1(); - } -} '''); } }