Skip to content

Commit 80f3eaf

Browse files
committed
Generate property accessors
1 parent fa7ed32 commit 80f3eaf

76 files changed

Lines changed: 370 additions & 245 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,59 @@ private void GenerateSyntaxWrapper(in GeneratorExecutionContext context, SyntaxD
8888
type: SyntaxFactory.IdentifierName(concreteBase),
8989
variables: SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator("node")))));
9090

91+
foreach (var field in nodeData.Fields)
92+
{
93+
if (field.IsSkipped)
94+
{
95+
continue;
96+
}
97+
98+
if (field.IsOverride)
99+
{
100+
// The 'get' accessor is skipped for override fields
101+
continue;
102+
}
103+
104+
// private static readonly Func<CSharpSyntaxNode, T> FieldAccessor;
105+
members = members.Add(SyntaxFactory.FieldDeclaration(
106+
attributeLists: default,
107+
modifiers: SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)),
108+
declaration: SyntaxFactory.VariableDeclaration(
109+
type: SyntaxFactory.GenericName(
110+
identifier: SyntaxFactory.Identifier("Func"),
111+
typeArgumentList: SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(
112+
new[]
113+
{
114+
SyntaxFactory.IdentifierName(concreteBase),
115+
SyntaxFactory.ParseTypeName(field.GetAccessorResultType(syntaxData)),
116+
}))),
117+
variables: SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(field.AccessorName)))));
118+
}
119+
120+
foreach (var field in nodeData.Fields)
121+
{
122+
if (field.IsSkipped)
123+
{
124+
continue;
125+
}
126+
127+
// private static readonly Func<CSharpSyntaxNode, T, CSharpSyntaxNode> WithFieldAccessor;
128+
members = members.Add(SyntaxFactory.FieldDeclaration(
129+
attributeLists: default,
130+
modifiers: SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword), SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword)),
131+
declaration: SyntaxFactory.VariableDeclaration(
132+
type: SyntaxFactory.GenericName(
133+
identifier: SyntaxFactory.Identifier("Func"),
134+
typeArgumentList: SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList(
135+
new[]
136+
{
137+
SyntaxFactory.IdentifierName(concreteBase),
138+
SyntaxFactory.ParseTypeName(field.GetAccessorResultType(syntaxData)),
139+
SyntaxFactory.IdentifierName(concreteBase),
140+
}))),
141+
variables: SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(field.WithAccessorName)))));
142+
}
143+
91144
// private SyntaxNodeWrapper(SyntaxNode node)
92145
// {
93146
// this.node = node;
@@ -602,9 +655,9 @@ public SyntaxData(in GeneratorExecutionContext context, XDocument document)
602655

603656
public ImmutableArray<NodeData> Nodes { get; }
604657

605-
public NodeData TryGetConcreteBase(NodeData node)
658+
public NodeData TryGetConcreteType(NodeData node)
606659
{
607-
for (var current = this.TryGetNode(node.BaseName); current is not null; current = this.TryGetNode(current.BaseName))
660+
for (var current = node; current is not null; current = this.TryGetNode(current.BaseName))
608661
{
609662
if (current.WrapperName is null)
610663
{
@@ -616,7 +669,12 @@ public NodeData TryGetConcreteBase(NodeData node)
616669
return null;
617670
}
618671

619-
private NodeData TryGetNode(string name)
672+
public NodeData TryGetConcreteBase(NodeData node)
673+
{
674+
return this.TryGetConcreteType(this.TryGetNode(node.BaseName));
675+
}
676+
677+
public NodeData TryGetNode(string name)
620678
{
621679
this.nameToNode.TryGetValue(name, out var node);
622680
return node;
@@ -650,7 +708,7 @@ public NodeData(in GeneratorExecutionContext context, XElement element)
650708
}
651709

652710
this.BaseName = element.Attribute("Base").Value;
653-
this.Fields = element.XPathSelectElements("Field").Select(field => new FieldData(field)).ToImmutableArray();
711+
this.Fields = element.XPathSelectElements("descendant::Field").Select(field => new FieldData(field)).ToImmutableArray();
654712
}
655713

656714
public NodeKind Kind { get; }
@@ -669,15 +727,52 @@ private sealed class FieldData
669727
public FieldData(XElement element)
670728
{
671729
this.Name = element.Attribute("Name").Value;
672-
this.Type = element.Attribute("Type").Value;
730+
731+
var type = element.Attribute("Type").Value;
732+
this.Type = type switch
733+
{
734+
"SyntaxList<SyntaxToken>" => nameof(SyntaxTokenList),
735+
_ => type,
736+
};
737+
673738
this.IsOverride = element.Attribute("Override")?.Value == "true";
739+
740+
this.AccessorName = this.Name + "Accessor";
741+
this.WithAccessorName = "With" + this.Name + "Accessor";
674742
}
675743

744+
public bool IsSkipped => false;
745+
676746
public string Name { get; }
677747

748+
public string AccessorName { get; }
749+
750+
public string WithAccessorName { get; }
751+
678752
public string Type { get; }
679753

680754
public bool IsOverride { get; }
755+
756+
public string GetAccessorResultType(SyntaxData syntaxData)
757+
{
758+
var typeNode = syntaxData.TryGetNode(this.Type);
759+
if (typeNode is not null)
760+
{
761+
return syntaxData.TryGetConcreteType(typeNode)?.Name ?? nameof(SyntaxNode);
762+
}
763+
764+
if (this.Type.StartsWith("SeparatedSyntaxList<") && this.Type.EndsWith(">"))
765+
{
766+
var elementTypeName = this.Type.Substring("SeparatedSyntaxList<".Length, this.Type.Length - "SeparatedSyntaxList<".Length - ">".Length);
767+
var elementTypeNode = syntaxData.TryGetNode(elementTypeName);
768+
if (elementTypeNode is { WrapperName: not null })
769+
{
770+
return $"SeparatedSyntaxListWrapper<{elementTypeNode.WrapperName}>";
771+
}
772+
}
773+
774+
return this.Type;
775+
}
681776
}
682777
}
683778
}

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/BaseObjectCreationExpressionSyntaxWrapper.g.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.BaseObjectCreationExpressionSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly ExpressionSyntax node;
17+
private static readonly Func<ExpressionSyntax, SyntaxToken> NewKeywordAccessor;
18+
private static readonly Func<ExpressionSyntax, ArgumentListSyntax> ArgumentListAccessor;
19+
private static readonly Func<ExpressionSyntax, InitializerExpressionSyntax> InitializerAccessor;
20+
private static readonly Func<ExpressionSyntax, SyntaxToken, ExpressionSyntax> WithNewKeywordAccessor;
21+
private static readonly Func<ExpressionSyntax, ArgumentListSyntax, ExpressionSyntax> WithArgumentListAccessor;
22+
private static readonly Func<ExpressionSyntax, InitializerExpressionSyntax, ExpressionSyntax> WithInitializerAccessor;
1723
private BaseObjectCreationExpressionSyntaxWrapper(ExpressionSyntax node)
1824
{
1925
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/BaseParameterSyntaxWrapper.g.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.BaseParameterSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly CSharpSyntaxNode node;
17+
private static readonly Func<CSharpSyntaxNode, SyntaxList<AttributeListSyntax>> AttributeListsAccessor;
18+
private static readonly Func<CSharpSyntaxNode, SyntaxTokenList> ModifiersAccessor;
19+
private static readonly Func<CSharpSyntaxNode, TypeSyntax> TypeAccessor;
20+
private static readonly Func<CSharpSyntaxNode, SyntaxList<AttributeListSyntax>, CSharpSyntaxNode> WithAttributeListsAccessor;
21+
private static readonly Func<CSharpSyntaxNode, SyntaxTokenList, CSharpSyntaxNode> WithModifiersAccessor;
22+
private static readonly Func<CSharpSyntaxNode, TypeSyntax, CSharpSyntaxNode> WithTypeAccessor;
1723
private BaseParameterSyntaxWrapper(CSharpSyntaxNode node)
1824
{
1925
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/BinaryPatternSyntaxWrapper.g.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.BinaryPatternSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly CSharpSyntaxNode node;
17+
private static readonly Func<CSharpSyntaxNode, CSharpSyntaxNode> LeftAccessor;
18+
private static readonly Func<CSharpSyntaxNode, SyntaxToken> OperatorTokenAccessor;
19+
private static readonly Func<CSharpSyntaxNode, CSharpSyntaxNode> RightAccessor;
20+
private static readonly Func<CSharpSyntaxNode, CSharpSyntaxNode, CSharpSyntaxNode> WithLeftAccessor;
21+
private static readonly Func<CSharpSyntaxNode, SyntaxToken, CSharpSyntaxNode> WithOperatorTokenAccessor;
22+
private static readonly Func<CSharpSyntaxNode, CSharpSyntaxNode, CSharpSyntaxNode> WithRightAccessor;
1723
private BinaryPatternSyntaxWrapper(CSharpSyntaxNode node)
1824
{
1925
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/CasePatternSwitchLabelSyntaxWrapper.g.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.CasePatternSwitchLabelSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly SwitchLabelSyntax node;
17+
private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode> PatternAccessor;
18+
private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode> WhenClauseAccessor;
19+
private static readonly Func<SwitchLabelSyntax, SyntaxToken, SwitchLabelSyntax> WithKeywordAccessor;
20+
private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode, SwitchLabelSyntax> WithPatternAccessor;
21+
private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode, SwitchLabelSyntax> WithWhenClauseAccessor;
22+
private static readonly Func<SwitchLabelSyntax, SyntaxToken, SwitchLabelSyntax> WithColonTokenAccessor;
1723
private CasePatternSwitchLabelSyntaxWrapper(SwitchLabelSyntax node)
1824
{
1925
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/CommonForEachStatementSyntaxWrapper.g.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.CommonForEachStatementSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly StatementSyntax node;
17+
private static readonly Func<StatementSyntax, SyntaxToken> AwaitKeywordAccessor;
18+
private static readonly Func<StatementSyntax, SyntaxToken> ForEachKeywordAccessor;
19+
private static readonly Func<StatementSyntax, SyntaxToken> OpenParenTokenAccessor;
20+
private static readonly Func<StatementSyntax, SyntaxToken> InKeywordAccessor;
21+
private static readonly Func<StatementSyntax, ExpressionSyntax> ExpressionAccessor;
22+
private static readonly Func<StatementSyntax, SyntaxToken> CloseParenTokenAccessor;
23+
private static readonly Func<StatementSyntax, StatementSyntax> StatementAccessor;
24+
private static readonly Func<StatementSyntax, SyntaxToken, StatementSyntax> WithAwaitKeywordAccessor;
25+
private static readonly Func<StatementSyntax, SyntaxToken, StatementSyntax> WithForEachKeywordAccessor;
26+
private static readonly Func<StatementSyntax, SyntaxToken, StatementSyntax> WithOpenParenTokenAccessor;
27+
private static readonly Func<StatementSyntax, SyntaxToken, StatementSyntax> WithInKeywordAccessor;
28+
private static readonly Func<StatementSyntax, ExpressionSyntax, StatementSyntax> WithExpressionAccessor;
29+
private static readonly Func<StatementSyntax, SyntaxToken, StatementSyntax> WithCloseParenTokenAccessor;
30+
private static readonly Func<StatementSyntax, StatementSyntax, StatementSyntax> WithStatementAccessor;
1731
private CommonForEachStatementSyntaxWrapper(StatementSyntax node)
1832
{
1933
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/ConstantPatternSyntaxWrapper.g.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.ConstantPatternSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly CSharpSyntaxNode node;
17+
private static readonly Func<CSharpSyntaxNode, ExpressionSyntax> ExpressionAccessor;
18+
private static readonly Func<CSharpSyntaxNode, ExpressionSyntax, CSharpSyntaxNode> WithExpressionAccessor;
1719
private ConstantPatternSyntaxWrapper(CSharpSyntaxNode node)
1820
{
1921
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/DeclarationExpressionSyntaxWrapper.g.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.DeclarationExpressionSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly ExpressionSyntax node;
17+
private static readonly Func<ExpressionSyntax, TypeSyntax> TypeAccessor;
18+
private static readonly Func<ExpressionSyntax, CSharpSyntaxNode> DesignationAccessor;
19+
private static readonly Func<ExpressionSyntax, TypeSyntax, ExpressionSyntax> WithTypeAccessor;
20+
private static readonly Func<ExpressionSyntax, CSharpSyntaxNode, ExpressionSyntax> WithDesignationAccessor;
1721
private DeclarationExpressionSyntaxWrapper(ExpressionSyntax node)
1822
{
1923
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/DeclarationPatternSyntaxWrapper.g.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.DeclarationPatternSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly CSharpSyntaxNode node;
17+
private static readonly Func<CSharpSyntaxNode, TypeSyntax> TypeAccessor;
18+
private static readonly Func<CSharpSyntaxNode, CSharpSyntaxNode> DesignationAccessor;
19+
private static readonly Func<CSharpSyntaxNode, TypeSyntax, CSharpSyntaxNode> WithTypeAccessor;
20+
private static readonly Func<CSharpSyntaxNode, CSharpSyntaxNode, CSharpSyntaxNode> WithDesignationAccessor;
1721
private DeclarationPatternSyntaxWrapper(CSharpSyntaxNode node)
1822
{
1923
this.node = node;

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/.generated/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.SyntaxLightupGenerator/DefaultConstraintSyntaxWrapper.g.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace StyleCop.Analyzers.Lightup
1414
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.DefaultConstraintSyntax";
1515
private static readonly Type WrappedType;
1616
private readonly TypeParameterConstraintSyntax node;
17+
private static readonly Func<TypeParameterConstraintSyntax, SyntaxToken> DefaultKeywordAccessor;
18+
private static readonly Func<TypeParameterConstraintSyntax, SyntaxToken, TypeParameterConstraintSyntax> WithDefaultKeywordAccessor;
1719
private DefaultConstraintSyntaxWrapper(TypeParameterConstraintSyntax node)
1820
{
1921
this.node = node;

0 commit comments

Comments
 (0)