@@ -14,6 +14,7 @@ namespace CodeWriter.RequireFieldsInit
1414 public class RequireFieldsInitAnalyzer : DiagnosticAnalyzer
1515 {
1616 private const string RequireFieldsInitAttributeName = "RequireFieldsInitAttribute" ;
17+ private const string RequireFieldsInitDisableChecksInNamespaceAttributeName = "RequireFieldsInitDisableChecksInNamespaceAttribute" ;
1718
1819 private static readonly DiagnosticDescriptor UnhandledErrorRule = new (
1920 id : "RequireFieldsInit_000" ,
@@ -52,9 +53,21 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
5253 return ;
5354 }
5455
56+ if ( context . Compilation . GetTypeByMetadataName ( RequireFieldsInitDisableChecksInNamespaceAttributeName )
57+ is not INamedTypeSymbol disableChecksInNamespaceAttributeTypeSymbol )
58+ {
59+ return ;
60+ }
61+
62+ var disabledNamespaces = context . Compilation . Assembly . GetAttributes ( )
63+ . Where ( it => SymbolEqualityComparer . Default . Equals ( it . AttributeClass , disableChecksInNamespaceAttributeTypeSymbol ) )
64+ . Select ( it => ( string ) it . NamedArguments . Single ( a => a . Key == "Namespace" ) . Value . Value )
65+ . ToList ( ) ;
66+
5567 var cache = new Cache
5668 {
5769 AttributeTypeSymbol = attributeTypeSymbol ,
70+ DisabledNamespaces = disabledNamespaces ,
5871 RequiredFieldsCache =
5972 new ConcurrentDictionary < INamedTypeSymbol , List < string > > ( SymbolEqualityComparer . Default ) ,
6073 } ;
@@ -104,6 +117,16 @@ private void CheckObjectCreation(SyntaxNodeAnalysisContext context, Cache cache)
104117 return ;
105118 }
106119
120+ if ( cache . DisabledNamespaces . Count > 0 )
121+ {
122+ var ns = creationSyntax . FirstAncestorOrSelf < NamespaceDeclarationSyntax > ( ) ? . Name ? . ToString ( ) ?? "" ;
123+
124+ if ( cache . DisabledNamespaces . Contains ( ns ) )
125+ {
126+ return ;
127+ }
128+ }
129+
107130 Analyze ( context , creationSyntax , allRequiredFields ) ;
108131 }
109132
@@ -153,23 +176,23 @@ private static List<string> PopulateRequiredFields(INamedTypeSymbol typeSymbol,
153176 {
154177 switch ( kvp )
155178 {
156- case { Key : "Optional" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
179+ case { Key : "Optional" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
157180 optionalArg = kvp . Value ;
158181 break ;
159182
160- case { Key : "Required" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
183+ case { Key : "Required" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
161184 requiredArg = kvp . Value ;
162185 break ;
163186 }
164187 }
165188
166189 var requiredFields = new List < string > ( ) ;
167190
168- if ( requiredArg is { Values : var requiredValuesArg } )
191+ if ( requiredArg is { Values : var requiredValuesArg } )
169192 {
170193 foreach ( var element in requiredValuesArg )
171194 {
172- if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string requiredFieldName } )
195+ if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string requiredFieldName } )
173196 {
174197 requiredFields . Add ( requiredFieldName ) ;
175198 }
@@ -186,11 +209,11 @@ private static List<string> PopulateRequiredFields(INamedTypeSymbol typeSymbol,
186209 }
187210 }
188211
189- if ( optionalArg is { Values : var optionalValuesArg } )
212+ if ( optionalArg is { Values : var optionalValuesArg } )
190213 {
191214 foreach ( var element in optionalValuesArg )
192215 {
193- if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string optionalFieldName } )
216+ if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string optionalFieldName } )
194217 {
195218 requiredFields . Remove ( optionalFieldName ) ;
196219 }
@@ -202,6 +225,7 @@ private static List<string> PopulateRequiredFields(INamedTypeSymbol typeSymbol,
202225
203226 public class Cache
204227 {
228+ public List < string > DisabledNamespaces ;
205229 public INamedTypeSymbol AttributeTypeSymbol ;
206230 public ConcurrentDictionary < INamedTypeSymbol , List < string > > RequiredFieldsCache ;
207231 }
0 commit comments