Skip to content

Commit c93a4bc

Browse files
committed
Update SA1137 for file-scoped namespaces
1 parent fc7a433 commit c93a4bc

2 files changed

Lines changed: 161 additions & 4 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/ReadabilityRules/SA1137CSharp10UnitTests.cs

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,166 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp10.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp9.ReadabilityRules;
9+
using StyleCop.Analyzers.Test.Helpers;
10+
using Xunit;
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.ReadabilityRules.SA1137ElementsShouldHaveTheSameIndentation,
13+
StyleCop.Analyzers.ReadabilityRules.IndentationCodeFixProvider>;
714

815
public class SA1137CSharp10UnitTests : SA1137CSharp9UnitTests
916
{
17+
[Theory]
18+
[MemberData(nameof(CommonMemberData.BaseTypeDeclarationKeywords), MemberType = typeof(CommonMemberData))]
19+
public async Task TestFileScopedNamespaceDeclarationAsync(string baseTypeKind)
20+
{
21+
await new CSharpTest
22+
{
23+
TestSources =
24+
{
25+
$@"
26+
using System;
27+
28+
namespace Namespace0;
29+
30+
[My] [My] {baseTypeKind} TypeName {{ }}
31+
",
32+
$@"
33+
using System;
34+
35+
namespace Namespace1;
36+
37+
[My]
38+
[| |][My] {baseTypeKind} TypeName {{ }}
39+
",
40+
$@"
41+
using System;
42+
43+
namespace Namespace2;
44+
45+
[My]
46+
[| |][My]
47+
{baseTypeKind} TypeName {{ }}
48+
",
49+
$@"
50+
using System;
51+
52+
namespace Namespace3;
53+
54+
[| |][My]
55+
[| |][My]
56+
{baseTypeKind} TypeName {{ }}
57+
",
58+
$@"
59+
using System;
60+
61+
namespace Namespace4;
62+
63+
{baseTypeKind} TypeName1 {{ }}
64+
65+
[| |][My] {baseTypeKind} TypeName2 {{ }}
66+
",
67+
$@"
68+
using System;
69+
70+
namespace Namespace5;
71+
72+
{baseTypeKind} TypeName1 {{ }}
73+
74+
[My]
75+
[| |][My] {baseTypeKind} TypeName2 {{ }}
76+
",
77+
$@"
78+
using System;
79+
80+
namespace Namespace6;
81+
82+
{baseTypeKind} TypeName1 {{ }}
83+
84+
[| |][My]
85+
[My] {baseTypeKind} TypeName2 {{ }}
86+
",
87+
$@"
88+
using System;
89+
90+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
91+
class MyAttribute : Attribute {{ }}
92+
",
93+
},
94+
FixedSources =
95+
{
96+
$@"
97+
using System;
98+
99+
namespace Namespace0;
100+
101+
[My] [My] {baseTypeKind} TypeName {{ }}
102+
",
103+
$@"
104+
using System;
105+
106+
namespace Namespace1;
107+
108+
[My]
109+
[My] {baseTypeKind} TypeName {{ }}
110+
",
111+
$@"
112+
using System;
113+
114+
namespace Namespace2;
115+
116+
[My]
117+
[My]
118+
{baseTypeKind} TypeName {{ }}
119+
",
120+
$@"
121+
using System;
122+
123+
namespace Namespace3;
124+
125+
[My]
126+
[My]
127+
{baseTypeKind} TypeName {{ }}
128+
",
129+
$@"
130+
using System;
131+
132+
namespace Namespace4;
133+
134+
{baseTypeKind} TypeName1 {{ }}
135+
136+
[My] {baseTypeKind} TypeName2 {{ }}
137+
",
138+
$@"
139+
using System;
140+
141+
namespace Namespace5;
142+
143+
{baseTypeKind} TypeName1 {{ }}
144+
145+
[My]
146+
[My] {baseTypeKind} TypeName2 {{ }}
147+
",
148+
$@"
149+
using System;
150+
151+
namespace Namespace6;
152+
153+
{baseTypeKind} TypeName1 {{ }}
154+
155+
[My]
156+
[My] {baseTypeKind} TypeName2 {{ }}
157+
",
158+
$@"
159+
using System;
160+
161+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
162+
class MyAttribute : Attribute {{ }}
163+
",
164+
},
165+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
166+
}
10167
}
11168
}

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1137ElementsShouldHaveTheSameIndentation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal class SA1137ElementsShouldHaveTheSameIndentation : DiagnosticAnalyzer
3232
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
3333

3434
private static readonly Action<SyntaxNodeAnalysisContext> CompilationUnitAction = HandleCompilationUnit;
35-
private static readonly Action<SyntaxNodeAnalysisContext> NamespaceDeclarationAction = HandleNamespaceDeclaration;
35+
private static readonly Action<SyntaxNodeAnalysisContext> BaseNamespaceDeclarationAction = HandleBaseNamespaceDeclaration;
3636
private static readonly Action<SyntaxNodeAnalysisContext> TypeDeclarationAction = HandleTypeDeclaration;
3737
private static readonly Action<SyntaxNodeAnalysisContext> EnumDeclarationAction = HandleEnumDeclaration;
3838
private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration;
@@ -61,7 +61,7 @@ public override void Initialize(AnalysisContext context)
6161
context.EnableConcurrentExecution();
6262

6363
context.RegisterSyntaxNodeAction(CompilationUnitAction, SyntaxKind.CompilationUnit);
64-
context.RegisterSyntaxNodeAction(NamespaceDeclarationAction, SyntaxKind.NamespaceDeclaration);
64+
context.RegisterSyntaxNodeAction(BaseNamespaceDeclarationAction, SyntaxKinds.BaseNamespaceDeclaration);
6565
context.RegisterSyntaxNodeAction(TypeDeclarationAction, SyntaxKinds.TypeDeclaration);
6666
context.RegisterSyntaxNodeAction(EnumDeclarationAction, SyntaxKind.EnumDeclaration);
6767
context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration);
@@ -94,9 +94,9 @@ private static void HandleCompilationUnit(SyntaxNodeAnalysisContext context)
9494
CheckElements(context, elements.ToImmutable());
9595
}
9696

97-
private static void HandleNamespaceDeclaration(SyntaxNodeAnalysisContext context)
97+
private static void HandleBaseNamespaceDeclaration(SyntaxNodeAnalysisContext context)
9898
{
99-
var namespaceDeclaration = (NamespaceDeclarationSyntax)context.Node;
99+
var namespaceDeclaration = (BaseNamespaceDeclarationSyntaxWrapper)context.Node;
100100

101101
var elements = ImmutableList.CreateBuilder<SyntaxNode>();
102102

0 commit comments

Comments
 (0)