Skip to content

Commit

Permalink
Update SA1210 for file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Jan 28, 2022
1 parent a0463b3 commit cf5eea9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ namespace StyleCop.Analyzers.Test.OrderingRules
/// </summary>
public class SA1210CombinedSystemDirectivesUnitTests
{
protected const string CombinedUsingDirectivesTestSettings = @"
{
""settings"": {
""orderingRules"": {
""systemUsingDirectivesFirst"": false
}
}
}
";

[Fact]
public async Task TestProperOrderedUsingDirectivesInNamespaceDeclarationAsync()
{
Expand Down Expand Up @@ -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<SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace, UsingCodeFixProvider>.CSharpTest
{
TestCode = source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand Down Expand Up @@ -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<SyntaxNodeAnalysisContext, StyleCopSettings> CompilationUnitAction = HandleCompilationUnit;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> NamespaceDeclarationAction = HandleNamespaceDeclaration;
private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> BaseNamespaceDeclarationAction = HandleBaseNamespaceDeclaration;

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
Expand All @@ -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)
Expand All @@ -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);
}
Expand Down

0 comments on commit cf5eea9

Please sign in to comment.