diff --git a/CodeBlocker.Test/ScopesTests.cs b/CodeBlocker.Test/ScopesTests.cs index 701100f..7dc95e2 100644 --- a/CodeBlocker.Test/ScopesTests.cs +++ b/CodeBlocker.Test/ScopesTests.cs @@ -164,6 +164,34 @@ public void PragmaScopeWithAnEmptyStringIsANoOp() Assert.AreEqual("public int Value;\n", codeBlocker.ToString()); } + [TestMethod] + public void PragmaScopeWithOnlyEmptyWarningEntriesIsANoOp() + { + using CodeBlocker codeBlocker = Create(); + + using (new PragmaScope(codeBlocker, ["", " ", null!])) + { + codeBlocker.WriteLine("public int Value;"); + } + + Assert.AreEqual("public int Value;\n", codeBlocker.ToString()); + } + + [TestMethod] + public void PragmaScopeSkipsEmptyWarningEntriesAmongRealOnes() + { + using CodeBlocker codeBlocker = Create(); + + using (new PragmaScope(codeBlocker, ["CS0168", "", " CA1707 "])) + { + codeBlocker.WriteLine("int x;"); + } + + Assert.AreEqual( + "#pragma warning disable CS0168, CA1707\nint x;\n#pragma warning restore CS0168, CA1707\n", + codeBlocker.ToString()); + } + [TestMethod] public void PragmaScopeWithAnEmptyWarningSequenceDoesNotAffectAnOuterScope() { diff --git a/CodeBlocker/Scopes.cs b/CodeBlocker/Scopes.cs index bbbfb8d..f09d869 100644 --- a/CodeBlocker/Scopes.cs +++ b/CodeBlocker/Scopes.cs @@ -178,12 +178,18 @@ public class PragmaScope(CodeBlocker codeBlocker, string warnings) /// Create a new instance of suppressing several warnings. /// /// The parent . - /// The warning identifiers to suppress. + /// + /// The warning identifiers to suppress. Each identifier is trimmed, and null, empty or whitespace + /// entries are skipped, so a sequence holding none but those is a no-op and emits no directives. + /// public PragmaScope(CodeBlocker codeBlocker, IEnumerable warnings) - : this(codeBlocker, string.Join(", ", warnings ?? [])) + : this(codeBlocker, JoinWarnings(warnings)) { } + private static string JoinWarnings(IEnumerable? warnings) => + string.Join(", ", (warnings ?? []).Where(warning => !string.IsNullOrWhiteSpace(warning)).Select(warning => warning.Trim())); + private static void Begin(CodeBlocker codeBlocker, string warnings) { Ensure.NotNull(codeBlocker);