Skip to content

Commit 784024a

Browse files
AgamAgarwalCommit Queue
authored andcommitted
Handle FieldDeclaration in ConvertToFunctionDeclaration
Closes #61816 GitOrigin-RevId: f0d4776 Change-Id: Iea5a95d6f10b3cb18591b22f1d32ef8985e641bc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/457460 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 964ff4a commit 784024a

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:analysis_server/src/services/correction/fix.dart';
66
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
7+
import 'package:analyzer/dart/ast/token.dart';
78
import 'package:analyzer/dart/ast/visitor.dart';
89
import 'package:analyzer/dart/element/element.dart';
910
import 'package:analyzer/dart/element/type.dart';
@@ -42,7 +43,14 @@ class ConvertToFunctionDeclaration extends ResolvedCorrectionProducer {
4243
var variables = parent.variables;
4344

4445
var grandParent = parent.parent;
45-
if (grandParent is! VariableDeclarationStatement) return;
46+
Token grandParentSemicolon;
47+
if (grandParent is VariableDeclarationStatement) {
48+
grandParentSemicolon = grandParent.semicolon;
49+
} else if (grandParent is FieldDeclaration) {
50+
grandParentSemicolon = grandParent.semicolon;
51+
} else {
52+
return;
53+
}
4654

4755
var previous = _previous(variables, node);
4856
var next = _next(variables, node);
@@ -152,7 +160,7 @@ class ConvertToFunctionDeclaration extends ResolvedCorrectionProducer {
152160
}
153161
} else if (initializer is FunctionExpression &&
154162
initializer.body is BlockFunctionBody) {
155-
builder.addDeletion(range.token(grandParent.semicolon));
163+
builder.addDeletion(range.token(grandParentSemicolon));
156164
}
157165
});
158166
}

pkg/analysis_server/test/src/services/correction/fix/convert_to_function_declaration_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,27 @@ void f() {
113113
''');
114114
}
115115

116+
Future<void> test_class_field() async {
117+
await resolveTestCode('''
118+
class F {
119+
final v1 = () => 42;
120+
121+
void f() {
122+
v1();
123+
}
124+
}
125+
''');
126+
await assertHasFix('''
127+
class F {
128+
int v1() => 42;
129+
130+
void f() {
131+
v1();
132+
}
133+
}
134+
''');
135+
}
136+
116137
Future<void> test_declaration_different() async {
117138
await resolveTestCode('''
118139
void f() {

0 commit comments

Comments
 (0)