Skip to content

Commit

Permalink
[analysis_server] Preserve field doc comments in CONVERT_CLASS_TO_ENU…
Browse files Browse the repository at this point in the history
…M assist

Fixes #53202

Bug: #53202
Change-Id: Icbc1627da55eb7c680cba802fdb931a843317b3d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336860
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Auto-Submit: Parker Lougheed <parlough@gmail.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
parlough authored and Commit Queue committed Nov 20, 2023
1 parent 831ad55 commit a359210
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,95 @@ class ConvertClassToEnumTest extends AssistProcessorTest {
@override
AssistKind get kind => DartAssistKind.CONVERT_CLASS_TO_ENUM;

Future<void> 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<void> 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<void> 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<void> test_extends_object_privateClass() async {
await resolveTestCode('''
class _E extends Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,35 @@ class ConvertClassToEnumTest extends FixProcessorLintTest {
@override
String get lintCode => LintNames.use_enums;

Future<void> 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<void> test_minimal_intField_privateClass() async {
await resolveTestCode('''
class _E {
Expand Down

0 comments on commit a359210

Please sign in to comment.