From a35921065b0e8cebf9f2261fb5924ada8f688669 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Mon, 20 Nov 2023 18:29:28 +0000 Subject: [PATCH] [analysis_server] Preserve field doc comments in CONVERT_CLASS_TO_ENUM assist Fixes https://github.com/dart-lang/sdk/issues/53202 Bug: https://github.com/dart-lang/sdk/issues/53202 Change-Id: Icbc1627da55eb7c680cba802fdb931a843317b3d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336860 Commit-Queue: Phil Quitslund Auto-Submit: Parker Lougheed Reviewed-by: Phil Quitslund Reviewed-by: Brian Wilkerson --- .../dart/convert_class_to_enum.dart | 14 ++- .../assist/convert_class_to_enum_test.dart | 89 +++++++++++++++++++ .../fix/convert_class_to_enum_test.dart | 29 ++++++ 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart b/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart index ef84d142f8e1..77cf8f4f7b1d 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/convert_class_to_enum.dart @@ -194,8 +194,20 @@ class _EnumDescription { .sort((first, second) => first.indexValue.compareTo(second.indexValue)); for (var field in fieldsToConvert) { // Compute the declaration of the corresponding enum constant. + var documentationComment = field.element.documentationComment; if (constantsBuffer.isNotEmpty) { - constantsBuffer.write(',$eol$indent'); + constantsBuffer.write(',$eol'); + if (documentationComment != null) { + // If the current field has a documentation comment and + // it's not the first field, add an extra new line. + constantsBuffer.write(eol); + } + constantsBuffer.write(indent); + } + if (documentationComment != null) { + constantsBuffer + .write(documentationComment.replaceAll(eol, '$eol$indent')); + constantsBuffer.write('$eol$indent'); } constantsBuffer.write(field.name); var invocation = field.instanceCreation; diff --git a/pkg/analysis_server/test/src/services/correction/assist/convert_class_to_enum_test.dart b/pkg/analysis_server/test/src/services/correction/assist/convert_class_to_enum_test.dart index 67113780c90f..f98306f4b4a0 100644 --- a/pkg/analysis_server/test/src/services/correction/assist/convert_class_to_enum_test.dart +++ b/pkg/analysis_server/test/src/services/correction/assist/convert_class_to_enum_test.dart @@ -19,6 +19,95 @@ class ConvertClassToEnumTest extends AssistProcessorTest { @override AssistKind get kind => DartAssistKind.CONVERT_CLASS_TO_ENUM; + Future test_documentationComments_mix() async { + await resolveTestCode(''' +class E { + /// AAA + static const E a = E._('a'); + static const E b = E._('b'); + + /// CCC + static const E c = E._('c'); + + final String name; + + const E._(this.name); +} +'''); + await assertHasAssistAt('E {', ''' +enum E { + /// AAA + a._('a'), + b._('b'), + + /// CCC + c._('c'); + + final String name; + + const E._(this.name); +} +'''); + } + + Future test_documentationComments_multiple() async { + await resolveTestCode(''' +class E { + /// AAA + static const E a = E._('a'); + + /// BBB + /// BBB + static const E b = E._('b'); + + /// Name. + final String name; + + const E._(this.name); +} +'''); + await assertHasAssistAt('E {', ''' +enum E { + /// AAA + a._('a'), + + /// BBB + /// BBB + b._('b'); + + /// Name. + final String name; + + const E._(this.name); +} +'''); + } + + Future test_documentationComments_single() async { + await resolveTestCode(''' +class E { + /// AAA + static const E a = E._('a'); + + /// Name. + final String name; + + const E._(this.name); +} +'''); + await assertHasAssistAt('E {', ''' +enum E { + /// AAA + a._('a'); + + /// Name. + final String name; + + const E._(this.name); +} +'''); + } + Future test_extends_object_privateClass() async { await resolveTestCode(''' class _E extends Object { diff --git a/pkg/analysis_server/test/src/services/correction/fix/convert_class_to_enum_test.dart b/pkg/analysis_server/test/src/services/correction/fix/convert_class_to_enum_test.dart index b9183483da1d..85bd708763cf 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/convert_class_to_enum_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/convert_class_to_enum_test.dart @@ -75,6 +75,35 @@ class ConvertClassToEnumTest extends FixProcessorLintTest { @override String get lintCode => LintNames.use_enums; + Future test_minimal_documentationComments() async { + await resolveTestCode(''' +class E { + /// C0. + static const E c0 = E._(0); + + /// C1. + static const E c1 = E._(1); + + final int value; + + const E._(this.value); +} +'''); + await assertHasFix(''' +enum E { + /// C0. + c0._(0), + + /// C1. + c1._(1); + + final int value; + + const E._(this.value); +} +'''); + } + Future test_minimal_intField_privateClass() async { await resolveTestCode(''' class _E {