@@ -69,6 +69,12 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol)
6969
7070 var rewrittenDeclarationSyntax = GetSyntaxNodeFromSymbol ( typeSymbol , remappedName ) ;
7171
72+ if ( rewrittenDeclarationSyntax == null )
73+ {
74+ _processedTypes . Remove ( fullTypeName ) ;
75+ return null ;
76+ }
77+
7278 var typeCode = rewrittenDeclarationSyntax . ToFullString ( ) ;
7379 var newTypeDeclaration = SyntaxFactory . ParseMemberDeclaration ( typeCode ) ;
7480
@@ -93,31 +99,52 @@ private TypeSyntax CreateTypeSyntaxForSymbol(ITypeSymbol typeSymbol)
9399 arrayRankSpecifiers = SyntaxFactory . List ( new [ ] { SyntaxFactory . ArrayRankSpecifier ( ranksList ) } ) ;
94100 var nextTypeSyntax = CreateTypeSyntaxForSymbol ( arrayTypeSymbol . ElementType ) ;
95101
102+ if ( nextTypeSyntax == null )
103+ {
104+ return null ;
105+ }
106+
96107 result = SyntaxFactory . ArrayType ( nextTypeSyntax , arrayRankSpecifiers . Value ) ;
97108 }
98109 // TODO optimize
99110 else if ( typeSymbol is INamedTypeSymbol namedTypeSymbol &&
100- namedTypeSymbol . TypeArguments . Length = = 1 &&
111+ namedTypeSymbol . TypeArguments . Length > = 1 &&
101112 namedTypeSymbol . IsSupportedCollection ( ) )
102113 {
103- var underlyingTypeSyntax = CreateTypeSyntaxForSymbol ( namedTypeSymbol . TypeArguments . Single ( ) ) ;
104- var listSyntax = SyntaxFactory . GenericName (
105- SyntaxFactory . Identifier ( "List" ) ,
106- SyntaxFactory . TypeArgumentList ( SyntaxFactory . SeparatedList ( new [ ] { underlyingTypeSyntax } ) ) ) ;
114+ var underlyingTypeSyntaxes = namedTypeSymbol . TypeArguments . Select ( typeArgument => CreateTypeSyntaxForSymbol ( typeArgument ) ) ;
115+ if ( underlyingTypeSyntaxes . Any ( underlyingTypeSyntax => underlyingTypeSyntax == null ) )
116+ {
117+ return null ;
118+ }
119+
120+ var collectionIdentifier = namedTypeSymbol . Name ;
121+
122+ var collectionSyntax = SyntaxFactory . GenericName (
123+ SyntaxFactory . Identifier ( collectionIdentifier ) ,
124+ SyntaxFactory . TypeArgumentList ( SyntaxFactory . SeparatedList ( underlyingTypeSyntaxes ) ) ) ;
107125
108126 result = SyntaxFactory . QualifiedName (
109127 SyntaxFactory . QualifiedName (
110128 SyntaxFactory . QualifiedName (
111129 SyntaxFactory . IdentifierName ( "System" ) ,
112130 SyntaxFactory . IdentifierName ( "Collections" ) ) ,
113131 SyntaxFactory . IdentifierName ( "Generic" ) ) ,
114- listSyntax ) ;
132+ collectionSyntax ) ;
133+ }
134+ else if ( typeSymbol . IsUserDefinedCollection ( ) )
135+ {
136+ return null ;
115137 }
116138 else
117139 {
118140 var ( isNullable , underlingTypeSymbol ) = typeSymbol . DiscardNullable ( ) ;
119-
120141 var newTypeName = ProcessTypeSymbol ( underlingTypeSymbol ) ;
142+
143+ if ( newTypeName == null )
144+ {
145+ return null ;
146+ }
147+
121148 result = isNullable ? SyntaxFactoryUtilities . GetNullableType ( newTypeName ) :
122149 SyntaxFactory . ParseTypeName ( newTypeName ) ;
123150 }
@@ -171,7 +198,7 @@ private ExpressionSyntax GenerateExpressionFromBsonAttributeArgumentInfo(TypedCo
171198 _ => null
172199 } ;
173200
174- private void GenerateFields ( ITypeSymbol typeSymbol , List < MemberDeclarationSyntax > members )
201+ private bool GenerateFields ( ITypeSymbol typeSymbol , List < MemberDeclarationSyntax > members )
175202 {
176203 var typeFields = typeSymbol
177204 . GetMembers ( )
@@ -184,6 +211,10 @@ private void GenerateFields(ITypeSymbol typeSymbol, List<MemberDeclarationSyntax
184211 foreach ( var fieldSymbol in typeFields )
185212 {
186213 var typeSyntax = CreateTypeSyntaxForSymbol ( fieldSymbol . Type ) ;
214+ if ( typeSyntax == null )
215+ {
216+ return false ;
217+ }
187218
188219 var variableDeclaration = SyntaxFactory . VariableDeclaration ( typeSyntax , SyntaxFactory . SingletonSeparatedList ( SyntaxFactory . VariableDeclarator ( fieldSymbol . Name ) ) ) ;
189220
@@ -199,9 +230,11 @@ private void GenerateFields(ITypeSymbol typeSymbol, List<MemberDeclarationSyntax
199230
200231 members . Add ( fieldDeclaration ) ;
201232 }
233+
234+ return true ;
202235 }
203236
204- private void GenerateProperties ( ITypeSymbol typeSymbol , List < MemberDeclarationSyntax > members )
237+ private bool GenerateProperties ( ITypeSymbol typeSymbol , List < MemberDeclarationSyntax > members )
205238 {
206239 var typeProperties = typeSymbol
207240 . GetMembers ( )
@@ -215,6 +248,10 @@ private void GenerateProperties(ITypeSymbol typeSymbol, List<MemberDeclarationSy
215248 foreach ( var propertySymbol in typeProperties )
216249 {
217250 var typeSyntax = CreateTypeSyntaxForSymbol ( propertySymbol . Type ) ;
251+ if ( typeSyntax == null )
252+ {
253+ return false ;
254+ }
218255
219256 var propertyDeclaration = SyntaxFactory . PropertyDeclaration ( typeSyntax , propertySymbol . Name ) ;
220257
@@ -229,6 +266,8 @@ private void GenerateProperties(ITypeSymbol typeSymbol, List<MemberDeclarationSy
229266
230267 members . Add ( propertyDeclaration ) ;
231268 }
269+
270+ return true ;
232271 }
233272
234273 private BaseListSyntax GetBaseListSyntax ( string typeName )
@@ -244,7 +283,8 @@ private string GetFullName(ITypeSymbol typeSymbol) =>
244283
245284 private ( string RemappedName , string FullTypeName ) GetGeneratedTypeMapping ( ITypeSymbol typeSymbol , bool userOnlyTypes )
246285 {
247- if ( typeSymbol == null )
286+ if ( typeSymbol == null ||
287+ typeSymbol . IsUserDefinedCollection ( ) )
248288 {
249289 return default ;
250290 }
@@ -285,8 +325,11 @@ private TypeDeclarationSyntax GetSyntaxForClassOrStruct(ITypeSymbol typeSymbol,
285325
286326 var members = new List < MemberDeclarationSyntax > ( ) ;
287327
288- GenerateProperties ( typeSymbol , members ) ;
289- GenerateFields ( typeSymbol , members ) ;
328+ if ( ! GenerateProperties ( typeSymbol , members ) ||
329+ ! GenerateFields ( typeSymbol , members ) )
330+ {
331+ return null ;
332+ }
290333
291334 typeDeclaration = typeDeclaration
292335 . AddMembers ( members . ToArray ( ) )
@@ -298,6 +341,11 @@ private TypeDeclarationSyntax GetSyntaxForClassOrStruct(ITypeSymbol typeSymbol,
298341 {
299342 var baseTypeNameGenerated = ProcessTypeSymbol ( typeSymbol . BaseType ) ;
300343
344+ if ( baseTypeNameGenerated == null )
345+ {
346+ return null ;
347+ }
348+
301349 typeDeclaration = typeDeclaration . WithBaseList ( GetBaseListSyntax ( baseTypeNameGenerated ) ) ;
302350 }
303351
@@ -351,6 +399,11 @@ private BaseTypeDeclarationSyntax GetSyntaxNodeFromSymbol(ITypeSymbol typeSymbol
351399 throw new NotSupportedException ( $ "Symbol type { typeSymbol . TypeKind } is not supported.") ;
352400 }
353401
402+ if ( typeDeclaration == null )
403+ {
404+ return null ;
405+ }
406+
354407 typeDeclaration = typeDeclaration . NormalizeWhitespace ( ) ;
355408 return typeDeclaration ;
356409 }
0 commit comments