Repository navigation
Expand file tree
/
Copy pathIncrementalGeneratorInitializationContextExtensions.cs
More file actions
155 lines (134 loc) · 5.5 KB
/
Copy pathIncrementalGeneratorInitializationContextExtensions.cs
File metadata and controls
155 lines (134 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
namespace CommonSourceGeneratorsHelpers;
internal static class IncrementalGeneratorInitializationContextExtensions
{
public static IncrementalValueProvider<ImmutableArray<TResult>> RunFilteredGenerator<TResult>(
this IncrementalGeneratorInitializationContext context,
string baseClassFullName,
string boolPropertyName,
bool expectedValue,
Func<ImmutableHashSet<ClassDeclarationSyntax>, Compilation, ImmutableHashSet<TResult>> processClasses)
{
return context.RunFilteredGenerator(baseClassFullName, boolPropertyName, expectedValue, false, processClasses); //usually false
}
public static IncrementalValueProvider<ImmutableArray<TResult>> RunFilteredGenerator<TResult>(
this IncrementalGeneratorInitializationContext context,
string baseClassFullName,
string boolPropertyName,
bool expectedValue,
bool defaultBoolValue,
Func<ImmutableHashSet<ClassDeclarationSyntax>, Compilation, ImmutableHashSet<TResult>> processClasses)
{
// 1. Find candidate classes by syntax (quick syntax filter)
IncrementalValuesProvider<ClassDeclarationSyntax> candidates = context.SyntaxProvider.CreateSyntaxProvider(
predicate: (node, _) => IsCandidateSyntax(node, baseClassFullName),
transform: (ctx, _) => ctx.Node as ClassDeclarationSyntax)
.Where(cls => cls != null)!;
// 2. Combine compilation and collected candidates
var combined = context.CompilationProvider.Combine(candidates.Collect());
// 3. Filter classes based on bool property, then run processClasses delegate
var filteredResults = combined.SelectMany((compAndClasses, _) =>
{
Compilation compilation = compAndClasses.Left;
ImmutableArray<ClassDeclarationSyntax> classesArray = compAndClasses.Right;
var classesSet = classesArray.ToImmutableHashSet();
var filteredClasses = FilterByBoolProperty(classesSet, compilation, boolPropertyName, expectedValue, defaultBoolValue);
return processClasses(filteredClasses, compilation);
});
// 4. Collect all results as ImmutableArray<TResult>
return filteredResults.Collect();
}
private static bool IsCandidateSyntax(SyntaxNode node, string baseClassFullName)
{
if (node is ClassDeclarationSyntax cls && cls.BaseList != null)
{
// Simple textual check in base list
return cls.BaseList.ToString().Contains(baseClassFullName);
}
return false;
}
private static ImmutableHashSet<ClassDeclarationSyntax> FilterByBoolProperty(
ImmutableHashSet<ClassDeclarationSyntax> classes,
Compilation compilation,
string boolPropertyName,
bool expectedValue,
bool defaultBoolValue)
{
var builder = ImmutableHashSet.CreateBuilder<ClassDeclarationSyntax>();
foreach (var cls in classes)
{
var model = compilation.GetSemanticModel(cls.SyntaxTree);
if (model.GetDeclaredSymbol(cls) is not INamedTypeSymbol symbol)
{
continue;
}
var boolProp = symbol.GetMembers()
.OfType<IPropertySymbol>()
.FirstOrDefault(p => p.Name == boolPropertyName);
bool propValue;
if (boolProp == null)
{
propValue = defaultBoolValue;
}
else
{
bool? val = TryGetBoolPropertyLiteralValue(cls, boolProp);
propValue = val ?? defaultBoolValue;
}
if (propValue == expectedValue)
{
builder.Add(cls);
}
}
return builder.ToImmutable();
}
private static bool? TryGetBoolPropertyLiteralValue(
ClassDeclarationSyntax classDecl,
IPropertySymbol propSymbol)
{
var propDecl = classDecl.Members
.OfType<PropertyDeclarationSyntax>()
.FirstOrDefault(p => p.Identifier.Text == propSymbol.Name);
if (propDecl?.ExpressionBody != null)
{
var expr = propDecl.ExpressionBody.Expression;
if (expr is LiteralExpressionSyntax literal)
{
if (literal.IsKind(SyntaxKind.TrueLiteralExpression))
{
return true;
}
if (literal.IsKind(SyntaxKind.FalseLiteralExpression))
{
return false;
}
}
}
else if (propDecl?.AccessorList != null)
{
var getter = propDecl.AccessorList.Accessors
.FirstOrDefault(a => a.Kind() == SyntaxKind.GetAccessorDeclaration);
if (getter?.Body != null)
{
var retStmt = getter.Body.Statements
.OfType<ReturnStatementSyntax>()
.FirstOrDefault();
if (retStmt?.Expression is LiteralExpressionSyntax lit)
{
if (lit.IsKind(SyntaxKind.TrueLiteralExpression))
{
return true;
}
if (lit.IsKind(SyntaxKind.FalseLiteralExpression))
{
return false;
}
}
}
}
return null;
}
}