Skip to content

Commit 4ba311b

Browse files
committed
Fix failure to sort record kinds correctly
1 parent d45ce58 commit 4ba311b

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1201UnitTests.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.OrderingRules
75
{
6+
using System.Collections.Generic;
7+
using System.Linq;
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis.Testing;
11+
using StyleCop.Analyzers.Test.Helpers;
1112
using Xunit;
1213

1314
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
@@ -16,6 +17,20 @@ namespace StyleCop.Analyzers.Test.OrderingRules
1617

1718
public class SA1201UnitTests
1819
{
20+
public static IEnumerable<object[]> ValueTypesAndReferenceTypes
21+
{
22+
get
23+
{
24+
foreach (var valueTypeKeyword in CommonMemberData.ValueTypeDeclarationKeywords)
25+
{
26+
foreach (var referenceTypeKeyword in CommonMemberData.ReferenceTypeDeclarationKeywords)
27+
{
28+
yield return new object[] { valueTypeKeyword.Single(), referenceTypeKeyword.Single() };
29+
}
30+
}
31+
}
32+
}
33+
1934
[Fact]
2035
public async Task TestOuterOrderCorrectOrderAsync()
2136
{
@@ -52,6 +67,35 @@ public struct FooStruct { }
5267
await VerifyCSharpDiagnosticAsync("namespace OuterNamespace { " + testCode + " }", expected, CancellationToken.None).ConfigureAwait(false);
5368
}
5469

70+
[Theory]
71+
[MemberData(nameof(ValueTypesAndReferenceTypes))]
72+
public async Task TestClassBeforeStructAsync(
73+
string structKeyword,
74+
string classKeyword)
75+
{
76+
string testCode = $@"
77+
public {classKeyword} FooClass {{ }}
78+
public {structKeyword} {{|#0:FooStruct|}} {{ }}
79+
";
80+
string fixedCode = $@"public {structKeyword} FooStruct {{ }}
81+
82+
public {classKeyword} FooClass {{ }}
83+
";
84+
85+
var reportedClassKind = classKeyword switch
86+
{
87+
"record class" => "record",
88+
_ => classKeyword,
89+
};
90+
91+
var expected = new[]
92+
{
93+
Diagnostic().WithLocation(0).WithArguments(structKeyword, reportedClassKind),
94+
};
95+
96+
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
97+
}
98+
5599
[Fact]
56100
public async Task TestTypeMemberOrderCorrectOrderClassAsync()
57101
{

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/MemberOrderHelper.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Helpers
75
{
86
using System;
@@ -16,7 +14,7 @@ namespace StyleCop.Analyzers.Helpers
1614
/// <summary>
1715
/// Helper for dealing with member priority.
1816
/// </summary>
19-
internal struct MemberOrderHelper
17+
internal readonly struct MemberOrderHelper
2018
{
2119
private static readonly ImmutableArray<SyntaxKind> TypeMemberOrder = ImmutableArray.Create(
2220
SyntaxKind.ClassDeclaration,
@@ -44,8 +42,13 @@ internal MemberOrderHelper(MemberDeclarationSyntax member, ImmutableArray<Orderi
4442
{
4543
this.Member = member;
4644
var modifiers = member.GetModifiers();
47-
var type = member.Kind();
48-
type = type == SyntaxKind.EventFieldDeclaration ? SyntaxKind.EventDeclaration : type;
45+
var type = member.Kind() switch
46+
{
47+
SyntaxKind.EventFieldDeclaration => SyntaxKind.EventDeclaration,
48+
SyntaxKindEx.RecordDeclaration => SyntaxKind.ClassDeclaration,
49+
SyntaxKindEx.RecordStructDeclaration => SyntaxKind.StructDeclaration,
50+
var kind => kind,
51+
};
4952

5053
this.Priority = 0;
5154
foreach (OrderingTrait trait in elementOrder)

0 commit comments

Comments
 (0)