-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptifyGenerator.cs
More file actions
37 lines (33 loc) · 1.5 KB
/
Copy pathOptifyGenerator.cs
File metadata and controls
37 lines (33 loc) · 1.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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Optify;
[Generator]
public class OptifyGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var provider = context.SyntaxProvider.ForAttributeWithMetadataName(
"Optify.OptifyOptionsAttribute",
static (node, _) => node is ClassDeclarationSyntax or RecordDeclarationSyntax,
static (ctx, _) =>
{
// Register a null object value that we'll filter for later.
if (ctx.TargetSymbol is not INamedTypeSymbol target)
return new OptionsTypeToRegister();
// At this point, ctx.Attributes *only* contains OptifyOptionsAttribute, even
// when other attributes are on the type. A user should receive a compiler error
// CS0579 if they added more than one [OptifyOptions]. However, generated source
// may be able to bypass that. In such a case, we're just going to assume
// that we'll take the first one.
return new OptionsTypeToRegister(ctx.Attributes[0], target);
}
);
context.RegisterSourceOutput(
provider.Where(static opt => opt.IsValid).Collect(),
static (spc, types) =>
{
spc.AddSource(OptifyRegistrationSource.Filename, OptifyRegistrationSource.GenerateSource(types));
}
);
}
}