@@ -21,7 +21,7 @@ public class ParserSourceGenerator : IIncrementalGenerator
2121{
2222 public void Initialize ( IncrementalGeneratorInitializationContext context )
2323 {
24- var classDeclarations = context . SyntaxProvider
24+ var classesToGenerateFor = context . SyntaxProvider
2525 . CreateSyntaxProvider (
2626 predicate : static ( syntaxNode , _ ) => syntaxNode is ClassDeclarationSyntax
2727 {
@@ -30,38 +30,20 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3030 transform : ( ctx , _ ) =>
3131 {
3232 var classDeclaration = ( ClassDeclarationSyntax ) ctx . Node ;
33- return ctx . SemanticModel . GetDeclaredSymbol ( classDeclaration ) as INamedTypeSymbol ;
34- } )
35- . Where ( static symbol => symbol is not null ) ;
36-
37- var parserOutputAttrSymbol = context . CompilationProvider
38- . Select ( ( compilation , _ ) => compilation . GetTypeByMetadataName ( "Parsers.ParserOutputAttribute" ) ) ;
33+ var symbol = ctx . SemanticModel . GetDeclaredSymbol ( classDeclaration ) as INamedTypeSymbol ;
34+ if ( symbol == null ) return null ;
3935
40- var classSymbolsArray = classDeclarations
41- . Combine ( parserOutputAttrSymbol )
42- . Select ( ( l , _ ) =>
43- {
44- var ( symbol , parserAttr ) = l ;
45- if ( parserAttr is null ) return null ;
46-
47- return symbol . GetAttributes ( ) . Any ( a =>
48- SymbolEqualityComparer . Default . Equals ( a . AttributeClass , parserAttr ) )
49- ? symbol
50- : null ;
51- } )
36+ return symbol . GetAttributes ( ) . Any ( attr => attr . AttributeClass is { Name : "ParserOutputAttribute" } )
37+ ? symbol
38+ : null ;
39+ } )
5240 . Where ( static symbol => symbol is not null )
5341 . Collect ( ) ;
5442
55- context . RegisterSourceOutput (
56- context . CompilationProvider . Combine ( classSymbolsArray ) ,
57- ( sourceProductionContext , f ) =>
58- {
59- var attributeIndexTypeSymbol = f . Left . GetTypeByMetadataName ( "Parsers.ArrayIndexAttribute" ) ;
60- GenerateSerializer ( sourceProductionContext , f . Right , attributeIndexTypeSymbol ) ;
61- } ) ;
43+ context . RegisterSourceOutput ( classesToGenerateFor , GenerateSerializer ) ;
6244 }
6345
64- private static void GenerateSerializer ( SourceProductionContext context , ImmutableArray < INamedTypeSymbol > classSymbol , INamedTypeSymbol attributeIndexTypeSymbol )
46+ private static void GenerateSerializer ( SourceProductionContext context , ImmutableArray < INamedTypeSymbol > classSymbol )
6547 {
6648 var typeNames = new List < ( string TargetTypeName , string TargetTypeFullName , string TargetTypeParserName ) > ( ) ;
6749 var builder = new StringBuilder ( ) ;
@@ -89,8 +71,8 @@ public class Parser : IParserFactory
8971 var props = typeSymbol . GetMembers ( ) . OfType < IPropertySymbol > ( ) ;
9072 foreach ( var prop in props )
9173 {
92- var attr = prop . GetAttributes ( ) . FirstOrDefault ( x => SymbolEqualityComparer . Default . Equals ( x . AttributeClass , attributeIndexTypeSymbol ) ) ;
93- if ( attr == null || attr . ConstructorArguments [ 0 ] . Value is not int ) continue ;
74+ var attr = prop . GetAttributes ( ) . FirstOrDefault ( attr => attr . AttributeClass is { Name : "ArrayIndexAttribute" } ) ;
75+ if ( attr ? . ConstructorArguments [ 0 ] . Value is not int ) continue ;
9476
9577 int order = ( int ) attr . ConstructorArguments [ 0 ] . Value ;
9678 if ( order < 0 ) continue ;
0 commit comments