From 4fbb4dc6ee23f905d24ba866cdedb17849ab65ea Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Thu, 5 Jun 2025 14:30:09 -0700 Subject: [PATCH] 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 Commit-Queue: Brian Wilkerson --- CHANGELOG.md | 1 + .../dart/convert_field_formal_to_normal.dart | 2 +- .../convert_field_formal_to_normal_test.dart | 15 +++++++++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f16430f2e22..f03681d1cc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_field_formal_to_normal.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_field_formal_to_normal.dart index fb434a3aa03..f437ba737d2 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_field_formal_to_normal.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_field_formal_to_normal.dart @@ -23,7 +23,7 @@ class ConvertFieldFormalToNormal extends ResolvedCorrectionProducer { @override Future 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; diff --git a/pkg/analysis_server/test/src/services/correction/assist/convert_field_formal_to_normal_test.dart b/pkg/analysis_server/test/src/services/correction/assist/convert_field_formal_to_normal_test.dart index 7c3add339b5..785f820d0f6 100644 --- a/pkg/analysis_server/test/src/services/correction/assist/convert_field_formal_to_normal_test.dart +++ b/pkg/analysis_server/test/src/services/correction/assist/convert_field_formal_to_normal_test.dart @@ -281,19 +281,30 @@ class C { '''); } - Future test_withFunctionTypedField() async { + Future test_withFunctionTypedField_functionTypedParameter() async { await resolveTestCode(''' class C { void Function() f; C({required this.f^()}); } +'''); + await assertNoAssist(); + } + + Future 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; } '''); }