Skip to content

Commit 7b95c1b

Browse files
authored
Fix analyzer RCS0010 (#1620)
1 parent b271158 commit 7b95c1b

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Fix analyzer [RCS1229](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1229) ([PR](https://github.com/dotnet/roslynator/pull/1618))
1313
- Fix analyzer [RCS1174](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1174) ([PR](https://github.com/dotnet/roslynator/pull/1619))
14+
- Fix analyzer [RCS0010](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0010) ([PR](https://github.com/dotnet/roslynator/pull/1620))
1415

1516
### Added
1617

src/Formatting.Analyzers/CSharp/BlankLineBetweenDeclarationsAnalyzer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ public override void Initialize(AnalysisContext context)
4848
SyntaxKind.InterfaceDeclaration);
4949

5050
context.RegisterSyntaxNodeAction(f => AnalyzeCompilationUnit(f), SyntaxKind.CompilationUnit);
51-
context.RegisterSyntaxNodeAction(f => AnalyzeNamespaceDeclaration(f), SyntaxKind.NamespaceDeclaration);
51+
context.RegisterSyntaxNodeAction(
52+
f => AnalyzeNamespaceDeclaration(f),
53+
#if ROSLYN_4_0
54+
SyntaxKind.FileScopedNamespaceDeclaration,
55+
#endif
56+
SyntaxKind.NamespaceDeclaration);
5257
context.RegisterSyntaxNodeAction(f => AnalyzeEnumDeclaration(f), SyntaxKind.EnumDeclaration);
5358
}
5459

@@ -61,8 +66,11 @@ private static void AnalyzeCompilationUnit(SyntaxNodeAnalysisContext context)
6166

6267
private static void AnalyzeNamespaceDeclaration(SyntaxNodeAnalysisContext context)
6368
{
69+
#if ROSLYN_4_0
70+
var namespaceDeclaration = (BaseNamespaceDeclarationSyntax)context.Node;
71+
#else
6472
var namespaceDeclaration = (NamespaceDeclarationSyntax)context.Node;
65-
73+
#endif
6674
Analyze(context, namespaceDeclaration.Members);
6775
}
6876

src/Tests/Formatting.Analyzers.Tests/RCS0010AddBlankLineBetweenDeclarationsTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ enum E
162162
");
163163
}
164164

165+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddBlankLineBetweenDeclarations)]
166+
public async Task Test_FileScopedNamespace()
167+
{
168+
await VerifyDiagnosticAndFixAsync("""
169+
namespace N;
170+
171+
class Foo
172+
{
173+
void M() { }
174+
}[|
175+
|]enum Bar
176+
{
177+
A = 0
178+
}
179+
""", """
180+
namespace N;
181+
182+
class Foo
183+
{
184+
void M() { }
185+
}
186+
187+
enum Bar
188+
{
189+
A = 0
190+
}
191+
""");
192+
}
193+
165194
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddBlankLineBetweenDeclarations)]
166195
public async Task TestNoDiagnostic_MemberDeclaration_SingleLine()
167196
{

0 commit comments

Comments
 (0)