Skip to content

Commit 1bcc864

Browse files
committed
Enable nullable reference types
1 parent 80f3eaf commit 1bcc864

3 files changed

Lines changed: 31 additions & 22 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeGeneration/OperationLightupGenerator.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ private void GenerateOperationInterface(in GeneratorExecutionContext context, In
179179
identifier: SyntaxFactory.Identifier(node.WrapperName),
180180
parameterList: SyntaxFactory.ParameterList(),
181181
initializer: null,
182-
body: SyntaxFactory.Block(staticCtorStatements)));
182+
body: SyntaxFactory.Block(staticCtorStatements),
183+
expressionBody: null));
183184

184185
// private IArgumentOperationWrapper(IOperation operation)
185186
// {
@@ -203,7 +204,8 @@ private void GenerateOperationInterface(in GeneratorExecutionContext context, In
203204
SyntaxKind.SimpleMemberAccessExpression,
204205
expression: SyntaxFactory.ThisExpression(),
205206
name: SyntaxFactory.IdentifierName("operation")),
206-
right: SyntaxFactory.IdentifierName("operation"))))));
207+
right: SyntaxFactory.IdentifierName("operation")))),
208+
expressionBody: null));
207209

208210
// public IOperation WrappedOperation => this.operation;
209211
members = members.Add(SyntaxFactory.PropertyDeclaration(
@@ -679,7 +681,8 @@ private void GenerateOperationWrapperHelper(in GeneratorExecutionContext context
679681
identifier: SyntaxFactory.Identifier("OperationWrapperHelper"),
680682
parameterList: SyntaxFactory.ParameterList(),
681683
initializer: null,
682-
body: SyntaxFactory.Block(staticCtorStatements));
684+
body: SyntaxFactory.Block(staticCtorStatements),
685+
expressionBody: null);
683686

684687
// internal static Type GetWrappedType(Type wrapperType)
685688
// {
@@ -831,7 +834,7 @@ public DocumentData(XDocument document)
831834

832835
if (!operationKinds.TryGetValue(node.Attribute("Name").Value, out var kinds))
833836
{
834-
kinds = ImmutableArray<(string name, int value, string extraDescription)>.Empty;
837+
kinds = ImmutableArray<(string name, int value, string? extraDescription)>.Empty;
835838
}
836839

837840
var interfaceData = new InterfaceData(this, node, kinds);
@@ -847,7 +850,7 @@ public DocumentData(XDocument document)
847850

848851
if (!operationKinds.TryGetValue(node.Attribute("Name").Value, out var kinds))
849852
{
850-
kinds = ImmutableArray<(string name, int value, string extraDescription)>.Empty;
853+
kinds = ImmutableArray<(string name, int value, string? extraDescription)>.Empty;
851854
}
852855

853856
var interfaceData = new InterfaceData(this, node, kinds);
@@ -859,11 +862,11 @@ public DocumentData(XDocument document)
859862

860863
public ReadOnlyDictionary<string, InterfaceData> Interfaces { get; }
861864

862-
private static ImmutableDictionary<string, ImmutableArray<(string name, int value, string extraDescription)>> GetOperationKinds(XDocument document)
865+
private static ImmutableDictionary<string, ImmutableArray<(string name, int value, string? extraDescription)>> GetOperationKinds(XDocument document)
863866
{
864867
var skippedOperationKinds = GetSkippedOperationKinds(document);
865868

866-
var builder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<(string name, int value, string extraDescription)>>();
869+
var builder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<(string name, int value, string? extraDescription)>>();
867870

868871
int operationKind = 0;
869872
foreach (var node in document.XPathSelectElements("/Tree/AbstractNode|/Tree/Node"))
@@ -886,7 +889,7 @@ public DocumentData(XDocument document)
886889
}
887890
else if (explicitKind.XPathSelectElements("Entry").Any())
888891
{
889-
var nodeBuilder = ImmutableArray.CreateBuilder<(string name, int value, string extraDescription)>();
892+
var nodeBuilder = ImmutableArray.CreateBuilder<(string name, int value, string? extraDescription)>();
890893
foreach (var entry in explicitKind.XPathSelectElements("Entry"))
891894
{
892895
if (entry.Attribute("EditorBrowsable")?.Value == "false")
@@ -918,7 +921,7 @@ public DocumentData(XDocument document)
918921

919922
var nodeName = node.Attribute("Name").Value;
920923
var kindName = nodeName.Substring("I".Length, nodeName.Length - "I".Length - "Operation".Length);
921-
builder.Add(nodeName, ImmutableArray.Create((kindName, operationKind, (string)null)));
924+
builder.Add(nodeName, ImmutableArray.Create((kindName, operationKind, (string?)null)));
922925
}
923926

924927
return builder.ToImmutable();
@@ -955,7 +958,7 @@ private sealed class InterfaceData
955958
{
956959
private readonly DocumentData documentData;
957960

958-
public InterfaceData(DocumentData documentData, XElement node, ImmutableArray<(string name, int value, string extraDescription)> operationKinds)
961+
public InterfaceData(DocumentData documentData, XElement node, ImmutableArray<(string name, int value, string? extraDescription)> operationKinds)
959962
{
960963
this.documentData = documentData;
961964

@@ -968,7 +971,7 @@ public InterfaceData(DocumentData documentData, XElement node, ImmutableArray<(s
968971
this.Properties = node.XPathSelectElements("Property").Select(property => new PropertyData(property)).ToImmutableArray();
969972
}
970973

971-
public ImmutableArray<(string name, int value, string extraDescription)> OperationKinds { get; }
974+
public ImmutableArray<(string name, int value, string? extraDescription)> OperationKinds { get; }
972975

973976
public string InterfaceName { get; }
974977

@@ -982,7 +985,7 @@ public InterfaceData(DocumentData documentData, XElement node, ImmutableArray<(s
982985

983986
public ImmutableArray<PropertyData> Properties { get; }
984987

985-
public InterfaceData BaseInterface
988+
public InterfaceData? BaseInterface
986989
{
987990
get
988991
{

StyleCop.Analyzers/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
<PropertyGroup>
55
<TargetFrameworks>netstandard2.0</TargetFrameworks>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<PropertyGroup>

StyleCop.Analyzers/StyleCop.Analyzers.CodeGeneration/SyntaxLightupGenerator.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ private void GenerateSyntaxWrappers(in GeneratorExecutionContext context, Syntax
4848
{
4949
foreach (var node in syntaxData.Nodes)
5050
{
51-
if (node.WrapperName is not null)
52-
{
53-
this.GenerateSyntaxWrapper(in context, syntaxData, node);
54-
}
51+
this.GenerateSyntaxWrapper(in context, syntaxData, node);
5552
}
5653
}
5754

5855
private void GenerateSyntaxWrapper(in GeneratorExecutionContext context, SyntaxData syntaxData, NodeData nodeData)
5956
{
57+
if (nodeData.WrapperName is null)
58+
{
59+
// No need to generate a wrapper for this type
60+
return;
61+
}
62+
6063
var concreteBase = syntaxData.TryGetConcreteBase(nodeData)?.Name ?? nameof(SyntaxNode);
6164

6265
var members = SyntaxFactory.List<MemberDeclarationSyntax>();
@@ -163,7 +166,8 @@ private void GenerateSyntaxWrapper(in GeneratorExecutionContext context, SyntaxD
163166
SyntaxKind.SimpleMemberAccessExpression,
164167
expression: SyntaxFactory.ThisExpression(),
165168
name: SyntaxFactory.IdentifierName("node")),
166-
right: SyntaxFactory.IdentifierName("node"))))));
169+
right: SyntaxFactory.IdentifierName("node")))),
170+
expressionBody: null));
167171

168172
// public SyntaxNode SyntaxNode => this.node;
169173
members = members.Add(SyntaxFactory.PropertyDeclaration(
@@ -553,7 +557,8 @@ private void GenerateSyntaxWrapperHelper(in GeneratorExecutionContext context, I
553557
identifier: SyntaxFactory.Identifier("SyntaxWrapperHelper"),
554558
parameterList: SyntaxFactory.ParameterList(),
555559
initializer: null,
556-
body: SyntaxFactory.Block(staticCtorStatements));
560+
body: SyntaxFactory.Block(staticCtorStatements),
561+
expressionBody: null);
557562

558563
// internal static Type GetWrappedType(Type wrapperType)
559564
// {
@@ -655,7 +660,7 @@ public SyntaxData(in GeneratorExecutionContext context, XDocument document)
655660

656661
public ImmutableArray<NodeData> Nodes { get; }
657662

658-
public NodeData TryGetConcreteType(NodeData node)
663+
public NodeData? TryGetConcreteType(NodeData? node)
659664
{
660665
for (var current = node; current is not null; current = this.TryGetNode(current.BaseName))
661666
{
@@ -669,12 +674,12 @@ public NodeData TryGetConcreteType(NodeData node)
669674
return null;
670675
}
671676

672-
public NodeData TryGetConcreteBase(NodeData node)
677+
public NodeData? TryGetConcreteBase(NodeData node)
673678
{
674679
return this.TryGetConcreteType(this.TryGetNode(node.BaseName));
675680
}
676681

677-
public NodeData TryGetNode(string name)
682+
public NodeData? TryGetNode(string name)
678683
{
679684
this.nameToNode.TryGetValue(name, out var node);
680685
return node;
@@ -715,7 +720,7 @@ public NodeData(in GeneratorExecutionContext context, XElement element)
715720

716721
public string Name { get; }
717722

718-
public string WrapperName { get; }
723+
public string? WrapperName { get; }
719724

720725
public string BaseName { get; }
721726

0 commit comments

Comments
 (0)