Fix the field formal parameter to normal parameter assist

This disables the assist if the field formal parameter happens to also
be an older style function typed parameter. As written the assist
produces invalid code, and the older style is strongly discouraged so
not supporting it shouldn't impact very many users.

Change-Id: I44a3942b4656265bb1cbbba29e68fff61962b689
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433260
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson
2025-06-05 14:30:09 -07:00
committed by Commit Queue
parent ef4e39a6ef
commit 4fbb4dc6ee
3 changed files with 15 additions and 3 deletions
+1
View File
@@ -18,6 +18,7 @@ constraint][language version] lower bound to 3.9 or greater (`sdk: '^3.9.0'`).
- Add the [`switch_on_type`][] lint rule.
- Add the [`unnecessary_unawaited`][] lint rule.
- Add an assist to convert a field formal parameter to a normal parameter.
[`switch_on_type`]: http://dart.dev/lints/switch_on_type
[`unnecessary_unawaited`]: http://dart.dev/lints/unnecessary_unawaited
@@ -23,7 +23,7 @@ class ConvertFieldFormalToNormal extends ResolvedCorrectionProducer {
@override
Future<void> compute(ChangeBuilder builder) async {
var parameter = node;
if (parameter is! FieldFormalParameter) {
if (parameter is! FieldFormalParameter || parameter.parameters != null) {
return;
}
var field = parameter.declaredFragment?.element.field2;
@@ -281,19 +281,30 @@ class C {
''');
}
Future<void> test_withFunctionTypedField() async {
Future<void> test_withFunctionTypedField_functionTypedParameter() async {
await resolveTestCode('''
class C {
void Function() f;
C({required this.f^()});
}
''');
await assertNoAssist();
}
Future<void> test_withFunctionTypedField_normalParameter() async {
await resolveTestCode('''
class C {
void Function() f;
C({required this.f^});
}
''');
await assertHasAssist('''
class C {
void Function() f;
C({required void Function() f()}) : f = f;
C({required void Function() f}) : f = f;
}
''');
}