Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ void f() {
''');
}

Future<void> 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<void> test_declaration_different() async {
await resolveTestCode('''
void f() {
Expand Down