diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10CombinedSystemDirectivesUnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10CombinedSystemDirectivesUnitTests.cs index 58eab7583..bb3e6509c 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10CombinedSystemDirectivesUnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10CombinedSystemDirectivesUnitTests.cs @@ -3,9 +3,51 @@ namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules { + using System.Threading; + using System.Threading.Tasks; using StyleCop.Analyzers.Test.CSharp9.OrderingRules; + using Xunit; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.OrderingRules.SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace, + StyleCop.Analyzers.OrderingRules.UsingCodeFixProvider>; public class SA1210CSharp10CombinedSystemDirectivesUnitTests : SA1210CSharp9CombinedSystemDirectivesUnitTests { + [Fact] + public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync() + { + await new CSharpTest + { + TestSources = + { + @"namespace Food; + +[|using System.Threading;|] +using System; +", + @"namespace Bar; + +[|using Food;|] +using Bar; +[|using System.Threading;|] +using System; +", + }, + FixedSources = + { + @"namespace Food; +using System; +using System.Threading; +", + @"namespace Bar; +using Bar; +using Food; +using System; +using System.Threading; +", + }, + Settings = CombinedUsingDirectivesTestSettings, + }.RunAsync(CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10UnitTests.cs index 84afe12e0..c9db20a85 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/OrderingRules/SA1210CSharp10UnitTests.cs @@ -3,9 +3,51 @@ namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules { + using System.Threading; + using System.Threading.Tasks; + using Microsoft.CodeAnalysis.Testing; using StyleCop.Analyzers.Test.CSharp9.OrderingRules; + using Xunit; + using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier< + StyleCop.Analyzers.OrderingRules.SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace, + StyleCop.Analyzers.OrderingRules.UsingCodeFixProvider>; public class SA1210CSharp10UnitTests : SA1210CSharp9UnitTests { + [Fact] + public async Task TestUsingDirectivesInFileScopedNamespaceDeclarationAsync() + { + await new CSharpTest + { + TestSources = + { + @"namespace Foo; + +[|using System.Threading;|] +using System; +", + @"namespace Bar; + +[|using Foo;|] +using Bar; +[|using System.Threading;|] +using System; +", + }, + FixedSources = + { + @"namespace Foo; +using System; +using System.Threading; +", + @"namespace Bar; +using System; +using System.Threading; +using Bar; +using Foo; +", + }, + }.RunAsync(CancellationToken.None).ConfigureAwait(false); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210CombinedSystemDirectivesUnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210CombinedSystemDirectivesUnitTests.cs index 29acbefdc..0f9d82008 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210CombinedSystemDirectivesUnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1210CombinedSystemDirectivesUnitTests.cs @@ -18,6 +18,16 @@ namespace StyleCop.Analyzers.Test.OrderingRules /// public class SA1210CombinedSystemDirectivesUnitTests { + protected const string CombinedUsingDirectivesTestSettings = @" +{ + ""settings"": { + ""orderingRules"": { + ""systemUsingDirectivesFirst"": false + } + } +} +"; + [Fact] public async Task TestProperOrderedUsingDirectivesInNamespaceDeclarationAsync() { @@ -212,16 +222,6 @@ private static Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[ private static Task VerifyCSharpFixAsync(string source, DiagnosticResult[] expected, string fixedSource, CancellationToken cancellationToken) { - const string CombinedUsingDirectivesTestSettings = @" -{ - ""settings"": { - ""orderingRules"": { - ""systemUsingDirectivesFirst"": false - } - } -} -"; - var test = new StyleCopCodeFixVerifier.CSharpTest { TestCode = source, diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace.cs b/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace.cs index 99424ebc2..1fad7c2f5 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace.cs @@ -11,6 +11,7 @@ namespace StyleCop.Analyzers.OrderingRules using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using StyleCop.Analyzers.Helpers; + using StyleCop.Analyzers.Lightup; using StyleCop.Analyzers.Settings.ObjectModel; /// @@ -40,7 +41,7 @@ internal class SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace : Dia new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.OrderingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink); private static readonly Action CompilationUnitAction = HandleCompilationUnit; - private static readonly Action NamespaceDeclarationAction = HandleNamespaceDeclaration; + private static readonly Action BaseNamespaceDeclarationAction = HandleBaseNamespaceDeclaration; /// public override ImmutableArray SupportedDiagnostics { get; } = @@ -53,7 +54,7 @@ public override void Initialize(AnalysisContext context) context.EnableConcurrentExecution(); context.RegisterSyntaxNodeAction(CompilationUnitAction, SyntaxKind.CompilationUnit); - context.RegisterSyntaxNodeAction(NamespaceDeclarationAction, SyntaxKind.NamespaceDeclaration); + context.RegisterSyntaxNodeAction(BaseNamespaceDeclarationAction, SyntaxKinds.BaseNamespaceDeclaration); } private static void HandleCompilationUnit(SyntaxNodeAnalysisContext context, StyleCopSettings settings) @@ -63,9 +64,9 @@ private static void HandleCompilationUnit(SyntaxNodeAnalysisContext context, Sty ProcessUsings(context, settings.OrderingRules, compilationUnit.Usings); } - private static void HandleNamespaceDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings) + private static void HandleBaseNamespaceDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings) { - var namespaceDeclaration = (NamespaceDeclarationSyntax)context.Node; + var namespaceDeclaration = (BaseNamespaceDeclarationSyntaxWrapper)context.Node; ProcessUsings(context, settings.OrderingRules, namespaceDeclaration.Usings); }