Skip to content

Commit 5e47f43

Browse files
committed
ensure code is nullable compliant
1 parent 40fed67 commit 5e47f43

27 files changed

+324
-198
lines changed

CodeGenHelpers.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeGenHelpers.Tests", "tes
1313
EndProject
1414
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8F6F4081-C731-48F9-88C9-7AA98D7C265C}"
1515
ProjectSection(SolutionItems) = preProject
16+
global.json = global.json
1617
version.json = version.json
1718
EndProjectSection
1819
EndProject

global.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"sdk": {
3-
"version": "6.0.101",
4-
"rollForward": "latestMinor",
5-
"allowPrerelease": false
3+
"version": "6.0.400"
64
}
75
}

src/CodeGenHelpers/AvantiPoint.CodeGenHelpers.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<DevelopmentDependency>true</DevelopmentDependency>
1414
<IncludeBuildOutput>false</IncludeBuildOutput>
1515
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddGeneratorsToOutput</TargetsForTfmSpecificContentInPackage>
16+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
17+
<NoWarn>$(NoWarn);NU5128;</NoWarn>
1618
</PropertyGroup>
1719

1820
<ItemGroup>

src/CodeGenHelpers/BuilderBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System.Collections.Generic;
22
using System.Linq;
33

4+
#pragma warning disable IDE0008
5+
#pragma warning disable IDE0090
6+
#nullable enable
47
namespace CodeGenHelpers
58
{
69
public abstract class BuilderBase : IBuilder

src/CodeGenHelpers/BuilderBase{T}.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.CodeAnalysis;
1+
using System;
2+
using Microsoft.CodeAnalysis;
23

4+
#nullable enable
35
namespace CodeGenHelpers
46
{
57
public abstract class BuilderBase<T> : BuilderBase
@@ -12,7 +14,10 @@ public abstract class BuilderBase<T> : BuilderBase
1214
public T DisableWarning(string buildCode)
1315
{
1416
_pragmaWarnings.Add(buildCode);
15-
return this as T;
17+
if (this is T thisAsT)
18+
return thisAsT;
19+
20+
throw new InvalidOperationException($"The Builder must be of type {typeof(T).FullName}");
1621
}
1722
}
1823
}

src/CodeGenHelpers/ClassBuilder.cs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
using CodeGenHelpers.Internals;
66
using Microsoft.CodeAnalysis;
77

8+
#pragma warning disable IDE0008
9+
#pragma warning disable IDE0079
10+
#pragma warning disable IDE0090
11+
#pragma warning disable IDE1006
12+
#nullable enable
813
namespace CodeGenHelpers
914
{
1015
public sealed class ClassBuilder : BuilderBase<ClassBuilder>
@@ -19,7 +24,7 @@ public sealed class ClassBuilder : BuilderBase<ClassBuilder>
1924
private readonly Queue<ClassBuilder> _nestedClass = new Queue<ClassBuilder>();
2025
private readonly GenericCollection _generics = new GenericCollection();
2126
private readonly bool _isPartial;
22-
private readonly DocumentationComment _xmlDoc = new DocumentationComment();
27+
private DocumentationComment? _xmlDoc;
2328

2429
internal ClassBuilder(string className, CodeBuilder codeBuilder, bool partial = true)
2530
{
@@ -42,7 +47,7 @@ internal ClassBuilder(string className, CodeBuilder codeBuilder, bool partial =
4247

4348
public CodeBuilder Builder { get; }
4449

45-
public string BaseClass { get; private set; }
50+
public string? BaseClass { get; private set; }
4651

4752
public Accessibility? AccessModifier { get; private set; }
4853

@@ -56,22 +61,19 @@ internal ClassBuilder(string className, CodeBuilder codeBuilder, bool partial =
5661

5762
public ClassBuilder WithSummary(string summary)
5863
{
59-
_xmlDoc.Summary = summary;
60-
_xmlDoc.InheritDoc = false;
64+
_xmlDoc = new SummaryDocumentationComment { Summary = summary };
6165
return this;
6266
}
6367

6468
public ClassBuilder WithInheritDoc(bool inherit = true)
6569
{
66-
_xmlDoc.InheritDoc = inherit;
67-
_xmlDoc.InheritFrom = null;
70+
_xmlDoc = new InheritDocumentationComment();
6871
return this;
6972
}
7073

7174
public ClassBuilder WithInheritDoc(string from)
7275
{
73-
_xmlDoc.InheritDoc = true;
74-
_xmlDoc.InheritFrom = from;
76+
_xmlDoc = new InheritDocumentationComment { InheritFrom = from };
7577
return this;
7678
}
7779

@@ -280,11 +282,9 @@ public ClassBuilder AddNestedClass(string name, Accessibility? accessModifier =
280282

281283
public string Build() => Builder.Build();
282284

283-
public string BuildSafe() => Builder.BuildSafe();
284-
285285
internal override void Write(in CodeWriter writer)
286286
{
287-
_xmlDoc.Write(writer);
287+
_xmlDoc?.Write(writer);
288288

289289
WriteClassAttributes(_classAttributes, writer);
290290

@@ -293,7 +293,7 @@ internal override void Write(in CodeWriter writer)
293293
var queue = new Queue<string>();
294294
if (!string.IsNullOrEmpty(BaseClass))
295295
{
296-
queue.Enqueue(BaseClass);
296+
queue.Enqueue(BaseClass!);
297297
}
298298

299299
foreach (var inter in _interfaces.Where(x => !string.IsNullOrEmpty(x)).Distinct().OrderBy(x => x))
@@ -323,13 +323,41 @@ internal override void Write(in CodeWriter writer)
323323
{
324324
var hadOutput = false;
325325
hadOutput = InvokeBuilderWrite(_events, ref hadOutput, writer);
326-
hadOutput = InvokeBuilderWrite(_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Const && x.IsStatic == false), ref hadOutput, writer, true);
327-
hadOutput = InvokeBuilderWrite(_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Const && x.IsStatic == true), ref hadOutput, writer, true);
328-
hadOutput = InvokeBuilderWrite(_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.ReadOnly), ref hadOutput, writer, true);
329-
hadOutput = InvokeBuilderWrite(_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Default), ref hadOutput, writer, true);
330-
hadOutput = InvokeBuilderWrite(_constructors, ref hadOutput, writer);
331-
hadOutput = InvokeBuilderWrite(_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Property), ref hadOutput, writer);
332-
hadOutput = InvokeBuilderWrite(_methods, ref hadOutput, writer);
326+
hadOutput = InvokeBuilderWrite(
327+
_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Const && x.IsStatic == false)
328+
.OrderBy(x => x.Name),
329+
ref hadOutput,
330+
writer,
331+
true);
332+
hadOutput = InvokeBuilderWrite(
333+
_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Const && x.IsStatic == true)
334+
.OrderBy(x => x.Name),
335+
ref hadOutput,
336+
writer,
337+
true);
338+
hadOutput = InvokeBuilderWrite(
339+
_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.ReadOnly)
340+
.OrderBy(x => x.Name),
341+
ref hadOutput,
342+
writer,
343+
true);
344+
hadOutput = InvokeBuilderWrite(
345+
_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Default)
346+
.OrderBy(x => x.Name),
347+
ref hadOutput,
348+
writer,
349+
true);
350+
hadOutput = InvokeBuilderWrite(_constructors.OrderBy(x => x.Parameters.Count), ref hadOutput, writer);
351+
hadOutput = InvokeBuilderWrite(
352+
_properties.Where(x => x.FieldTypeValue == PropertyBuilder.FieldType.Property)
353+
.OrderBy(x => x.Name),
354+
ref hadOutput,
355+
writer);
356+
hadOutput = InvokeBuilderWrite(
357+
_methods.OrderBy(x => x.Name)
358+
.ThenBy(x => x.Parameters.Count),
359+
ref hadOutput,
360+
writer);
333361
InvokeBuilderWrite(_nestedClass, ref hadOutput, writer);
334362
}
335363
}

src/CodeGenHelpers/CodeBuilder.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
using System.Text.RegularExpressions;
55
using Microsoft.CodeAnalysis;
66

7+
#pragma warning disable IDE0079
8+
#pragma warning disable IDE0090
9+
#pragma warning disable IDE1006
10+
#nullable enable
711
namespace CodeGenHelpers
812
{
913
public sealed class CodeBuilder
@@ -12,7 +16,7 @@ public sealed class CodeBuilder
1216
1317
Changes to this file may cause incorrect behavior and will be lost if
1418
the code is regenerated.";
15-
private string _autoGeneratedMessage;
19+
private string? _autoGeneratedMessage;
1620
private readonly List<string> _namespaceImports = new List<string>();
1721
private readonly List<string> _assemblyAttributes = new List<string>();
1822
private readonly Queue<IBuilder> _classes = new Queue<IBuilder>();
@@ -170,13 +174,6 @@ public string Build()
170174
return writer.ToString();
171175
}
172176

173-
public string BuildSafe()
174-
{
175-
var writer = BuildInternal();
176-
writer.ToString();
177-
return writer.SafeOutput;
178-
}
179-
180177
public override string ToString()
181178
{
182179
return Build();

src/CodeGenHelpers/ConstructorBuilder.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33
using System.Linq;
44
using Microsoft.CodeAnalysis;
55

6+
#pragma warning disable IDE0079
7+
#pragma warning disable IDE0090
8+
#pragma warning disable IDE1006
9+
#nullable enable
610
namespace CodeGenHelpers
711
{
812
public sealed class ConstructorBuilder : BuilderBase<ConstructorBuilder>, IParameterized<ConstructorBuilder>
913
{
1014
private readonly List<ParameterBuilder<ConstructorBuilder>> _parameters = new List<ParameterBuilder<ConstructorBuilder>>();
1115
private readonly List<string> _attributes = new List<string>();
12-
private readonly DocumentationComment _xmlDoc = new DocumentationComment(true);
16+
private DocumentationComment? _xmlDoc;
1317

14-
private Action<ICodeWriter> _methodBodyWriter;
18+
private Action<ICodeWriter>? _methodBodyWriter;
1519

1620
private Func<string> _baseCall;
1721

1822
internal ConstructorBuilder(Accessibility? accessModifier, ClassBuilder classBuilder)
1923
{
2024
AccessModifier = accessModifier;
2125
Class = classBuilder;
26+
_baseCall = () => string.Empty;
2227
}
2328

2429
List<ParameterBuilder<ConstructorBuilder>> IParameterized<ConstructorBuilder>.Parameters => _parameters;
@@ -35,28 +40,35 @@ internal ConstructorBuilder(Accessibility? accessModifier, ClassBuilder classBui
3540

3641
public ConstructorBuilder WithSummary(string summary)
3742
{
38-
_xmlDoc.Summary = summary;
39-
_xmlDoc.InheritDoc = false;
43+
if (_xmlDoc is not null && _xmlDoc is SummaryDocumentationComment summaryComment)
44+
summaryComment.Summary = summary;
45+
else
46+
_xmlDoc = new ParameterDocumentationComment { Summary = summary };
47+
4048
return this;
4149
}
4250

4351
public ConstructorBuilder WithInheritDoc(bool inherit = true)
4452
{
45-
_xmlDoc.InheritDoc = inherit;
46-
_xmlDoc.InheritFrom = null;
53+
_xmlDoc = new InheritDocumentationComment();
4754
return this;
4855
}
4956

5057
public ConstructorBuilder WithInheritDoc(string from)
5158
{
52-
_xmlDoc.InheritDoc = true;
53-
_xmlDoc.InheritFrom = from;
59+
_xmlDoc = new InheritDocumentationComment { InheritFrom = from };
5460
return this;
5561
}
5662

5763
public ConstructorBuilder WithParameterDoc(string paramName, string documentation)
5864
{
59-
_xmlDoc.ParameterDoc[paramName] = documentation;
65+
if (_xmlDoc is not null && _xmlDoc is not ParameterDocumentationComment)
66+
throw new Exception("Documentation Comment has already been initialized using an InheritDoc.");
67+
68+
var parameterDoc = _xmlDoc as ParameterDocumentationComment;
69+
if(parameterDoc is not null)
70+
parameterDoc.AddParameter(paramName, documentation);
71+
6072
return this;
6173
}
6274

@@ -178,8 +190,10 @@ public ConstructorBuilder AddConstructor(Accessibility? accessModifier = null)
178190

179191
internal override void Write(in CodeWriter writer)
180192
{
181-
_xmlDoc.RemoveUnusedParameters(_parameters);
182-
_xmlDoc.Write(writer);
193+
if(_xmlDoc is ParameterDocumentationComment parameterDocumentation)
194+
parameterDocumentation.RemoveUnusedParameters(_parameters);
195+
196+
_xmlDoc?.Write(writer);
183197

184198
foreach (var attribute in _attributes)
185199
writer.AppendLine($"[{attribute}]");
@@ -190,7 +204,7 @@ internal override void Write(in CodeWriter writer)
190204
_ => AccessModifier.ToString().ToLower()
191205
};
192206
var parameters = _parameters.Any() ? string.Join(", ", _parameters.Select(x => x.ToString())) : string.Empty;
193-
using(writer.Block($"{modifier} {Class.Name}({parameters})", _baseCall?.Invoke()))
207+
using(writer.Block($"{modifier} {Class.Name}({parameters})", _baseCall.Invoke()))
194208
{
195209
_methodBodyWriter?.Invoke(writer);
196210
}

0 commit comments

Comments
 (0)