-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathPolyfillsGenerator.cs
More file actions
54 lines (45 loc) · 2.08 KB
/
PolyfillsGenerator.cs
File metadata and controls
54 lines (45 loc) · 2.08 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
using System.Linq;
using Microsoft.CodeAnalysis;
using PolySharp.SourceGenerators.Models;
namespace PolySharp.SourceGenerators;
/// <summary>
/// A source generator injecting all needed C# polyfills at compile time.
/// </summary>
[Generator(LanguageNames.CSharp)]
public sealed partial class PolyfillsGenerator : IIncrementalGenerator
{
/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.SetupDebugging();
// Prepare all the generation options in a single incremental model
IncrementalValueProvider<GenerationOptions> generationOptions =
context.AnalyzerConfigOptionsProvider
.Select(GetGenerationOptions);
// Get the sequence of all available types that could be generated
IncrementalValuesProvider<AvailableType> availableTypes =
context.CompilationProvider
.Combine(generationOptions)
.SelectMany(static (pair, token) => GetAvailableTypes(pair.Left, pair.Right, token));
// Gather the sequence of all types to generate after filtering
IncrementalValuesProvider<GeneratedType> generatedTypes =
availableTypes
.Combine(generationOptions)
.Where(IsAvailableTypeSelected)
.Select(GetGeneratedType);
// Generate source for the current type depending on current accessibility
context.RegisterSourceOutput(generatedTypes, EmitGeneratedType);
// Get all potential type forwarded type names
IncrementalValuesProvider<string> typeForwardNames =
context.CompilationProvider
.SelectMany(GetCoreLibTypes);
// Filter the type forward names with the current generation options
IncrementalValuesProvider<string> filteredTypeForwardNames =
typeForwardNames
.Combine(generationOptions)
.Where(IsCoreLibTypeSelected)
.Select(GetCoreLibType);
// Generate the type forwards, if any
context.RegisterImplementationSourceOutput(filteredTypeForwardNames, EmitTypeForwards);
}
}