Skip to content

Commit 3252dca

Browse files
authored
Merge pull request #130 from BoBoBaSs84/refactoring/generator-attribute-name-deduplication
refactor: attribute name handling in source generators
2 parents 95f1e0b + 2ceef44 commit 3252dca

5 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/BB84.SourceGenerators/DisposableGenerator.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ namespace BB84.SourceGenerators;
2323
[Generator(LanguageNames.CSharp)]
2424
public sealed class DisposableGenerator : IIncrementalGenerator
2525
{
26-
private static readonly string GeneratorAttributeName = typeof(GenerateDisposableAttribute).FullName;
27-
private const string AttributeFullName = nameof(GenerateDisposableAttribute);
28-
private static readonly string AttributeShortName = GeneratorHelpers.StripAttributeSuffix(AttributeFullName);
29-
private const string DisposeResourceFullName = nameof(DisposeResourceAttribute);
30-
private static readonly string DisposeResourceShortName = GeneratorHelpers.StripAttributeSuffix(DisposeResourceFullName);
26+
private static readonly (string MetadataName, string FullName, string ShortName) AttributeNames =
27+
GeneratorHelpers.GetAttributeNames<GenerateDisposableAttribute>();
28+
private static readonly (string MetadataName, string FullName, string ShortName) DisposeResourceAttributeNames =
29+
GeneratorHelpers.GetAttributeNames<DisposeResourceAttribute>();
3130

3231
/// <inheritdoc/>
3332
public void Initialize(IncrementalGeneratorInitializationContext context)
34-
=> GeneratorHelpers.RegisterClassGenerator(context, GeneratorAttributeName, Execute);
33+
=> GeneratorHelpers.RegisterClassGenerator(context, AttributeNames.MetadataName, Execute);
3534

3635
private void Execute(SourceProductionContext context, (ClassDeclarationSyntax ClassSyntax, SemanticModel SemanticModel)? input)
3736
{
@@ -109,7 +108,7 @@ private static (bool generateFinalizer, bool generateAsync) GetAttributeOptions(
109108
{
110109
string name = attribute.Name.ToString();
111110

112-
if (name != AttributeShortName && name != AttributeFullName)
111+
if (name != AttributeNames.ShortName && name != AttributeNames.FullName)
113112
continue;
114113

115114
if (attribute.ArgumentList is null || attribute.ArgumentList.Arguments.Count == 0)
@@ -195,7 +194,7 @@ private static int GetDisposeResourceOrder(FieldDeclarationSyntax fieldDeclarati
195194
{
196195
string name = attribute.Name.ToString();
197196

198-
if (name != DisposeResourceShortName && name != DisposeResourceFullName)
197+
if (name != DisposeResourceAttributeNames.ShortName && name != DisposeResourceAttributeNames.FullName)
199198
continue;
200199

201200
if (attribute.ArgumentList is null || attribute.ArgumentList.Arguments.Count == 0)

src/BB84.SourceGenerators/Helpers/GeneratorHelpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ internal static string StripAttributeSuffix(string attributeName)
146146
? attributeName[..^AttributeKeyword.Length]
147147
: attributeName;
148148

149+
/// <summary>
150+
/// Returns the fully-qualified metadata name, the simple full name, and the short name
151+
/// (without the "Attribute" suffix) for the given attribute type.
152+
/// </summary>
153+
/// <typeparam name="TAttribute">The attribute type.</typeparam>
154+
/// <returns>
155+
/// A tuple of <c>(MetadataName, FullName, ShortName)</c> where <c>MetadataName</c> is the
156+
/// value of <see cref="Type.FullName"/>, <c>FullName</c> is the simple type name including
157+
/// the "Attribute" suffix, and <c>ShortName</c> is the type name without the suffix.
158+
/// </returns>
159+
internal static (string MetadataName, string FullName, string ShortName) GetAttributeNames<TAttribute>()
160+
where TAttribute : Attribute
161+
{
162+
string fullName = typeof(TAttribute).Name;
163+
return (typeof(TAttribute).FullName, fullName, StripAttributeSuffix(fullName));
164+
}
165+
149166
/// <summary>
150167
/// Escapes a string for use in a regular string literal.
151168
/// </summary>

src/BB84.SourceGenerators/IniFileGenerator.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ namespace BB84.SourceGenerators;
2424
[Generator(LanguageNames.CSharp)]
2525
public sealed class IniFileGenerator : IIncrementalGenerator
2626
{
27-
private static readonly string GeneratorAttributeName = typeof(GenerateIniFileAttribute).FullName;
27+
private static readonly (string MetadataName, string FullName, string ShortName) AttributeNames =
28+
GeneratorHelpers.GetAttributeNames<GenerateIniFileAttribute>();
2829
private static readonly string SectionAttributeName = typeof(GenerateIniFileSectionAttribute).FullName;
2930
private static readonly string ValueAttributeName = typeof(GenerateIniFileValueAttribute).FullName;
30-
private const string AttributeFullName = nameof(GenerateIniFileAttribute);
31-
private static readonly string AttributeShortName = GeneratorHelpers.StripAttributeSuffix(AttributeFullName);
3231
private static readonly string[] LineBreakSeparators = ["\r\n", "\n"];
3332

3433
/// <inheritdoc/>
3534
public void Initialize(IncrementalGeneratorInitializationContext context)
36-
=> GeneratorHelpers.RegisterClassGenerator(context, GeneratorAttributeName, Execute);
35+
=> GeneratorHelpers.RegisterClassGenerator(context, AttributeNames.MetadataName, Execute);
3736

3837
private void Execute(SourceProductionContext context, (ClassDeclarationSyntax ClassSyntax, SemanticModel SemanticModel)? input)
3938
{
@@ -644,7 +643,7 @@ private static bool GetSerializeComments(ClassDeclarationSyntax classDeclaration
644643
{
645644
string name = attribute.Name.ToString();
646645

647-
if (name != AttributeShortName && name != AttributeFullName)
646+
if (name != AttributeNames.ShortName && name != AttributeNames.FullName)
648647
continue;
649648

650649
if (attribute.ArgumentList is null)
@@ -817,7 +816,7 @@ private static string GetNonNullableFullyQualifiedName(INamedTypeSymbol type)
817816
{
818817
foreach (AttributeData attr in classSymbol.GetAttributes())
819818
{
820-
if (attr.AttributeClass?.ToDisplayString() == GeneratorAttributeName)
819+
if (attr.AttributeClass?.ToDisplayString() == AttributeNames.MetadataName)
821820
return attr;
822821
}
823822

src/BB84.SourceGenerators/NotificationsGenerator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ namespace BB84.SourceGenerators;
2020
[Generator(LanguageNames.CSharp)]
2121
public sealed class NotificationsGenerator : IIncrementalGenerator
2222
{
23-
private static readonly string GeneratorAttributeName = typeof(GenerateNotificationsAttribute).FullName;
24-
private const string AttributeFullName = nameof(GenerateNotificationsAttribute);
25-
private static readonly string AttributeShortName = GeneratorHelpers.StripAttributeSuffix(AttributeFullName);
23+
private static readonly (string MetadataName, string FullName, string ShortName) AttributeNames =
24+
GeneratorHelpers.GetAttributeNames<GenerateNotificationsAttribute>();
2625

2726
/// <inheritdoc/>
2827
public void Initialize(IncrementalGeneratorInitializationContext context)
29-
=> GeneratorHelpers.RegisterClassGenerator(context, GeneratorAttributeName, Execute);
28+
=> GeneratorHelpers.RegisterClassGenerator(context, AttributeNames.MetadataName, Execute);
3029

3130
private void Execute(SourceProductionContext context, (ClassDeclarationSyntax ClassSyntax, SemanticModel SemanticModel)? input)
3231
{
@@ -82,7 +81,7 @@ private static (bool propertyChanged, bool propertyChanging, bool hasChanged) Ge
8281
{
8382
string name = attribute.Name.ToString();
8483

85-
if (name != AttributeShortName && name != AttributeFullName)
84+
if (name != AttributeNames.ShortName && name != AttributeNames.FullName)
8685
continue;
8786

8887
if (attribute.ArgumentList is null || attribute.ArgumentList.Arguments.Count == 0)

src/BB84.SourceGenerators/SingletonGenerator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ namespace BB84.SourceGenerators;
2020
[Generator(LanguageNames.CSharp)]
2121
public sealed class SingletonGenerator : IIncrementalGenerator
2222
{
23-
private const string AttributeFullName = nameof(GenerateSingletonAttribute);
24-
private static readonly string AttributeShortName = GeneratorHelpers.StripAttributeSuffix(AttributeFullName);
25-
private static readonly string GeneratorAttributeName = typeof(GenerateSingletonAttribute).FullName;
23+
private static readonly (string MetadataName, string FullName, string ShortName) AttributeNames =
24+
GeneratorHelpers.GetAttributeNames<GenerateSingletonAttribute>();
2625

2726
/// <inheritdoc/>
2827
public void Initialize(IncrementalGeneratorInitializationContext context)
29-
=> GeneratorHelpers.RegisterClassGenerator(context, GeneratorAttributeName, Execute);
28+
=> GeneratorHelpers.RegisterClassGenerator(context, AttributeNames.MetadataName, Execute);
3029

3130
private void Execute(SourceProductionContext context, (ClassDeclarationSyntax ClassSyntax, SemanticModel SemanticModel)? input)
3231
{
@@ -122,7 +121,7 @@ private static bool GetUseLazy(ClassDeclarationSyntax classDeclaration)
122121
{
123122
string name = attribute.Name.ToString();
124123

125-
if (name != AttributeShortName && name != AttributeFullName)
124+
if (name != AttributeNames.ShortName && name != AttributeNames.FullName)
126125
continue;
127126

128127
if (attribute.ArgumentList is null || attribute.ArgumentList.Arguments.Count == 0)

0 commit comments

Comments
 (0)