From 9a81210c5e4f45a79eba9ba7f62e69a4804df23e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 15:28:37 +0000 Subject: [PATCH] Refuse a null parameter, member or nested type before writing [patch] A null entry in Parameters, PositionalParameters, Members or NestedClasses used to throw NullReferenceException partway through the declaration, leaving `void M}` or `record R` behind. Validate now rejects them up front with an InvalidOperationException naming the template and collection, and method, constructor and operator WriteTo run that check before writing. Fixes #198 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW5L5j1hRifcohu2Dmb --- CodeBlocker.Test/TemplateTests.cs | 89 ++++++++++++++++++++ CodeBlocker/Templates/ClassTemplate.cs | 12 ++- CodeBlocker/Templates/ConstructorTemplate.cs | 11 +++ CodeBlocker/Templates/MethodTemplate.cs | 11 +++ CodeBlocker/Templates/OperatorTemplate.cs | 14 ++- CodeBlocker/Templates/TemplateRendering.cs | 18 ++++ 6 files changed, 150 insertions(+), 5 deletions(-) diff --git a/CodeBlocker.Test/TemplateTests.cs b/CodeBlocker.Test/TemplateTests.cs index 3fd8237..e0de776 100644 --- a/CodeBlocker.Test/TemplateTests.cs +++ b/CodeBlocker.Test/TemplateTests.cs @@ -1200,6 +1200,95 @@ public void AClassWithAnInvalidMemberWritesNothing() Assert.AreEqual(string.Empty, codeBlocker.ToString()); } + [TestMethod] + public void ANullMethodParameterIsRefusedBeforeTheClassIsWritten() + { + MethodTemplate method = new() { Name = "M", Type = "void", BodyFactory = _ => { } }; + method.Parameters.Add(null!); + ClassTemplate type = new() { Name = "C", Members = { method } }; + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + InvalidOperationException exception = + Assert.ThrowsExactly(() => codeBlocker.AddClass(type)); + Assert.Contains("'M'", exception.Message); + Assert.Contains("Parameters", exception.Message); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + + [TestMethod] + public void ANullPositionalParameterIsRefusedBeforeTheRecordIsWritten() + { + ClassTemplate record = new() { Name = "R", Kind = TypeKind.Record }; + record.PositionalParameters.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + InvalidOperationException exception = + Assert.ThrowsExactly(() => codeBlocker.AddClass(record)); + Assert.Contains("'R'", exception.Message); + Assert.Contains("PositionalParameters", exception.Message); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + + [TestMethod] + public void ANullConstructorParameterIsRefusedBeforeTheClassIsWritten() + { + ConstructorTemplate constructor = new() { Name = "C" }; + constructor.Parameters.Add(null!); + ClassTemplate type = new() { Name = "C", Members = { constructor } }; + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + Assert.ThrowsExactly(() => codeBlocker.AddClass(type)); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + + [TestMethod] + public void ANullOperatorParameterIsRefusedBeforeTheOperatorIsWritten() + { + OperatorTemplate op = new() { Type = "Money", Symbol = "-", Keywords = { "public", "static" } }; + op.Parameters.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + codeBlocker.WriteLine("int a;"); + + Assert.ThrowsExactly(() => op.WriteTo(codeBlocker)); + Assert.AreEqual("int a;\n", codeBlocker.ToString()); + } + + [TestMethod] + public void ANullMethodParameterIsRefusedWhenTheMethodIsWrittenOnItsOwn() + { + MethodTemplate method = new() { Name = "M", Type = "void", BodyFactory = _ => { } }; + method.Parameters.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + codeBlocker.WriteLine("int a;"); + + Assert.ThrowsExactly(() => method.WriteTo(codeBlocker)); + Assert.AreEqual("int a;\n", codeBlocker.ToString()); + } + + [TestMethod] + public void ANullMemberOrNestedClassIsRefusedWithTheTypeNamed() + { + ClassTemplate withNullMember = new() { Name = "A" }; + withNullMember.Members.Add(null!); + ClassTemplate withNullNested = new() { Name = "B" }; + withNullNested.NestedClasses.Add(null!); + + using CodeBlocker codeBlocker = CodeBlocker.Create(CodeBlocker.DefaultIndentString, NewLines.Lf); + + InvalidOperationException memberException = + Assert.ThrowsExactly(() => codeBlocker.AddClass(withNullMember)); + Assert.Contains("'A'", memberException.Message); + InvalidOperationException nestedException = + Assert.ThrowsExactly(() => codeBlocker.AddClass(withNullNested)); + Assert.Contains("'B'", nestedException.Message); + Assert.AreEqual(string.Empty, codeBlocker.ToString()); + } + [TestMethod] public void AnInvalidMemberOfANestedClassLeavesTheOuterClassUnwritten() { diff --git a/CodeBlocker/Templates/ClassTemplate.cs b/CodeBlocker/Templates/ClassTemplate.cs index 9304811..0ebf49a 100644 --- a/CodeBlocker/Templates/ClassTemplate.cs +++ b/CodeBlocker/Templates/ClassTemplate.cs @@ -64,7 +64,9 @@ public class ClassTemplate : TemplateBase /// , or the kind of a nested type, is not a known kind. Nothing is written. /// /// - /// A member, or a member of a nested type, would be refused when written. Nothing is written. + /// A member, or a member of a nested type, would be refused when written, or + /// , or holds a + /// entry. Nothing is written. /// public override void WriteTo(CodeBlocker codeBlocker) { @@ -106,6 +108,14 @@ internal override void Validate() { _ = TypeKindKeywords.For(Kind); + // Null entries are refused here rather than found while writing: a null member or nested + // type would throw a bare NullReferenceException, and a null positional parameter would + // throw after "record R" is already written. + string owner = $"Type '{Name}'"; + TemplateRendering.EnsureNoNullEntries(Members, nameof(Members), owner); + TemplateRendering.EnsureNoNullEntries(NestedClasses, nameof(NestedClasses), owner); + TemplateRendering.EnsureNoNullEntries(PositionalParameters, nameof(PositionalParameters), owner); + foreach (MemberTemplate member in Members) { member.Validate(); diff --git a/CodeBlocker/Templates/ConstructorTemplate.cs b/CodeBlocker/Templates/ConstructorTemplate.cs index 4915a51..a1d8ee1 100644 --- a/CodeBlocker/Templates/ConstructorTemplate.cs +++ b/CodeBlocker/Templates/ConstructorTemplate.cs @@ -39,16 +39,27 @@ public class ConstructorTemplate : MemberTemplate /// /// The to write to. /// is . + /// + /// holds a entry. Nothing is written. + /// public override void WriteTo(CodeBlocker codeBlocker) { Ensure.NotNull(codeBlocker); + Validate(); base.WriteTo(codeBlocker); TemplateRendering.WriteParameterList(codeBlocker, Parameters); WriteInitialiserTo(codeBlocker); TemplateRendering.WriteBody(codeBlocker, BodyFactory); } + /// + internal override void Validate() + { + base.Validate(); + TemplateRendering.EnsureNoNullEntries(Parameters, nameof(Parameters), $"Constructor '{Name}'"); + } + private void WriteInitialiserTo(CodeBlocker codeBlocker) { if (!ChainsToThis && BaseParameters.Count == 0) diff --git a/CodeBlocker/Templates/MethodTemplate.cs b/CodeBlocker/Templates/MethodTemplate.cs index 70b4f91..ba025c7 100644 --- a/CodeBlocker/Templates/MethodTemplate.cs +++ b/CodeBlocker/Templates/MethodTemplate.cs @@ -39,14 +39,25 @@ public class MethodTemplate : MemberTemplate /// /// The to write to. /// is . + /// + /// holds a entry. Nothing is written. + /// public override void WriteTo(CodeBlocker codeBlocker) { Ensure.NotNull(codeBlocker); + Validate(); base.WriteTo(codeBlocker); TemplateRendering.WriteTypeParameterList(codeBlocker, TypeParameters); TemplateRendering.WriteParameterList(codeBlocker, Parameters); TemplateRendering.WriteConstraints(codeBlocker, Constraints); TemplateRendering.WriteBody(codeBlocker, BodyFactory); } + + /// + internal override void Validate() + { + base.Validate(); + TemplateRendering.EnsureNoNullEntries(Parameters, nameof(Parameters), $"Method '{Name}'"); + } } diff --git a/CodeBlocker/Templates/OperatorTemplate.cs b/CodeBlocker/Templates/OperatorTemplate.cs index 4c237b3..908c0e8 100644 --- a/CodeBlocker/Templates/OperatorTemplate.cs +++ b/CodeBlocker/Templates/OperatorTemplate.cs @@ -60,14 +60,16 @@ public class OperatorTemplate : MemberTemplate /// The to write to. /// is . /// - /// is not an value. Nothing is written. + /// is not an value, or + /// holds a entry. Nothing is written. /// public override void WriteTo(CodeBlocker codeBlocker) { Ensure.NotNull(codeBlocker); - // Worked out before anything is written, so an unknown kind cannot leave a half-written - // declaration behind. + // Checked before anything is written, so an unknown kind or a null parameter cannot leave a + // half-written declaration behind. + Validate(); string signature = Signature; // Not through the MemberTemplate implementation: an operator's signature is its symbol and @@ -80,7 +82,11 @@ public override void WriteTo(CodeBlocker codeBlocker) } /// - internal override void Validate() => _ = Signature; + internal override void Validate() + { + string signature = Signature; + TemplateRendering.EnsureNoNullEntries(Parameters, nameof(Parameters), $"Operator '{signature}'"); + } private string Signature => Kind switch { diff --git a/CodeBlocker/Templates/TemplateRendering.cs b/CodeBlocker/Templates/TemplateRendering.cs index f993ece..9151d7c 100644 --- a/CodeBlocker/Templates/TemplateRendering.cs +++ b/CodeBlocker/Templates/TemplateRendering.cs @@ -191,6 +191,24 @@ private static bool IsLoneSurrogate(string value, int index) return false; } + /// + /// Throws if a template's collection holds a entry, which would otherwise + /// only be found partway through writing the declaration that lists it. + /// + /// The entry type. + /// The collection to check. + /// The collection's property name, for the message. + /// The template that holds the collection, for the message. + /// An entry is . + internal static void EnsureNoNullEntries(IEnumerable entries, string collectionName, string owner) + where T : class + { + if (entries.Any(entry => entry is null)) + { + throw new InvalidOperationException($"{owner} has a null entry in {collectionName}."); + } + } + /// /// Writes a parenthesised, comma-separated parameter list. ///