From a1160684d9106c894bf730e0e436f4f855ce75d0 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Mon, 8 Jun 2026 09:11:55 -0700 Subject: [PATCH] [analysis_server] Remove class modifiers in convert_class_to_enum fix The class modifiers aren't valid on the resulting enum declaration. Fixes https://github.com/dart-lang/sdk/issues/61189 Change-Id: Idd8ed4fcfae071d21f25811b2158cfad3178fef5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/509880 Reviewed-by: Samuel Rawlins Reviewed-by: Brian Wilkerson --- .../dart/convert_class_to_enum.dart | 8 +- .../assist/convert_class_to_enum_test.dart | 75 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) 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 9500f4e9bfa..803a04ca73c 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 @@ -28,6 +28,7 @@ typedef _Constructors = Map; /// the following changes: /// /// * changes the `class` keyword to `enum`, +/// * removes any class modifiers, /// * removes the `const` keyword from the primary constructor, if there is one, /// * converts static fields into enum constant values, /// * removes an `int index` field if there is one, @@ -192,9 +193,12 @@ class _EnumDescription { /// Use the [builder] and correction [utils] to apply the change necessary to /// convert the class to an enum. void applyChanges(DartFileEditBuilder builder, CorrectionUtils utils) { - // Replace the keyword. + // Replace the class keyword and remove leading class modifiers. builder.addSimpleReplacement( - range.token(classDeclaration.classKeyword), + range.startEnd( + classDeclaration.firstTokenAfterCommentAndMetadata, + classDeclaration.classKeyword, + ), 'enum', ); 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 d0d588b01f9..0c54f014c4a 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 @@ -900,6 +900,81 @@ enum E { '''); } + Future test_withClassModifier_base() async { + await resolveTestCode(''' +base class ^_E { + static const _E c = _E(); + + const _E(); +} +'''); + await assertHasAssist(''' +enum _E { + c +} +'''); + } + + Future test_withClassModifier_baseMixin() async { + await resolveTestCode(''' +base mixin class ^_E { + static const _E c = _E(); + + const _E(); +} +'''); + await assertHasAssist(''' +enum _E { + c +} +'''); + } + + Future test_withClassModifier_final() async { + await resolveTestCode(''' +final class ^_E { + static const _E c = _E(); + + const _E(); +} +'''); + await assertHasAssist(''' +enum _E { + c +} +'''); + } + + Future test_withClassModifier_interface() async { + await resolveTestCode(''' +interface class ^_E { + static const _E c = _E(); + + const _E(); +} +'''); + await assertHasAssist(''' +enum _E { + c +} +'''); + } + + Future test_withClassModifier_mixin() async { + await resolveTestCode(''' +mixin class ^_E { + static const _E c = _E(); + + const _E(); +} +'''); + await assertHasAssist(''' +enum _E { + c +} +'''); + } + Future test_withReferencedFactoryConstructor() async { await resolveTestCode(''' class _^E {