Skip to content

Commit 59e5c5c

Browse files
committed
Update SA1202 for default interface members
1 parent 2959cac commit 59e5c5c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/OrderingRules/SA1202CSharp8UnitTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ namespace StyleCop.Analyzers.Test.CSharp8.OrderingRules
55
{
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Testing;
89
using StyleCop.Analyzers.Test.CSharp7.OrderingRules;
10+
using StyleCop.Analyzers.Test.Helpers;
911
using Xunit;
1012
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1113
StyleCop.Analyzers.OrderingRules.SA1202ElementsMustBeOrderedByAccess,
@@ -57,5 +59,70 @@ public async Task TestPropertiesOfInterfaceAsync()
5759
NumberOfFixAllIterations = 2,
5860
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
5961
}
62+
63+
[Fact]
64+
[WorkItem(3002, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3002")]
65+
public async Task TestDefaultInterfaceMembersRequireAccessOrderingAsync()
66+
{
67+
var testCode = @"public interface ITest
68+
{
69+
private void Helper() { }
70+
public void {|#0:DoWork|}() { }
71+
}
72+
";
73+
74+
var fixedCode = @"public interface ITest
75+
{
76+
public void DoWork() { }
77+
private void Helper() { }
78+
}
79+
";
80+
81+
await VerifyCSharpFixAsync(testCode, Diagnostic().WithLocation(0).WithArguments("public", "private"), fixedCode, CancellationToken.None).ConfigureAwait(false);
82+
}
83+
84+
[Fact]
85+
[WorkItem(3002, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3002")]
86+
public async Task TestDefaultInterfaceMembersCorrectOrderingAsync()
87+
{
88+
var testCode = @"public interface ITest
89+
{
90+
public void DoWork() { }
91+
private void Helper() { }
92+
}
93+
";
94+
95+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
96+
}
97+
98+
[Fact]
99+
[WorkItem(3002, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3002")]
100+
public async Task TestInterfaceMembersMixingImplicitAndExplicitAccessibilityAsync()
101+
{
102+
var testCode = @"public interface ITest
103+
{
104+
private void Helper() { }
105+
void {|#0:ImplicitPublic|}() { }
106+
public void ExplicitPublic() { }
107+
}
108+
";
109+
110+
var fixedCode = @"public interface ITest
111+
{
112+
void ImplicitPublic() { }
113+
public void ExplicitPublic() { }
114+
private void Helper() { }
115+
}
116+
";
117+
118+
await new CSharpTest
119+
{
120+
TestCode = testCode,
121+
ExpectedDiagnostics = { Diagnostic().WithLocation(0).WithArguments("public", "private") },
122+
FixedCode = fixedCode,
123+
NumberOfIncrementalIterations = 2,
124+
NumberOfFixAllIterations = 2,
125+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
126+
}
60127
}
61128
}

documentation/SA1202.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ rule.
4040
Complying with a standard ordering scheme based on access level can increase the readability and maintainability of the
4141
file and make it easier to identify the public interface that is being exposed from a class.
4242

43+
### Interfaces and default interface members
44+
45+
Default interface members (including members with implementations and static interface members) follow the same access
46+
ordering as class members. Within an interface, `public` members should appear before `internal`, `protected internal`,
47+
`protected`, `private protected`, and `private` members.
48+
4349
## How to fix violations
4450

4551
To fix an instance of this violation, order the elements in the file in the order described above.

0 commit comments

Comments
 (0)