From c0e0430be68e77326d16a210de8c14dfec6bb064 Mon Sep 17 00:00:00 2001 From: Domn Werner Date: Wed, 6 Jul 2022 20:47:33 -0700 Subject: [PATCH] Support multiple records with same name (#38) * Remove unused union record generator code. * Support multiple unions with the same name. * Clarify union attribute constant names. * Union attribute source naming improvements. --- .../UnionAttributeGenerator.cs | 4 ++-- .../UnionAttributeSource.cs | 8 ++++---- .../UnionRecordGenerator.cs | 20 ++++++++----------- src/IsExternalInit.cs | 2 +- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/GenerateUnionAttribute/UnionAttributeGenerator.cs b/src/GenerateUnionAttribute/UnionAttributeGenerator.cs index 3a1e6bf..bc1f5da 100644 --- a/src/GenerateUnionAttribute/UnionAttributeGenerator.cs +++ b/src/GenerateUnionAttribute/UnionAttributeGenerator.cs @@ -12,8 +12,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.RegisterPostInitializationOutput( ctx => ctx.AddSource( - "UnionAttribute.g.cs", - SourceText.From(UnionAttributeSource.Attribute, Encoding.UTF8) + $"{UnionAttributeSource.Name}.g.cs", + SourceText.From(UnionAttributeSource.SourceCode, Encoding.UTF8) ) ); } diff --git a/src/GenerateUnionAttribute/UnionAttributeSource.cs b/src/GenerateUnionAttribute/UnionAttributeSource.cs index 43d0e7f..bc725cb 100644 --- a/src/GenerateUnionAttribute/UnionAttributeSource.cs +++ b/src/GenerateUnionAttribute/UnionAttributeSource.cs @@ -2,11 +2,11 @@ internal class UnionAttributeSource { - public const string AttributeNamespace = "Dunet"; - public const string AttributeName = "UnionAttribute"; - public const string FullAttributeName = $"{AttributeNamespace}.{AttributeName}"; + public const string Namespace = "Dunet"; + public const string Name = "UnionAttribute"; + public const string FullyQualifiedName = $"{Namespace}.{Name}"; - public const string Attribute = + public const string SourceCode = @"using System; namespace Dunet; diff --git a/src/GenerateUnionRecord/UnionRecordGenerator.cs b/src/GenerateUnionRecord/UnionRecordGenerator.cs index 7be56d7..20bb113 100644 --- a/src/GenerateUnionRecord/UnionRecordGenerator.cs +++ b/src/GenerateUnionRecord/UnionRecordGenerator.cs @@ -13,14 +13,14 @@ public class UnionRecordGenerator : IIncrementalGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { - var recordDeclarations = context.SyntaxProvider + var targets = context.SyntaxProvider .CreateSyntaxProvider( predicate: static (node, _) => node.IsDecoratedRecord(), transform: static (ctx, _) => GetGenerationTarget(ctx) ) .Where(static m => m is not null); - var compilation = context.CompilationProvider.Combine(recordDeclarations.Collect()); + var compilation = context.CompilationProvider.Combine(targets.Collect()); context.RegisterSourceOutput( compilation, @@ -47,7 +47,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var fullAttributeName = attributeSymbol.ToDisplayString(); - if (fullAttributeName is UnionAttributeSource.FullAttributeName) + if (fullAttributeName is UnionAttributeSource.FullyQualifiedName) { return recordDeclaration; } @@ -68,23 +68,19 @@ SourceProductionContext context return; } - var distinctRecords = recordDeclarations.Distinct(); - var unionRecords = GetCodeToGenerate( compilation, - distinctRecords, + recordDeclarations, context.CancellationToken ); - if (unionRecords.Count <= 0) - { - return; - } - foreach (var unionRecord in unionRecords) { var result = UnionRecordSource.GenerateRecord(unionRecord); - context.AddSource($"{unionRecord.Name}.g.cs", SourceText.From(result, Encoding.UTF8)); + context.AddSource( + $"{unionRecord.Namespace}.{unionRecord.Name}.g.cs", + SourceText.From(result, Encoding.UTF8) + ); } } diff --git a/src/IsExternalInit.cs b/src/IsExternalInit.cs index 8acae1a..4c5d528 100644 --- a/src/IsExternalInit.cs +++ b/src/IsExternalInit.cs @@ -11,4 +11,4 @@ namespace System.Runtime.CompilerServices; /// Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported /// [EditorBrowsable(EditorBrowsableState.Never)] -sealed class IsExternalInit { } +internal sealed class IsExternalInit { }