From 58f7180780c30aae548453eccfc285528f6e6c06 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Thu, 28 Dec 2023 12:44:46 +0100 Subject: [PATCH] refactor: Added indicator for auto-generated code and nullability+ --- .../AddStubMethodStubGenerator/AddStubGenerator.cs | 3 ++- .../StubComponentBuilder.cs | 5 +++-- .../ComponentStubAttribute.cs | 4 +++- .../ComponentStubAttributeGenerator.cs | 5 +++-- src/bunit.generators/Web.Stubs/HeaderProvider.cs | 13 +++++++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 src/bunit.generators/Web.Stubs/HeaderProvider.cs diff --git a/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs b/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs index 6ecd2c44e..ae654b913 100644 --- a/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs +++ b/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/AddStubGenerator.cs @@ -124,7 +124,8 @@ public InterceptsLocationAttribute(string filePath, int line, int column) """; // Generate the interceptor - var interceptorSource = new StringBuilder(); + var interceptorSource = new StringBuilder(1000); + interceptorSource.AppendLine(HeaderProvider.Header); interceptorSource.AppendLine(attribute); interceptorSource.AppendLine(); interceptorSource.AppendLine("namespace Bunit"); diff --git a/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/StubComponentBuilder.cs b/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/StubComponentBuilder.cs index 5042ad1bb..b65e9f42f 100644 --- a/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/StubComponentBuilder.cs +++ b/src/bunit.generators/Web.Stubs/AddStubMethodStubGenerator/StubComponentBuilder.cs @@ -13,8 +13,9 @@ public static bool GenerateStubComponent(AddStubClassInfo classInfo, SourceProdu { var hasSomethingToStub = false; var targetTypeSymbol = (INamedTypeSymbol)classInfo!.TargetType; - var sourceBuilder = new StringBuilder(); + var sourceBuilder = new StringBuilder(1000); + sourceBuilder.AppendLine(HeaderProvider.Header); sourceBuilder.AppendLine($"namespace {classInfo.TargetTypeNamespace};"); sourceBuilder.AppendLine(); sourceBuilder.AppendLine($"internal partial class {classInfo.StubClassName} : global::Microsoft.AspNetCore.Components.ComponentBase"); @@ -39,7 +40,7 @@ public static bool GenerateStubComponent(AddStubClassInfo classInfo, SourceProdu var attributeLine = GetAttributeLine(member); sourceBuilder.AppendLine(attributeLine); - sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }}"); + sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }} = default!;"); } sourceBuilder.AppendLine("}"); diff --git a/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttribute.cs b/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttribute.cs index 68eb77224..f8c809770 100644 --- a/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttribute.cs +++ b/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttribute.cs @@ -2,7 +2,9 @@ namespace Bunit.Web.Stubs.AttributeStubGenerator; internal static class ComponentStubAttribute { - public static string ComponentStubAttributeSource = """ + public static string ComponentStubAttributeSource = $$""" + {{HeaderProvider.Header}} + #if NET5_0_OR_GREATER namespace Bunit; diff --git a/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs b/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs index 3a40031dc..ea24b4465 100644 --- a/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs +++ b/src/bunit.generators/Web.Stubs/AttributeStubGenerator/ComponentStubAttributeGenerator.cs @@ -73,8 +73,9 @@ private static void Execute(StubClassInfo classInfo, SourceProductionContext con { var hasSomethingToStub = false; var targetTypeSymbol = (INamedTypeSymbol)classInfo!.TargetType; - var sourceBuilder = new StringBuilder(); + var sourceBuilder = new StringBuilder(1000); + sourceBuilder.AppendLine(HeaderProvider.Header); sourceBuilder.AppendLine($"namespace {classInfo.Namespace};"); sourceBuilder.AppendLine( @@ -104,7 +105,7 @@ private static void Execute(StubClassInfo classInfo, SourceProductionContext con : "\t[global::Microsoft.AspNetCore.Components.CascadingParameter]"; sourceBuilder.AppendLine(attributeLine); - sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }}"); + sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }} = default!;"); } sourceBuilder.AppendLine("}"); diff --git a/src/bunit.generators/Web.Stubs/HeaderProvider.cs b/src/bunit.generators/Web.Stubs/HeaderProvider.cs new file mode 100644 index 000000000..9604138a1 --- /dev/null +++ b/src/bunit.generators/Web.Stubs/HeaderProvider.cs @@ -0,0 +1,13 @@ +namespace Bunit.Web.Stubs; + +internal static class HeaderProvider +{ + public const string Header = """ + // + // This code was generated by bunit.generators. + // Changes to this file may cause incorrect behavior and will be lost if + // the code is regenerated. + // + #nullable enable + """; +}