@@ -14,6 +14,7 @@ namespace CodeWriter.RequireFieldsInit
14
14
public class RequireFieldsInitAnalyzer : DiagnosticAnalyzer
15
15
{
16
16
private const string RequireFieldsInitAttributeName = "RequireFieldsInitAttribute" ;
17
+ private const string RequireFieldsInitDisableChecksInNamespaceAttributeName = "RequireFieldsInitDisableChecksInNamespaceAttribute" ;
17
18
18
19
private static readonly DiagnosticDescriptor UnhandledErrorRule = new (
19
20
id : "RequireFieldsInit_000" ,
@@ -52,9 +53,21 @@ private void OnCompilationStart(CompilationStartAnalysisContext context)
52
53
return ;
53
54
}
54
55
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
+
55
67
var cache = new Cache
56
68
{
57
69
AttributeTypeSymbol = attributeTypeSymbol ,
70
+ DisabledNamespaces = disabledNamespaces ,
58
71
RequiredFieldsCache =
59
72
new ConcurrentDictionary < INamedTypeSymbol , List < string > > ( SymbolEqualityComparer . Default ) ,
60
73
} ;
@@ -104,6 +117,16 @@ private void CheckObjectCreation(SyntaxNodeAnalysisContext context, Cache cache)
104
117
return ;
105
118
}
106
119
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
+
107
130
Analyze ( context , creationSyntax , allRequiredFields ) ;
108
131
}
109
132
@@ -153,23 +176,23 @@ private static List<string> PopulateRequiredFields(INamedTypeSymbol typeSymbol,
153
176
{
154
177
switch ( kvp )
155
178
{
156
- case { Key : "Optional" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
179
+ case { Key : "Optional" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
157
180
optionalArg = kvp . Value ;
158
181
break ;
159
182
160
- case { Key : "Required" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
183
+ case { Key : "Required" , Value : { IsNull : false , Kind : TypedConstantKind . Array } } :
161
184
requiredArg = kvp . Value ;
162
185
break ;
163
186
}
164
187
}
165
188
166
189
var requiredFields = new List < string > ( ) ;
167
190
168
- if ( requiredArg is { Values : var requiredValuesArg } )
191
+ if ( requiredArg is { Values : var requiredValuesArg } )
169
192
{
170
193
foreach ( var element in requiredValuesArg )
171
194
{
172
- if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string requiredFieldName } )
195
+ if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string requiredFieldName } )
173
196
{
174
197
requiredFields . Add ( requiredFieldName ) ;
175
198
}
@@ -186,11 +209,11 @@ private static List<string> PopulateRequiredFields(INamedTypeSymbol typeSymbol,
186
209
}
187
210
}
188
211
189
- if ( optionalArg is { Values : var optionalValuesArg } )
212
+ if ( optionalArg is { Values : var optionalValuesArg } )
190
213
{
191
214
foreach ( var element in optionalValuesArg )
192
215
{
193
- if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string optionalFieldName } )
216
+ if ( element is { IsNull : false , Kind : TypedConstantKind . Primitive , Value : string optionalFieldName } )
194
217
{
195
218
requiredFields . Remove ( optionalFieldName ) ;
196
219
}
@@ -202,6 +225,7 @@ private static List<string> PopulateRequiredFields(INamedTypeSymbol typeSymbol,
202
225
203
226
public class Cache
204
227
{
228
+ public List < string > DisabledNamespaces ;
205
229
public INamedTypeSymbol AttributeTypeSymbol ;
206
230
public ConcurrentDictionary < INamedTypeSymbol , List < string > > RequiredFieldsCache ;
207
231
}
0 commit comments