-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use
ForAttributeWithMetadataName
instead of `CreateSyntaxProvi…
…der` (#327)
- Loading branch information
1 parent
36830e8
commit 3b9c2f9
Showing
3 changed files
with
69 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#if !ROSLYN4_4_OR_GREATER | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly; | ||
|
||
internal static class SyntaxProvider | ||
{ | ||
public static IncrementalValuesProvider<ClassDeclarationSyntax> GetClassDeclarations(IncrementalGeneratorInitializationContext context) | ||
{ | ||
return context.SyntaxProvider | ||
.CreateSyntaxProvider( | ||
static (s, _) => IsSyntaxTargetForGeneration(s), | ||
static (ctx, _) => GetSemanticTargetForGeneration(ctx)) | ||
.WhereNotNull(); | ||
} | ||
|
||
private static bool IsSyntaxTargetForGeneration(SyntaxNode node) | ||
=> node is ClassDeclarationSyntax { AttributeLists.Count: > 0 }; | ||
|
||
private static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext ctx) | ||
{ | ||
var classDeclaration = (ClassDeclarationSyntax)ctx.Node; | ||
foreach (var attributeListSyntax in classDeclaration.AttributeLists) | ||
{ | ||
foreach (var attributeSyntax in attributeListSyntax.Attributes) | ||
{ | ||
if (ctx.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) | ||
continue; | ||
|
||
var attributeContainingTypeSymbol = attributeSymbol.ContainingType; | ||
var fullName = attributeContainingTypeSymbol.ToDisplayString(); | ||
if (fullName == MapperGenerator.MapperAttributeName) | ||
return classDeclaration; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#if ROSLYN4_4_OR_GREATER | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly; | ||
|
||
internal static class SyntaxProvider | ||
{ | ||
|
||
public static IncrementalValuesProvider<ClassDeclarationSyntax> GetClassDeclarations(IncrementalGeneratorInitializationContext context) | ||
{ | ||
return context.SyntaxProvider | ||
.ForAttributeWithMetadataName(MapperGenerator.MapperAttributeName, | ||
static (s, _) => s is ClassDeclarationSyntax, | ||
static (ctx, _) => ctx.TargetNode as ClassDeclarationSyntax) | ||
.WhereNotNull(); | ||
} | ||
|
||
} | ||
#endif |