Skip to content

Commit 1bcb856

Browse files
committed
add more tests and fix extern alias
1 parent edfbcf1 commit 1bcb856

3 files changed

Lines changed: 184 additions & 53 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1516CodeFixProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ private static SyntaxNode GetRelevantNode(SyntaxNode innerNode)
152152
return currentNode;
153153
}
154154

155+
if (currentNode is ExternAliasDirectiveSyntax)
156+
{
157+
return currentNode;
158+
}
159+
155160
currentNode = currentNode.Parent;
156161
}
157162

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/LayoutRules/SA1516CSharp10UnitTests.cs

Lines changed: 157 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,96 +16,202 @@ namespace StyleCop.Analyzers.Test.CSharp10.LayoutRules
1616

1717
public class SA1516CSharp10UnitTests : SA1516CSharp9UnitTests
1818
{
19-
private const string CorrectCode = @"extern alias corlib;
19+
/// <summary>
20+
/// Verifies that SA1516 is reported for usings and extern alias outside a file scoped namespace.
21+
/// </summary>
22+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
23+
[Fact]
24+
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
25+
public async Task TestThatDiagnosticIIsReportedOnUsingsAndExternAliasOutsideFileScopedNamespaceAsync()
26+
{
27+
var testCode = @"extern alias corlib;
28+
[|using|] System;
29+
using System.Linq;
30+
using a = System.Collections;
31+
[|namespace|] Foo;
32+
";
33+
34+
var fixedCode = @"extern alias corlib;
2035
2136
using System;
2237
using System.Linq;
23-
using a = System.Collections.Generic;
38+
using a = System.Collections;
2439
2540
namespace Foo;
41+
";
2642

27-
public class Bar
28-
{
29-
public string Test1;
30-
public string Test2;
31-
public string Test3;
43+
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
44+
}
3245

33-
public string TestProperty1 { get; set; }
46+
/// <summary>
47+
/// Verifies that SA1516 is reported for usings inside a file scoped namespace.
48+
/// </summary>
49+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
50+
[Fact]
51+
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
52+
public async Task TestThatDiagnosticIIsReportedOnSpacingWithUsingsInsideFileScopedNamespaceAsync()
53+
{
54+
var testCode = @"namespace Foo;
55+
[|using|] System;
56+
using System.Linq;
57+
using a = System.Collections;
58+
";
3459

35-
public string TestProperty2 { get; set; }
36-
/// <summary>
37-
/// A summary.
38-
/// </summary>
39-
public string TestProperty3 { get; set; }
60+
var fixedCode = @"namespace Foo;
4061
41-
public string TestProperty4
42-
{
43-
get
44-
{
45-
return Test1;
62+
using System;
63+
using System.Linq;
64+
using a = System.Collections;
65+
";
66+
67+
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
4668
}
4769

48-
set
70+
/// <summary>
71+
/// Verifies that SA1516 is reported for member declarations inside a file scoped namespace.
72+
/// </summary>
73+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
74+
[Fact]
75+
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
76+
public async Task TestThatDiagnosticIIsReportedOnMemberDeclarationsInsideFileScopedNamespaceAsync()
4977
{
50-
Test1 = value;
51-
}
52-
}
78+
var testCode = @"namespace Foo;
79+
[|public|] class Bar
80+
{
81+
}
82+
[|public|] enum Foobar
83+
{
84+
}
85+
";
5386

54-
public string FooValue, BarValue;
87+
var fixedCode = @"namespace Foo;
5588
56-
[Obsolete]
57-
public enum TestEnum
58-
{
59-
Value1,
60-
Value2
61-
}
89+
public class Bar
90+
{
6291
}
6392
6493
public enum Foobar
6594
{
95+
}
96+
";
6697

98+
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
99+
}
100+
101+
/// <summary>
102+
/// Verifies that SA1516 is reported for usings and member declarations inside a file scoped namespace.
103+
/// </summary>
104+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
105+
[Fact]
106+
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
107+
public async Task TestThatDiagnosticIIsReportedOnUsingsAndMemberDeclarationsInsideFileScopedNamespaceAsync()
108+
{
109+
var testCode = @"namespace Foo;
110+
[|using|] System;
111+
using System.Linq;
112+
using a = System.Collections;
113+
[|public|] class Bar
114+
{
115+
}
116+
[|public|] enum Foobar
117+
{
67118
}
68119
";
69120

121+
var fixedCode = @"namespace Foo;
122+
123+
using System;
124+
using System.Linq;
125+
using a = System.Collections;
126+
127+
public class Bar
128+
{
129+
}
130+
131+
public enum Foobar
132+
{
133+
}
134+
";
135+
136+
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
137+
}
138+
70139
/// <summary>
71-
/// Verifies that SA1516 is not reported for code with correct blank lines.
140+
/// Verifies that SA1516 is reported extern alias inside a file scoped namespace.
72141
/// </summary>
73142
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
74143
[Fact]
75144
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
76-
public async Task TestFileScopedNamespaceCorrectSpacingAsync()
145+
public async Task TestThatDiagnosticIIsReportedOnExternAliasInsideFileScopedNamespaceAsync()
77146
{
78-
await VerifyCSharpDiagnosticAsync(CorrectCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
147+
var testCode = @"namespace Foo;
148+
[|extern|] alias corlib;
149+
";
150+
151+
var fixedCode = @"namespace Foo;
152+
153+
extern alias corlib;
154+
";
155+
156+
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
79157
}
80158

81159
/// <summary>
82-
/// Verifies that SA1516 is reported for code with missing correct blank lines.
160+
/// Verifies that SA1516 is reported extern alias and usings inside a file scoped namespace.
83161
/// </summary>
84162
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
85163
[Fact]
86164
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
87-
public async Task TestFileScopedNamespaceWrongSpacingAsync()
165+
public async Task TestThatDiagnosticIIsReportedOnExternAliasAndUsingsInsideFileScopedNamespaceAsync()
88166
{
89-
var testCode = @"extern alias corlib;
90-
{|#0:using|} System;
167+
var testCode = @"namespace Foo;
168+
[|extern|] alias corlib;
169+
[|using|] System;
91170
using System.Linq;
92-
using a = System.Collections.Generic;
93-
{|#1:namespace|} Foo;
94-
{|#2:public|} class Bar
171+
using a = System.Collections;
172+
";
173+
174+
var fixedCode = @"namespace Foo;
175+
176+
extern alias corlib;
177+
178+
using System;
179+
using System.Linq;
180+
using a = System.Collections;
181+
";
182+
183+
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
184+
}
185+
186+
/// <summary>
187+
/// Verifies that SA1516 is reported extern alias, usings and member declarations
188+
/// inside a file scoped namespace.
189+
/// </summary>
190+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
191+
[Fact]
192+
[WorkItem(3512, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512")]
193+
public async Task TestThatDiagnosticIIsReportedOnExternAliasUsingsAndMemberDeclarationsInsideFileScopedNamespaceAsync()
194+
{
195+
var testCode = @"namespace Foo;
196+
[|extern|] alias corlib;
197+
[|using|] System;
198+
using System.Linq;
199+
using a = System.Collections;
200+
[|public|] class Bar
95201
{
96202
}
97-
{|#3:public|} enum Foobar
203+
[|public|] enum Foobar
98204
{
99205
}
100206
";
101207

102-
var fixedCode = @"extern alias corlib;
208+
var fixedCode = @"namespace Foo;
209+
210+
extern alias corlib;
103211
104212
using System;
105213
using System.Linq;
106-
using a = System.Collections.Generic;
107-
108-
namespace Foo;
214+
using a = System.Collections;
109215
110216
public class Bar
111217
{
@@ -116,6 +222,11 @@ public enum Foobar
116222
}
117223
";
118224

225+
await VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
226+
}
227+
228+
private static Task VerifyCSharpFixAsync(string testCode, string fixedCode)
229+
{
119230
var test = new CSharpTest(LanguageVersion.CSharp10)
120231
{
121232
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
@@ -126,15 +237,8 @@ public enum Foobar
126237
},
127238
FixedCode = fixedCode,
128239
};
129-
var expectedDiagnostic = new[]
130-
{
131-
Diagnostic().WithLocation(0),
132-
Diagnostic().WithLocation(1),
133-
Diagnostic().WithLocation(2),
134-
Diagnostic().WithLocation(3),
135-
};
136-
test.TestState.ExpectedDiagnostics.AddRange(expectedDiagnostic);
137-
await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
240+
241+
return test.RunAsync(CancellationToken.None);
138242
}
139243
}
140244
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1516ElementsMustBeSeparatedByBlankLine.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,35 @@ private static void HandleFileScopedNamespaceDeclaration(SyntaxNodeAnalysisConte
216216
{
217217
var namespaceDeclaration = (BaseNamespaceDeclarationSyntaxWrapper)context.Node;
218218

219+
var usings = namespaceDeclaration.Usings;
219220
var members = namespaceDeclaration.Members;
220221

222+
HandleUsings(context, usings, settings);
221223
HandleMemberList(context, members);
222224

225+
if (namespaceDeclaration.Externs.Count > 0)
226+
{
227+
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Name, namespaceDeclaration.Externs[0]);
228+
}
229+
230+
if (namespaceDeclaration.Usings.Count > 0)
231+
{
232+
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Name, namespaceDeclaration.Usings[0]);
233+
234+
if (namespaceDeclaration.Externs.Count > 0)
235+
{
236+
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Externs[namespaceDeclaration.Externs.Count - 1], namespaceDeclaration.Usings[0]);
237+
}
238+
}
239+
223240
if (members.Count > 0)
224241
{
225242
ReportIfThereIsNoBlankLine(context, namespaceDeclaration.Name, members[0]);
243+
244+
if (namespaceDeclaration.Usings.Count > 0)
245+
{
246+
ReportIfThereIsNoBlankLine(context, usings[usings.Count - 1], members[0]);
247+
}
226248
}
227249
}
228250

0 commit comments

Comments
 (0)