From 85351157e759505fc7e00ef8fe25b0c93590f42b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 14:24:52 +0000 Subject: [PATCH] Write no namespace line for a whitespace-only namespace [patch] WriteFileScopedNamespace checked IsNullOrEmpty, so SourceFileTemplate.Namespace = " " wrote 'namespace ;' (CS1001). Blank usings, attributes and list entries are already skipped with IsNullOrWhiteSpace; the namespace now matches. Fixes ktsu-dev/CodeBlocker#203 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW5YjyP4TWYsDuKLv95 --- CodeBlocker.Test/CodeBlockerExtensionsTests.cs | 13 +++++++++++++ CodeBlocker.Test/TemplateTests.cs | 14 ++++++++++++++ CodeBlocker/CodeBlockerExtensions.cs | 4 ++-- CodeBlocker/Templates/SourceFileTemplate.cs | 2 +- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CodeBlocker.Test/CodeBlockerExtensionsTests.cs b/CodeBlocker.Test/CodeBlockerExtensionsTests.cs index 25ffe88..fcb7bf0 100644 --- a/CodeBlocker.Test/CodeBlockerExtensionsTests.cs +++ b/CodeBlocker.Test/CodeBlockerExtensionsTests.cs @@ -127,6 +127,19 @@ public void AnEmptyNamespaceWritesNothing() Assert.AreEqual(string.Empty, emptyNamespace.ToString()); } + [TestMethod] + public void AWhitespaceNamespaceWritesNothing() + { + using CodeBlocker spaces = Create(); + using CodeBlocker tab = Create(); + + spaces.WriteFileScopedNamespace(" "); + tab.WriteFileScopedNamespace("\t"); + + Assert.AreEqual(string.Empty, spaces.ToString()); + Assert.AreEqual(string.Empty, tab.ToString()); + } + [TestMethod] public void UsingsAreWrittenOnePerLineAndFollowedByABlankLine() { diff --git a/CodeBlocker.Test/TemplateTests.cs b/CodeBlocker.Test/TemplateTests.cs index c703d78..555d6ae 100644 --- a/CodeBlocker.Test/TemplateTests.cs +++ b/CodeBlocker.Test/TemplateTests.cs @@ -2026,6 +2026,20 @@ public void ASourceFileSkipsBlankUsings() Render(codeBlocker => codeBlocker.AddSourceFile(file))); } + [TestMethod] + public void ASourceFileWithAWhitespaceNamespaceWritesNoNamespace() + { + SourceFileTemplate file = new() + { + Namespace = " ", + Usings = { " ", "System" }, + }; + + Assert.AreEqual( + "using System;\n\n", + Render(codeBlocker => codeBlocker.AddSourceFile(file))); + } + [TestMethod] public void ASourceFileWithAnInvalidClassWritesNothing() { diff --git a/CodeBlocker/CodeBlockerExtensions.cs b/CodeBlocker/CodeBlockerExtensions.cs index b1fae56..faddbad 100644 --- a/CodeBlocker/CodeBlockerExtensions.cs +++ b/CodeBlocker/CodeBlockerExtensions.cs @@ -73,7 +73,7 @@ public static CodeBlocker WriteNullableDisable(this CodeBlocker codeBlocker) /// /// Writes a file-scoped namespace declaration followed by a blank line, or nothing at all when - /// no namespace is given. + /// no namespace is given or it is only whitespace. /// /// The to write to. /// The namespace, for example Contoso.Widgets. @@ -83,7 +83,7 @@ public static CodeBlocker WriteFileScopedNamespace(this CodeBlocker codeBlocker, { Ensure.NotNull(codeBlocker); - if (string.IsNullOrEmpty(namespaceName)) + if (string.IsNullOrWhiteSpace(namespaceName)) { return codeBlocker; } diff --git a/CodeBlocker/Templates/SourceFileTemplate.cs b/CodeBlocker/Templates/SourceFileTemplate.cs index ddaf1ea..b8ec5d1 100644 --- a/CodeBlocker/Templates/SourceFileTemplate.cs +++ b/CodeBlocker/Templates/SourceFileTemplate.cs @@ -16,7 +16,7 @@ public class SourceFileTemplate : TemplateBase /// public string FileName { get; set; } = string.Empty; - /// Gets or sets the namespace. Empty writes no namespace declaration. + /// Gets or sets the namespace. Empty or whitespace writes no namespace declaration. public string Namespace { get; set; } = string.Empty; ///