Skip to content

Commit e477fbd

Browse files
committed
Implement code fix support for SA1208 with file-scoped namespaces
1 parent 7172c37 commit e477fbd

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.SourceMap.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace StyleCop.Analyzers.OrderingRules
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.CSharp;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
13+
using StyleCop.Analyzers.Lightup;
1314

1415
/// <summary>
1516
/// Implements a code fix for all misaligned using statements.
@@ -123,12 +124,12 @@ internal TreeTextSpan GetContainingSpan(SyntaxNode node)
123124

124125
private static void ProcessNodeMembers(TreeTextSpan.Builder builder, SyntaxList<MemberDeclarationSyntax> members)
125126
{
126-
foreach (var namespaceDeclaration in members.OfType<NamespaceDeclarationSyntax>())
127+
foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
127128
{
128129
var childBuilder = builder.AddChild(namespaceDeclaration.FullSpan.Start);
129130
childBuilder.SetEnd(namespaceDeclaration.FullSpan.End);
130131

131-
ProcessNodeMembers(childBuilder, namespaceDeclaration.Members);
132+
ProcessNodeMembers(childBuilder, ((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
132133
}
133134
}
134135

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ private bool StartsWithSystemUsingDirectiveIdentifier(NameSyntax name)
467467

468468
private void ProcessMembers(SyntaxList<MemberDeclarationSyntax> members)
469469
{
470-
foreach (var namespaceDeclaration in members.OfType<NamespaceDeclarationSyntax>())
470+
foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
471471
{
472-
this.ProcessUsingDirectives(namespaceDeclaration.Usings);
473-
this.ProcessMembers(namespaceDeclaration.Members);
472+
this.ProcessUsingDirectives(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Usings);
473+
this.ProcessMembers(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
474474
}
475475
}
476476

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace StyleCop.Analyzers.OrderingRules
1616
using Microsoft.CodeAnalysis.CSharp;
1717
using Microsoft.CodeAnalysis.CSharp.Syntax;
1818
using StyleCop.Analyzers.Helpers;
19+
using StyleCop.Analyzers.Lightup;
1920
using StyleCop.Analyzers.Settings.ObjectModel;
2021

2122
/// <summary>
@@ -134,7 +135,7 @@ private static string DetermineIndentation(CompilationUnitSyntax compilationUnit
134135

135136
if (usingDirectivesPlacement == UsingDirectivesPlacement.InsideNamespace)
136137
{
137-
var rootNamespace = compilationUnit.Members.OfType<NamespaceDeclarationSyntax>().First();
138+
var rootNamespace = compilationUnit.Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
138139
var indentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, rootNamespace);
139140
usingsIndentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationLevel + 1);
140141
}
@@ -185,9 +186,9 @@ private static int CountNamespaces(SyntaxList<MemberDeclarationSyntax> members)
185186
{
186187
var result = 0;
187188

188-
foreach (var namespaceDeclaration in members.OfType<NamespaceDeclarationSyntax>())
189+
foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
189190
{
190-
result += 1 + CountNamespaces(namespaceDeclaration.Members);
191+
result += 1 + CountNamespaces(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
191192
}
192193

193194
return result;
@@ -267,7 +268,7 @@ private static int CompareSpanStart(UsingDirectiveSyntax left, UsingDirectiveSyn
267268

268269
private static SyntaxNode AddUsingsToNamespace(SyntaxNode newSyntaxRoot, UsingsSorter usingsHelper, string usingsIndentation, bool hasConditionalDirectives)
269270
{
270-
var rootNamespace = ((CompilationUnitSyntax)newSyntaxRoot).Members.OfType<NamespaceDeclarationSyntax>().First();
271+
var rootNamespace = (BaseNamespaceDeclarationSyntaxWrapper)((CompilationUnitSyntax)newSyntaxRoot).Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
271272
var withTrailingBlankLine = hasConditionalDirectives || rootNamespace.Members.Any() || rootNamespace.Externs.Any();
272273

273274
var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withTrailingBlankLine, qualifyNames: false);

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace StyleCop.Analyzers.Test.CSharp10.OrderingRules
1414
public class SA1208CSharp10UnitTests : SA1208CSharp9UnitTests
1515
{
1616
[Fact]
17+
[WorkItem(3437, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3437")]
1718
public async Task TestWhenSystemUsingDirectivesAreNotOnTopInFileScopedNamespaceAsync()
1819
{
1920
await new CSharpTest
@@ -26,20 +27,36 @@ public async Task TestWhenSystemUsingDirectivesAreNotOnTopInFileScopedNamespaceA
2627
namespace Test;
2728
2829
using Xyz;
30+
{|#0:using System;|}
31+
{|#1:using System.IO;|}
32+
using AnotherNamespace;
33+
{|#2:using System.Threading.Tasks;|}
34+
35+
class A
36+
{
37+
}",
38+
},
39+
FixedSources =
40+
{
41+
"namespace Xyz {}",
42+
"namespace AnotherNamespace {}",
43+
@"
44+
namespace Test;
2945
using System;
3046
using System.IO;
31-
using AnotherNamespace;
3247
using System.Threading.Tasks;
48+
using AnotherNamespace;
49+
using Xyz;
3350
3451
class A
3552
{
3653
}",
3754
},
3855
ExpectedDiagnostics =
3956
{
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"),
57+
Diagnostic().WithLocation(0).WithArguments("System", "Xyz"),
58+
Diagnostic().WithLocation(1).WithArguments("System.IO", "Xyz"),
59+
Diagnostic().WithLocation(2).WithArguments("System.Threading.Tasks", "Xyz"),
4360
},
4461
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
4562
}

0 commit comments

Comments
 (0)