From a75be76dafff1843fb4b80b3370c87707b0f7c05 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Tue, 2 Jun 2026 13:04:55 -0700 Subject: [PATCH] Fix bug in convertIntoBlockBody I ran across this bug while testing to see what work was already done. I didn't take the time to create an issue. The bug is that an assist was being offered for `class C ^{}` to convert the class body into a block. The result, for an empty block, was just to add an extra space before the block body. For a non-empty block, the result was to delete everything inside the block. Change-Id: I0342b681ef67b5e659a00126a998d63d593223fd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/508702 Reviewed-by: Samuel Rawlins --- .../dart/convert_into_block_body.dart | 10 +++-- .../assist/convert_into_block_body_test.dart | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart index fa46209f1c5..cbe2eedf2da 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_into_block_body.dart @@ -82,10 +82,12 @@ class ConvertIntoBlockBody extends ResolvedCorrectionProducer { if (body == null) { return; } - var bodyRange = body.sourceRange; - await builder.addDartFileEdit(file, (builder) { - builder.addSimpleReplacement(bodyRange, ' {}'); - }); + if (body is EmptyClassBody || body is EmptyEnumBody) { + var bodyRange = body.sourceRange; + await builder.addDartFileEdit(file, (builder) { + builder.addSimpleReplacement(bodyRange, ' {}'); + }); + } } Future _computeMissingFunctionBody( diff --git a/pkg/analysis_server/test/src/services/correction/assist/convert_into_block_body_test.dart b/pkg/analysis_server/test/src/services/correction/assist/convert_into_block_body_test.dart index df1978dd665..07247c62882 100644 --- a/pkg/analysis_server/test/src/services/correction/assist/convert_into_block_body_test.dart +++ b/pkg/analysis_server/test/src/services/correction/assist/convert_into_block_body_test.dart @@ -80,6 +80,13 @@ class C {} '''); } + Future test_container_class_block() async { + await resolveTestCode(''' +class C ^{} +'''); + await assertNoAssist(); + } + Future test_container_enum() async { await resolveTestCode( ''' @@ -92,6 +99,16 @@ enum E {} '''); } + Future test_container_enum_block() async { + await resolveTestCode( + ''' +enum E ^{} +''', + ignore: [diag.enumWithoutConstants], + ); + await assertNoAssist(); + } + Future test_container_extension() async { await resolveTestCode(''' extension E on int^; @@ -101,6 +118,13 @@ extension E on int {} '''); } + Future test_container_extension_block() async { + await resolveTestCode(''' +extension E on int ^{} +'''); + await assertNoAssist(); + } + Future test_container_extensionType() async { await resolveTestCode(''' extension type ^E(int i); @@ -110,6 +134,13 @@ extension type E(int i) {} '''); } + Future test_container_extensionType_block() async { + await resolveTestCode(''' +extension type E(int i) ^{} +'''); + await assertNoAssist(); + } + Future test_container_mixin() async { await resolveTestCode(''' mixin M^; @@ -119,6 +150,13 @@ mixin M {} '''); } + Future test_container_mixin_block() async { + await resolveTestCode(''' +mixin M ^{} +'''); + await assertNoAssist(); + } + Future test_inExpression() async { await resolveTestCode(''' void f() => ^123;