Skip to content

Commit 6f4f90c

Browse files
authored
Merge pull request #3261 from sharwell/top-level-using
Fix SA1200 handling of top-level statements
2 parents 7e961ae + 0a58fe4 commit 6f4f90c

File tree

6 files changed

+126
-3
lines changed

6 files changed

+126
-3
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/OrderingRules/SA1200CSharp9OutsideNamespaceUnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.OrderingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.Testing;
611
using StyleCop.Analyzers.Test.CSharp8.OrderingRules;
12+
using Xunit;
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.OrderingRules.SA1200UsingDirectivesMustBePlacedCorrectly,
15+
StyleCop.Analyzers.OrderingRules.UsingCodeFixProvider>;
716

817
public class SA1200CSharp9OutsideNamespaceUnitTests : SA1200CSharp8OutsideNamespaceUnitTests
918
{
19+
/// <summary>
20+
/// Verifies that having using statements in the compilation unit will not produce diagnostics for top-level
21+
/// programs.
22+
/// </summary>
23+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
24+
[Fact]
25+
[WorkItem(3243, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3243")]
26+
public async Task TestValidUsingStatementsInTopLevelProgramAsync()
27+
{
28+
var testCode = @"using System;
29+
using System.Threading;
30+
31+
return 0;
32+
";
33+
34+
await new CSharpTest(LanguageVersion.CSharp9)
35+
{
36+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
37+
TestCode = testCode,
38+
Settings = TestSettings,
39+
SolutionTransforms =
40+
{
41+
(solution, projectId) =>
42+
{
43+
var project = solution.GetProject(projectId);
44+
var options = project.CompilationOptions;
45+
return solution.WithProjectCompilationOptions(projectId, options.WithOutputKind(OutputKind.ConsoleApplication));
46+
},
47+
},
48+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
49+
}
1050
}
1151
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/OrderingRules/SA1200CSharp9PreserveUnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.OrderingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.Testing;
611
using StyleCop.Analyzers.Test.CSharp8.OrderingRules;
12+
using Xunit;
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.OrderingRules.SA1200UsingDirectivesMustBePlacedCorrectly,
15+
StyleCop.Analyzers.OrderingRules.UsingCodeFixProvider>;
716

817
public class SA1200CSharp9PreserveUnitTests : SA1200CSharp8PreserveUnitTests
918
{
19+
/// <summary>
20+
/// Verifies that having using statements in the compilation unit will not produce diagnostics for top-level
21+
/// programs.
22+
/// </summary>
23+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
24+
[Fact]
25+
[WorkItem(3243, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3243")]
26+
public async Task TestValidUsingStatementsInTopLevelProgramAsync()
27+
{
28+
var testCode = @"using System;
29+
using System.Threading;
30+
31+
return 0;
32+
";
33+
34+
await new CSharpTest(LanguageVersion.CSharp9)
35+
{
36+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
37+
TestCode = testCode,
38+
Settings = TestSettings,
39+
SolutionTransforms =
40+
{
41+
(solution, projectId) =>
42+
{
43+
var project = solution.GetProject(projectId);
44+
var options = project.CompilationOptions;
45+
return solution.WithProjectCompilationOptions(projectId, options.WithOutputKind(OutputKind.ConsoleApplication));
46+
},
47+
},
48+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
49+
}
1050
}
1151
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/OrderingRules/SA1200CSharp9UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp9.OrderingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.Testing;
611
using StyleCop.Analyzers.Test.CSharp8.OrderingRules;
12+
using Xunit;
13+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
14+
StyleCop.Analyzers.OrderingRules.SA1200UsingDirectivesMustBePlacedCorrectly,
15+
StyleCop.Analyzers.OrderingRules.UsingCodeFixProvider>;
716

817
public class SA1200CSharp9UnitTests : SA1200CSharp8UnitTests
918
{
19+
/// <summary>
20+
/// Verifies that having using statements in the compilation unit will not produce diagnostics for top-level
21+
/// programs.
22+
/// </summary>
23+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
24+
[Fact]
25+
[WorkItem(3243, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3243")]
26+
public async Task TestValidUsingStatementsInTopLevelProgramAsync()
27+
{
28+
var testCode = @"using System;
29+
using System.Threading;
30+
31+
return 0;
32+
";
33+
34+
await new CSharpTest(LanguageVersion.CSharp9)
35+
{
36+
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
37+
TestCode = testCode,
38+
SolutionTransforms =
39+
{
40+
(solution, projectId) =>
41+
{
42+
var project = solution.GetProject(projectId);
43+
var options = project.CompilationOptions;
44+
return solution.WithProjectCompilationOptions(projectId, options.WithOutputKind(OutputKind.ConsoleApplication));
45+
},
46+
},
47+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
48+
}
1049
}
1150
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200OutsideNamespaceUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
1818
/// </summary>
1919
public class SA1200OutsideNamespaceUnitTests
2020
{
21-
private const string TestSettings = @"
21+
protected const string TestSettings = @"
2222
{
2323
""settings"": {
2424
""orderingRules"": {

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1200PreserveUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace StyleCop.Analyzers.Test.OrderingRules
1717
/// </summary>
1818
public class SA1200PreserveUnitTests
1919
{
20-
private const string TestSettings = @"
20+
protected const string TestSettings = @"
2121
{
2222
""settings"": {
2323
""orderingRules"": {

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1200UsingDirectivesMustBePlacedCorrectly.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ private static void HandleCompilationUnit(SyntaxNodeAnalysisContext context, Sty
222222
return;
223223

224224
case SyntaxKind.AttributeList:
225-
// suppress SA1200 if file contains an attribute in the global namespace
225+
// Suppress SA1200 if file contains an attribute in the global namespace
226+
return;
227+
228+
case SyntaxKind.GlobalStatement:
229+
// Suppress SA1200 if file contains top-level statements
226230
return;
227231

228232
case SyntaxKind.UsingDirective:

0 commit comments

Comments
 (0)