From fc8e8ce76794367132be516688c13ebeea04f43c Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 28 Jul 2023 18:35:06 +0200 Subject: [PATCH] Fix RCS0060 (#1139) --- ChangeLog.md | 1 + ...terFileScopedNamespaceDeclarationAnalyzer.cs | 3 +++ ...AddEmptyLineAfterFileScopedNamespaceTests.cs | 17 +++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 1d60fb0fe7..cf0c68a786 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [CLI] Fix member full declaration in generated documentation (command `generate-doc`) ([#1130](https://github.com/josefpihrt/roslynator/pull/1130)). - Append `?` to nullable reference types. - Fix [RCS1179](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1179.md) ([#1129](https://github.com/JosefPihrt/Roslynator/pull/1129)). +- Fix [RCS0060](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0060.md) ([#1139](https://github.com/JosefPihrt/Roslynator/pull/1139)). ## [4.3.0] - 2023-04-24 diff --git a/src/Formatting.Analyzers/CSharp/BlankLineAfterFileScopedNamespaceDeclarationAnalyzer.cs b/src/Formatting.Analyzers/CSharp/BlankLineAfterFileScopedNamespaceDeclarationAnalyzer.cs index 6d032cc65b..bae7536548 100644 --- a/src/Formatting.Analyzers/CSharp/BlankLineAfterFileScopedNamespaceDeclarationAnalyzer.cs +++ b/src/Formatting.Analyzers/CSharp/BlankLineAfterFileScopedNamespaceDeclarationAnalyzer.cs @@ -40,6 +40,9 @@ private static void AnalyzeFileScopedNamespaceDeclaration(SyntaxNodeAnalysisCont SyntaxNode node = GetNodeAfterNamespaceDeclaration(namespaceDeclaration); + if (node is null) + return; + BlankLineStyle style = context.GetBlankLineAfterFileScopedNamespaceDeclaration(); if (style == BlankLineStyle.None) diff --git a/src/Tests/Formatting.Analyzers.Tests/RCS0060AddEmptyLineAfterFileScopedNamespaceTests.cs b/src/Tests/Formatting.Analyzers.Tests/RCS0060AddEmptyLineAfterFileScopedNamespaceTests.cs index bbc1fa2be2..f63f995fd0 100644 --- a/src/Tests/Formatting.Analyzers.Tests/RCS0060AddEmptyLineAfterFileScopedNamespaceTests.cs +++ b/src/Tests/Formatting.Analyzers.Tests/RCS0060AddEmptyLineAfterFileScopedNamespaceTests.cs @@ -277,4 +277,21 @@ class C } ", options: Options.AddConfigOption(ConfigOptionKeys.BlankLineAfterFileScopedNamespaceDeclaration, false)); } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.BlankLineAfterFileScopedNamespaceDeclaration)] + public async Task TestNoDiagnostic_EmptyFileWithComment() + { + await VerifyNoDiagnosticAsync(@" +namespace A.B; + +// x", options: Options.AddConfigOption(ConfigOptionKeys.BlankLineAfterFileScopedNamespaceDeclaration, false)); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.BlankLineAfterFileScopedNamespaceDeclaration)] + public async Task TestNoDiagnostic_EmptyFileWithComment2() + { + await VerifyNoDiagnosticAsync(@" +namespace A.B; +// x", options: Options.AddConfigOption(ConfigOptionKeys.BlankLineAfterFileScopedNamespaceDeclaration, true)); + } }