Skip to content

Commit 7172c37

Browse files
author
ylabade
committed
Add file-scoped namespace support to SA1208
1 parent a821966 commit 7172c37

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1208CSharp10UnitTests.cs

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

44
namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp9.OrderingRules;
9+
using Xunit;
10+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
11+
StyleCop.Analyzers.OrderingRules.SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives,
12+
StyleCop.Analyzers.OrderingRules.UsingCodeFixProvider>;
713

814
public class SA1208CSharp10UnitTests : SA1208CSharp9UnitTests
915
{
16+
[Fact]
17+
public async Task TestWhenSystemUsingDirectivesAreNotOnTopInFileScopedNamespaceAsync()
18+
{
19+
await new CSharpTest
20+
{
21+
TestSources =
22+
{
23+
"namespace Xyz {}",
24+
"namespace AnotherNamespace {}",
25+
@"
26+
namespace Test;
27+
28+
using Xyz;
29+
using System;
30+
using System.IO;
31+
using AnotherNamespace;
32+
using System.Threading.Tasks;
33+
34+
class A
35+
{
36+
}",
37+
},
38+
ExpectedDiagnostics =
39+
{
40+
Diagnostic().WithLocation("/0/Test2.cs", 5, 1).WithArguments("System", "Xyz"),
41+
Diagnostic().WithLocation("/0/Test2.cs", 6, 1).WithArguments("System.IO", "Xyz"),
42+
Diagnostic().WithLocation("/0/Test2.cs", 8, 1).WithArguments("System.Threading.Tasks", "Xyz"),
43+
},
44+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
45+
}
1046
}
1147
}

StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.OrderingRules
1010
using Microsoft.CodeAnalysis.CSharp.Syntax;
1111
using Microsoft.CodeAnalysis.Diagnostics;
1212
using StyleCop.Analyzers.Helpers;
13+
using StyleCop.Analyzers.Lightup;
1314
using StyleCop.Analyzers.Settings.ObjectModel;
1415

1516
/// <summary>
@@ -40,6 +41,7 @@ internal class SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives
4041

4142
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> CompilationUnitAction = HandleCompilationUnit;
4243
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> NamespaceDeclarationAction = HandleNamespaceDeclaration;
44+
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> FileScopedNamespaceDeclarationAction = HandleFileScopedNamespaceDeclaration;
4345

4446
/// <inheritdoc/>
4547
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
@@ -53,6 +55,7 @@ public override void Initialize(AnalysisContext context)
5355

5456
context.RegisterSyntaxNodeAction(CompilationUnitAction, SyntaxKind.CompilationUnit);
5557
context.RegisterSyntaxNodeAction(NamespaceDeclarationAction, SyntaxKind.NamespaceDeclaration);
58+
context.RegisterSyntaxNodeAction(FileScopedNamespaceDeclarationAction, SyntaxKindEx.FileScopedNamespaceDeclaration);
5659
}
5760

5861
private static void HandleCompilationUnit(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
@@ -77,9 +80,19 @@ private static void HandleNamespaceDeclaration(SyntaxNodeAnalysisContext context
7780
}
7881

7982
var namespaceDeclaration = (NamespaceDeclarationSyntax)context.Node;
80-
8183
var usings = namespaceDeclaration.Usings;
84+
ProcessUsingsAndReportDiagnostic(usings, context);
85+
}
86+
87+
private static void HandleFileScopedNamespaceDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
88+
{
89+
if (!settings.OrderingRules.SystemUsingDirectivesFirst)
90+
{
91+
return;
92+
}
8293

94+
var namespaceDeclaration = (FileScopedNamespaceDeclarationSyntaxWrapper)context.Node;
95+
var usings = namespaceDeclaration.Usings;
8396
ProcessUsingsAndReportDiagnostic(usings, context);
8497
}
8598

0 commit comments

Comments
 (0)