Skip to content

Commit

Permalink
Fix missing comments (RCS1154) (dotnet#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored and JochemHarmes committed Oct 30, 2023
1 parent 412d527 commit fb94902
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 68 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix [RCS1056](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1056.md) ([#1096](https://github.com/JosefPihrt/Roslynator/pull/1096)).
- Fix [RCS1216](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1216.md) ([#1094](https://github.com/JosefPihrt/Roslynator/pull/1094)).
- Fix [RCS1146](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1146.md) ([#1098](https://github.com/JosefPihrt/Roslynator/pull/1098)).
- Fix [RCS1154](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1154.md) ([#1105](https://github.com/JosefPihrt/Roslynator/pull/1105)).

## [4.3.0] - 2023-04-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,68 +119,65 @@ private static async Task<Document> SortEnumMembersAsync(
CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

SpecialType enumSpecialType = semanticModel.GetDeclaredSymbol(enumDeclaration).EnumUnderlyingType.SpecialType;

SeparatedSyntaxList<EnumMemberDeclarationSyntax> members = enumDeclaration.Members;

SeparatedSyntaxList<EnumMemberDeclarationSyntax> newMembers = members
List<EnumMemberDeclarationSyntax> sortedList = members
.OrderBy(f => GetConstantValue(f, semanticModel, cancellationToken), EnumValueComparer.GetInstance(enumSpecialType))
.ToSeparatedSyntaxList();

if (AreSeparatedWithEmptyLine(members))
{
for (int i = 0; i < newMembers.Count; i++)
{
newMembers = newMembers.ReplaceAt(i, newMembers[i].TrimLeadingTrivia());
}

for (int i = 0; i < newMembers.Count - 1; i++)
{
SyntaxToken separator = newMembers.GetSeparator(i);
.ToList();

newMembers = newMembers.ReplaceSeparator(
separator,
separator.TrimTrailingTrivia().AppendToTrailingTrivia(new SyntaxTrivia[] { NewLine(), NewLine() }));
}
}
bool hasTrailingSeparator = members.HasTrailingSeparator();
int lastIndex = sortedList.Count - 1;

if (newMembers.SeparatorCount == members.SeparatorCount - 1)
if (!hasTrailingSeparator)
{
SyntaxNodeOrTokenList newMembersWithSeparators = newMembers.GetWithSeparators();
int index = members.IndexOf(sortedList.Last());

newMembersWithSeparators = newMembersWithSeparators.Add(CommaToken());
SyntaxTriviaList trailingTrivia = (index == members.Count - 1)
? members[index].GetTrailingTrivia()
: members.GetSeparator(index).TrailingTrivia;

newMembers = newMembersWithSeparators.ToSeparatedSyntaxList<EnumMemberDeclarationSyntax>();
sortedList[sortedList.Count - 1] = sortedList[sortedList.Count - 1].WithTrailingTrivia(trailingTrivia);
lastIndex--;
}

MemberDeclarationSyntax newNode = enumDeclaration
.WithMembers(newMembers)
.WithFormatterAnnotation();
List<SyntaxNodeOrToken> sortedMembers = sortedList.ConvertAll(member => (SyntaxNodeOrToken)member);

return await document.ReplaceNodeAsync(enumDeclaration, newNode, cancellationToken).ConfigureAwait(false);
}
for (int i = lastIndex; i >= 0; i--)
{
int oldIndex = members.IndexOf((EnumMemberDeclarationSyntax)sortedMembers[i]);

private static bool AreSeparatedWithEmptyLine(SeparatedSyntaxList<EnumMemberDeclarationSyntax> members)
{
int count = members.Count;
if (oldIndex == 0
&& members[i].GetLeadingTrivia().FirstOrDefault().IsEndOfLineTrivia())
{
sortedMembers[i] = sortedMembers[i].WithLeadingTrivia(sortedMembers[i].GetLeadingTrivia().Insert(0, NewLine()));
}

if (members.SeparatorCount < count - 1)
return false;
SyntaxTriviaList trailingTrivia;
if (!hasTrailingSeparator
&& oldIndex == members.Count - 1)
{
trailingTrivia = members.Last().GetTrailingTrivia();
sortedMembers[i] = sortedMembers[i].WithoutTrailingTrivia();
}
else
{
trailingTrivia = members.GetSeparator(oldIndex).TrailingTrivia;
}

for (int i = 1; i < count; i++)
{
if (!members[i].GetLeadingTrivia().Any(SyntaxKind.EndOfLineTrivia))
return false;
sortedMembers.Insert(i + 1, Token(SyntaxKind.CommaToken).WithTrailingTrivia(trailingTrivia));
}

for (int i = 0; i < count - 1; i++)
{
if (!members.GetSeparator(i).TrailingTrivia.Any(SyntaxKind.EndOfLineTrivia))
return false;
}
SyntaxTriviaList leadingTrivia = sortedMembers[0].GetLeadingTrivia();

if (leadingTrivia.FirstOrDefault().IsEndOfLineTrivia())
sortedMembers[0] = sortedMembers[0].WithLeadingTrivia(leadingTrivia.RemoveAt(0));

return true;
MemberDeclarationSyntax newEnumDeclaration = enumDeclaration
.WithMembers(sortedMembers.ToSeparatedSyntaxList<EnumMemberDeclarationSyntax>())
.WithFormatterAnnotation();

return await document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken).ConfigureAwait(false);
}

private static object GetConstantValue(
Expand Down
48 changes: 24 additions & 24 deletions src/Tests/Analyzers.Tests/RCS1154SortEnumMembersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,25 @@ await VerifyDiagnosticAndFixAsync(@"
enum [|Foo|]
{
/// <summary>B</summary>
B = 1,
B = 1, // B
/// <summary>A</summary>
A = 0,
A = 0, // A
/// <summary>D</summary>
D = 3,
D = 3, // D
/// <summary>C</summary>
C = 2,
C = 2, // C
}
", @"
enum Foo
{
/// <summary>A</summary>
A = 0,
A = 0, // A
/// <summary>B</summary>
B = 1,
B = 1, // B
/// <summary>C</summary>
C = 2,
C = 2, // C
/// <summary>D</summary>
D = 3,
D = 3, // D
}
");
}
Expand All @@ -121,31 +121,31 @@ await VerifyDiagnosticAndFixAsync(@"
enum [|Foo|]
{
/// <summary>B</summary>
B = 1,
B = 1, // B
/// <summary>A</summary>
A = 0,
A = 0, // A
/// <summary>D</summary>
D = 3,
D = 3, // D
/// <summary>C</summary>
C = 2
C = 2 // C
}
", @"
enum Foo
{
/// <summary>A</summary>
A = 0,
A = 0, // A
/// <summary>B</summary>
B = 1,
B = 1, // B
/// <summary>C</summary>
C = 2,
C = 2, // C
/// <summary>D</summary>
D = 3
D = 3 // D
}
");
}
Expand All @@ -157,31 +157,31 @@ await VerifyDiagnosticAndFixAsync(@"
enum [|Foo|]
{
/// <summary>B</summary>
B = 1,
B = 1, // B
/// <summary>A</summary>
A = 0,
A = 0, // A
/// <summary>D</summary>
D = 3,
D = 3, // D
/// <summary>C</summary>
C = 2,
C = 2, // C
}
", @"
enum Foo
{
/// <summary>A</summary>
A = 0,
A = 0, // A
/// <summary>B</summary>
B = 1,
B = 1, // B
/// <summary>C</summary>
C = 2,
C = 2, // C
/// <summary>D</summary>
D = 3,
D = 3, // D
}
");
}
Expand Down

0 comments on commit fb94902

Please sign in to comment.