Skip to content

Commit a1bd8a4

Browse files
authored
Merge pull request #39 from ByronMayne/feature/rename-class-attribute
Changed the class attribute for marking generators
2 parents 5cc8fe3 + 08e2d8e commit a1bd8a4

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ Source Generators are awesome but working with them can be a bit painful. This l
99
To get started all you need to do is add the NuGet package reference. You may or may not have to restart Visual Studio for the new types to show up. Then implement the base class `IncrementalGenerator` and apply the `[SgfGenerator]` attribute to your type;
1010

1111
```cs
12+
using SGF;
13+
1214
namespace Example
1315
{
1416
// IncrementalGenerator, is a generated type from `SourceGenerator.Foundations'
15-
[SgfGenerator]
17+
[IncrementalGenerator]
1618
public class ExampleSourceGenerator : IncrementalGenerator
1719
{
1820
public ExampleSourceGenerator() : base("ExampleSourceGenerator")
@@ -148,7 +150,7 @@ To fix the error just apply the attribute.
148150

149151
```cs
150152
// Fixed
151-
[SgfGeneratorAttribute]
153+
[IncrementalGenerator]
152154
public class MyGenerator : IncrementalGenerator
153155
{
154156
public MyGenerator() : base("MyGenerator")

src/Sandbox/ConsoleApp.SourceGenerator/ConsoleAppSourceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace ConsoleApp.SourceGenerator
99
{
10-
[SgfGenerator]
10+
[IncrementalGenerator]
1111
internal class ConsoleAppSourceGenerator : IncrementalGenerator
1212
{
1313
public class Payload

src/SourceGenerator.Foundations.Contracts/SgfGeneratorAttribute.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,20 @@ namespace SGF
77
/// that will have Source Generator Foundations wrapper generated around it. This adds
88
/// better error handling and logging to the given generator.
99
/// </summary>
10+
[Obsolete($"Please use the {nameof(IncrementalGeneratorAttribute)} instead", error: false)]
1011
[AttributeUsage(AttributeTargets.Class)]
1112
public sealed class SgfGeneratorAttribute : Attribute
1213
{
1314
}
15+
16+
/// <summary>
17+
/// Applied a class that inherits from <see cref="IncrementalGenerator"/>
18+
/// that will have Source Generator Foundations wrapper generated around it. This adds
19+
/// better error handling and logging to the given generator.
20+
/// </summary>
21+
[AttributeUsage(AttributeTargets.Class)]
22+
public sealed class IncrementalGeneratorAttribute : Attribute
23+
{
24+
25+
}
1426
}

src/SourceGenerator.Foundations/Analyzer/Rules/RequireSfgGeneratorAttributeRule.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ public RequireSfgGeneratorAttributeRule() : base(CreateDescriptor())
1212

1313
protected override void Analyze(ClassDeclarationSyntax classDeclaration)
1414
{
15-
if (!HasAttribute(classDeclaration, nameof(SgfGeneratorAttribute)))
15+
#pragma warning disable CS0618 // Type or member is obsolete
16+
if ( !HasAttribute(classDeclaration, nameof(IncrementalGeneratorAttribute)) &&
17+
!HasAttribute(classDeclaration, nameof(SgfGeneratorAttribute)))
1618
{
1719
Location location = classDeclaration.Identifier.GetLocation();
1820
ReportDiagnostic(location, classDeclaration.Identifier.Text);
1921
}
22+
#pragma warning restore CS0618 // Type or member is obsolete
23+
2024
}
2125

2226
private static DiagnosticDescriptor CreateDescriptor()
2327
=> new DiagnosticDescriptor("SGF1001",
2428
"SGFGeneratorAttributeApplied",
25-
$"{{0}} is missing the {nameof(SgfGeneratorAttribute)}",
29+
$"{{0}} is missing the {nameof(IncrementalGeneratorAttribute)}",
2630
"SourceGeneration",
2731
DiagnosticSeverity.Error,
2832
true,
29-
$"Source generators are required to have the attribute {nameof(SgfGeneratorAttribute)} applied to them otherwise the compiler won't invoke them",
33+
$"Source generators are required to have the attribute {nameof(IncrementalGeneratorAttribute)} applied to them otherwise the compiler won't invoke them",
3034
"https://github.com/ByronMayne/SourceGenerator.Foundations?tab=readme-ov-file#sgf1001");
3135
}
3236
}

src/SourceGenerator.Foundations/HoistSourceGenerator.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode s)
6666
string fullName = attributeContainingTypeSymbol.ToDisplayString();
6767

6868
// Is the attribute the [EnumExtensions] attribute?
69-
if (fullName == "SGF.SgfGeneratorAttribute")
69+
#pragma warning disable CS0618 // Type or member is obsolete
70+
switch (fullName)
7071
{
71-
// return the enum. Implementation shown in section 7.
72-
return SourceGeneratorDataModel.Create(classDeclarationSyntax, context.SemanticModel);
72+
case $"SGF.{nameof(IncrementalGeneratorAttribute)}":
73+
case $"SGF.{nameof(SgfGeneratorAttribute)}":
74+
return SourceGeneratorDataModel.Create(classDeclarationSyntax, context.SemanticModel);
7375
}
76+
#pragma warning restore CS0618 // Type or member is obsolete
7477
}
7578
}
7679

0 commit comments

Comments
 (0)