Skip to content

Commit 92e453f

Browse files
Added new test case for SA1205 to verify that nested partial types without access modifiers are changed to 'private' by the code fix. Updated code fix accordingly.
1 parent 88deb37 commit 92e453f

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1205CodeFixProvider.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.OrderingRules
55
{
6+
using System;
67
using System.Collections.Immutable;
78
using System.Composition;
89
using System.Threading;
@@ -59,7 +60,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5960
}
6061

6162
var symbol = semanticModel.GetDeclaredSymbol(typeDeclarationNode);
62-
var accessModifierKind = (symbol.DeclaredAccessibility == Accessibility.Public) ? SyntaxKind.PublicKeyword : SyntaxKind.InternalKeyword;
63+
var accessModifierKind = GetMissingAccessModifier(symbol.DeclaredAccessibility);
6364

6465
var keywordToken = typeDeclarationNode.Keyword;
6566

@@ -70,6 +71,21 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
7071
return document.WithSyntaxRoot(newSyntaxRoot);
7172
}
7273

74+
private static SyntaxKind GetMissingAccessModifier(Accessibility accessibility)
75+
{
76+
switch (accessibility)
77+
{
78+
case Accessibility.Public:
79+
return SyntaxKind.PublicKeyword;
80+
case Accessibility.Internal:
81+
return SyntaxKind.InternalKeyword;
82+
case Accessibility.Private:
83+
return SyntaxKind.PrivateKeyword;
84+
default:
85+
throw new InvalidOperationException("Unexpected accessibility " + accessibility);
86+
}
87+
}
88+
7389
// This code was copied from the Roslyn code base (and slightly modified). It can be removed if
7490
// TypeDeclarationSyntaxExtensions.WithModifiers is made public (Roslyn issue #2186)
7591
private static TypeDeclarationSyntax ReplaceModifiers(TypeDeclarationSyntax node, SyntaxTokenList modifiers)

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1205UnitTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,38 @@ internal static partial class TestPartial
200200
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
201201
}
202202

203+
/// <summary>
204+
/// Verifies that a nested type without access modifiers will produce a diagnostic and can be fixed correctly.
205+
/// </summary>
206+
/// <param name="declaration">The declaration to verify.</param>
207+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
208+
[Theory]
209+
[MemberData(nameof(InvalidDeclarations))]
210+
public async Task TestNestedTypeWithoutAccessModifierAsync(string declaration)
211+
{
212+
var testCode = $@"
213+
public class Foo
214+
{{
215+
{declaration} Bar
216+
{{
217+
}}
218+
}}
219+
";
220+
221+
var fixedTestCode = $@"
222+
public class Foo
223+
{{
224+
private {declaration} Bar
225+
{{
226+
}}
227+
}}
228+
";
229+
230+
await this.VerifyCSharpDiagnosticAsync(testCode, this.CSharpDiagnostic().WithLocation(4, 6 + declaration.Length), CancellationToken.None).ConfigureAwait(false);
231+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
232+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
233+
}
234+
203235
/// <inheritdoc/>
204236
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
205237
{

0 commit comments

Comments
 (0)