-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathAddStubGenerator.cs
More file actions
212 lines (182 loc) · 7.39 KB
/
AddStubGenerator.cs
File metadata and controls
212 lines (182 loc) · 7.39 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp; // For GetInterceptableLocation API and InterceptableLocation type
namespace Bunit.Web.Stubs.AddStubMethodStubGenerator;
/// <summary>
/// Generator that creates a stub that mimics the public surface of a Component.
/// </summary>
[Generator]
public class AddStubGenerator : IIncrementalGenerator
{
/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var classesToStub = context.SyntaxProvider
.CreateSyntaxProvider(
predicate: static (s, _) => s is InvocationExpressionSyntax
{
Expression: MemberAccessExpressionSyntax { Name.Identifier.Text: "AddStub" }
},
transform: static (ctx, _) => GetStubClassInfo(ctx))
.Where(static m => m is not null)
.Collect();
context.RegisterSourceOutput(
classesToStub,
static (spc, source) => Execute(source!, spc));
}
private static AddStubClassInfo? GetStubClassInfo(GeneratorSyntaxContext context)
{
if (context.Node is not InvocationExpressionSyntax invocation || !IsComponentFactoryStubMethod(invocation, context.SemanticModel))
{
return null;
}
if (invocation.Expression is not MemberAccessExpressionSyntax
{
Name: GenericNameSyntax { TypeArgumentList.Arguments.Count: 1 } genericName
})
{
return null;
}
var typeArgument = genericName.TypeArgumentList.Arguments[0];
if (context.SemanticModel.GetSymbolInfo(typeArgument).Symbol is not ITypeSymbol symbol)
{
return null;
}
var interceptableLocation = context.SemanticModel.GetInterceptableLocation(invocation);
if (interceptableLocation is null)
{
return null;
}
var properties = symbol.GetAllMembersRecursively()
.OfType<IPropertySymbol>()
.Where(IsParameterOrCascadingParameter)
.Select(CreateFromProperty)
.ToImmutableArray();
return new AddStubClassInfo
{
StubClassName = $"{symbol.Name}Stub",
TargetTypeNamespace = symbol.ContainingNamespace.ToDisplayString(),
TargetTypeName = symbol.ToDisplayString(),
Properties = properties,
Location = interceptableLocation,
};
static bool IsComponentFactoryStubMethod(InvocationExpressionSyntax invocation, SemanticModel semanticModel)
{
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
{
return false;
}
if (memberAccess.Name.Identifier.Text != "AddStub" ||
invocation.ArgumentList.Arguments.Count != 0)
{
return false;
}
return semanticModel.GetSymbolInfo(invocation).Symbol is IMethodSymbol { IsExtensionMethod: true, ReceiverType.Name: "ComponentFactoryCollection" };
}
static bool IsParameterOrCascadingParameter(ISymbol member)
{
return member.GetAttributes().Any(SupportedAttributes.IsSupportedAttribute);
}
static StubPropertyInfo CreateFromProperty(IPropertySymbol member)
{
return new StubPropertyInfo
{
Name = member.Name,
Type = member.Type.ToDisplayString(),
AttributeLine = AttributeLineGenerator.GetAttributeLine(member),
};
}
}
private static void Execute(ImmutableArray<AddStubClassInfo> classInfos, SourceProductionContext context)
{
foreach (var stubClassGrouped in classInfos.GroupBy(c => c.UniqueQualifier))
{
var stubbedComponentGroup = stubClassGrouped.First();
var didStubComponent = GenerateStubComponent(stubbedComponentGroup, context);
if (didStubComponent)
{
GenerateInterceptorCode(stubbedComponentGroup, stubClassGrouped, context);
}
}
}
private static void GenerateInterceptorCode(AddStubClassInfo stubbedComponentGroup, IEnumerable<AddStubClassInfo> stubClassGrouped, SourceProductionContext context)
{
// Generate the interceptor
var interceptorSource = new StringBuilder(1000);
interceptorSource.AppendLine(HeaderProvider.Header);
interceptorSource.AppendLine("namespace System.Runtime.CompilerServices");
interceptorSource.AppendLine("{");
interceptorSource.AppendLine("\t[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true)]");
interceptorSource.AppendLine("\tsealed file class InterceptsLocationAttribute : global::System.Attribute");
interceptorSource.AppendLine("\t{");
interceptorSource.AppendLine("\t\tpublic InterceptsLocationAttribute(int version, string data)");
interceptorSource.AppendLine("\t\t{");
interceptorSource.AppendLine("\t\t\t_ = version;");
interceptorSource.AppendLine("\t\t\t_ = data;");
interceptorSource.AppendLine("\t\t}");
interceptorSource.AppendLine("\t}");
interceptorSource.AppendLine("}");
interceptorSource.AppendLine();
interceptorSource.AppendLine("namespace Bunit");
interceptorSource.AppendLine("{");
interceptorSource.AppendLine($"\tinternal static class Interceptor{stubbedComponentGroup.StubClassName}");
interceptorSource.AppendLine("\t{");
foreach (var hit in stubClassGrouped)
{
interceptorSource.AppendLine($"\t\t{hit.Location.GetInterceptsLocationAttributeSyntax()}");
}
interceptorSource.AppendLine(
"\t\tpublic static global::Bunit.ComponentFactoryCollection AddStubInterceptor<TComponent>(this global::Bunit.ComponentFactoryCollection factories)");
interceptorSource.AppendLine("\t\t\twhere TComponent : global::Microsoft.AspNetCore.Components.IComponent");
interceptorSource.AppendLine("\t\t{");
interceptorSource.AppendLine(
$"\t\t\treturn factories.Add<global::{stubbedComponentGroup.TargetTypeName}, global::{stubbedComponentGroup.TargetTypeNamespace}.{stubbedComponentGroup.StubClassName}>();");
interceptorSource.AppendLine("\t\t}");
interceptorSource.AppendLine("\t}");
interceptorSource.AppendLine("}");
context.AddSource($"Interceptor{stubbedComponentGroup.StubClassName}.g.cs", interceptorSource.ToString());
}
private static bool GenerateStubComponent(AddStubClassInfo classInfo, SourceProductionContext context)
{
var hasSomethingToStub = false;
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");
sourceBuilder.Append('{');
foreach (var member in classInfo.Properties)
{
sourceBuilder.AppendLine();
hasSomethingToStub = true;
var propertyType = member.Type;
var propertyName = member.Name;
var attributeLine = member.AttributeLine;
sourceBuilder.AppendLine(attributeLine);
sourceBuilder.AppendLine($"\tpublic {propertyType} {propertyName} {{ get; set; }} = default!;");
}
sourceBuilder.AppendLine("}");
if (hasSomethingToStub)
{
context.AddSource($"{classInfo.StubClassName}.g.cs", sourceBuilder.ToString());
}
return hasSomethingToStub;
}
}
internal sealed record AddStubClassInfo
{
public required string StubClassName { get; set; }
public required string TargetTypeNamespace { get; set; }
public required string TargetTypeName { get; set; }
public string UniqueQualifier => $"{TargetTypeNamespace}.{StubClassName}";
public ImmutableArray<StubPropertyInfo> Properties { get; set; } = ImmutableArray<StubPropertyInfo>.Empty;
public required InterceptableLocation Location { get; set; }
}
internal sealed record StubPropertyInfo
{
public required string Name { get; set; }
public required string Type { get; set; }
public required string AttributeLine { get; set; }
}