[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 <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Parker Lougheed
2026-06-08 09:11:55 -07:00
committed by Brian Wilkerson
parent a4ced20e50
commit a1160684d9
2 changed files with 81 additions and 2 deletions
@@ -28,6 +28,7 @@ typedef _Constructors = Map<ConstructorElement, _Constructor>;
/// 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',
);
@@ -900,6 +900,81 @@ enum E {
''');
}
Future<void> test_withClassModifier_base() async {
await resolveTestCode('''
base class ^_E {
static const _E c = _E();
const _E();
}
''');
await assertHasAssist('''
enum _E {
c
}
''');
}
Future<void> test_withClassModifier_baseMixin() async {
await resolveTestCode('''
base mixin class ^_E {
static const _E c = _E();
const _E();
}
''');
await assertHasAssist('''
enum _E {
c
}
''');
}
Future<void> test_withClassModifier_final() async {
await resolveTestCode('''
final class ^_E {
static const _E c = _E();
const _E();
}
''');
await assertHasAssist('''
enum _E {
c
}
''');
}
Future<void> test_withClassModifier_interface() async {
await resolveTestCode('''
interface class ^_E {
static const _E c = _E();
const _E();
}
''');
await assertHasAssist('''
enum _E {
c
}
''');
}
Future<void> test_withClassModifier_mixin() async {
await resolveTestCode('''
mixin class ^_E {
static const _E c = _E();
const _E();
}
''');
await assertHasAssist('''
enum _E {
c
}
''');
}
Future<void> test_withReferencedFactoryConstructor() async {
await resolveTestCode('''
class _^E {