@@ -87,6 +87,28 @@ public class InspectionXmlDocAnalyzer : DiagnosticAnalyzer
87
87
new LocalizableResourceString ( nameof ( Resources . MissingNameAttributeDescription ) , Resources . ResourceManager , typeof ( Resources ) )
88
88
) ;
89
89
90
+ public const string MissingTypeAttribute = "MissingTypeAttribute" ;
91
+ private static readonly DiagnosticDescriptor MissingTypeAttributeRule = new DiagnosticDescriptor (
92
+ MissingTypeAttribute ,
93
+ new LocalizableResourceString ( nameof ( Resources . MissingTypeAttribute ) , Resources . ResourceManager , typeof ( Resources ) ) ,
94
+ new LocalizableResourceString ( nameof ( Resources . MissingTypeAttributeMessageFormat ) , Resources . ResourceManager , typeof ( Resources ) ) ,
95
+ new LocalizableResourceString ( nameof ( Resources . XmlDocAnalyzerCategory ) , Resources . ResourceManager , typeof ( Resources ) ) . ToString ( ) ,
96
+ DiagnosticSeverity . Error ,
97
+ true ,
98
+ new LocalizableResourceString ( nameof ( Resources . MissingTypeAttributeDescription ) , Resources . ResourceManager , typeof ( Resources ) )
99
+ ) ;
100
+
101
+ public const string InvalidTypeAttribute = "InvalidTypeAttribute" ;
102
+ private static readonly DiagnosticDescriptor InvalidTypeAttributeRule = new DiagnosticDescriptor (
103
+ InvalidTypeAttribute ,
104
+ new LocalizableResourceString ( nameof ( Resources . InvalidTypeAttribute ) , Resources . ResourceManager , typeof ( Resources ) ) ,
105
+ new LocalizableResourceString ( nameof ( Resources . InvalidTypeAttributeMessageFormat ) , Resources . ResourceManager , typeof ( Resources ) ) ,
106
+ new LocalizableResourceString ( nameof ( Resources . XmlDocAnalyzerCategory ) , Resources . ResourceManager , typeof ( Resources ) ) . ToString ( ) ,
107
+ DiagnosticSeverity . Error ,
108
+ true ,
109
+ new LocalizableResourceString ( nameof ( Resources . InvalidTypeAttributeDescription ) , Resources . ResourceManager , typeof ( Resources ) )
110
+ ) ;
111
+
90
112
public const string MissingHasResultAttribute = "MissingHasResultAttribute" ;
91
113
private static readonly DiagnosticDescriptor MissingHasResultAttributeRule = new DiagnosticDescriptor (
92
114
MissingHasResultAttribute ,
@@ -106,7 +128,9 @@ public class InspectionXmlDocAnalyzer : DiagnosticAnalyzer
106
128
MissingHasResultAttributeRule ,
107
129
MissingNameAttributeRule ,
108
130
MissingModuleElementRule ,
109
- MissingExampleElementRule
131
+ MissingExampleElementRule ,
132
+ MissingTypeAttributeRule ,
133
+ InvalidTypeAttributeRule
110
134
) ;
111
135
112
136
public override void Initialize ( AnalysisContext context )
@@ -160,14 +184,14 @@ private static void CheckWhyElement(SymbolAnalysisContext context, INamedTypeSym
160
184
161
185
private static void CheckNameAttribute ( SymbolAnalysisContext context , XElement element , Location location )
162
186
{
163
- if ( ! element . Attributes ( ) . Any ( a => a . Name . Equals ( "name" ) ) )
187
+ if ( ! element . Attributes ( ) . Any ( a => a . Name . LocalName . Equals ( "name" ) ) )
164
188
{
165
- var diagnostic = Diagnostic . Create ( MissingNameAttributeRule , location , element . Name ) ;
189
+ var diagnostic = Diagnostic . Create ( MissingNameAttributeRule , location , element . Name . LocalName ) ;
166
190
context . ReportDiagnostic ( diagnostic ) ;
167
191
}
168
192
}
169
193
170
- private static void CheckReferenceElement ( SymbolAnalysisContext context , INamedTypeSymbol symbol , XElement xml , IEnumerable < AttributeData > requiredLibAttributes )
194
+ private static void CheckReferenceElement ( SymbolAnalysisContext context , INamedTypeSymbol symbol , XElement xml , ICollection < AttributeData > requiredLibAttributes )
171
195
{
172
196
if ( requiredLibAttributes . Any ( ) && ! xml . Elements ( "reference" ) . Any ( ) )
173
197
{
@@ -218,23 +242,60 @@ private static void CheckExampleElement(SymbolAnalysisContext context, INamedTyp
218
242
var examples = xml . Elements ( "example" ) ;
219
243
foreach ( var example in examples )
220
244
{
221
- if ( ! example . Attributes ( ) . Any ( a => a . Name . LocalName . Equals ( "hasresult" , System . StringComparison . InvariantCultureIgnoreCase ) ) )
222
- {
223
- var diagnostic = Diagnostic . Create ( MissingHasResultAttributeRule , symbol . Locations [ 0 ] ) ;
224
- context . ReportDiagnostic ( diagnostic ) ;
225
- }
245
+ CheckHasResultAttribute ( context , example , symbol . Locations [ 0 ] ) ;
246
+ CheckModuleElements ( context , symbol , example ) ;
247
+ }
248
+ }
226
249
227
- if ( ! example . Elements ( "module" ) . Any ( ) )
228
- {
229
- var diagnostic = Diagnostic . Create ( MissingModuleElementRule , symbol . Locations [ 0 ] ) ;
230
- context . ReportDiagnostic ( diagnostic ) ;
231
- }
250
+ private static void CheckModuleElements ( SymbolAnalysisContext context , INamedTypeSymbol symbol , XElement example )
251
+ {
252
+ if ( ! example . Elements ( "module" ) . Any ( ) )
253
+ {
254
+ var diagnostic = Diagnostic . Create ( MissingModuleElementRule , symbol . Locations [ 0 ] ) ;
255
+ context . ReportDiagnostic ( diagnostic ) ;
256
+ }
257
+
258
+ foreach ( var module in example . Elements ( "module" ) )
259
+ {
260
+ CheckNameAttribute ( context , module , symbol . Locations [ 0 ] ) ;
261
+ CheckTypeAttribute ( context , module , symbol . Locations [ 0 ] ) ;
262
+ }
263
+ }
264
+
265
+ private static void CheckHasResultAttribute ( SymbolAnalysisContext context , XElement element , Location location )
266
+ {
267
+ if ( ! element . Attributes ( ) . Any ( a => a . Name . LocalName . Equals ( "hasresult" , System . StringComparison . InvariantCultureIgnoreCase ) ) )
268
+ {
269
+ var diagnostic = Diagnostic . Create ( MissingHasResultAttributeRule , location ) ;
270
+ context . ReportDiagnostic ( diagnostic ) ;
271
+ }
272
+ }
232
273
233
- foreach ( var module in example . Elements ( "module" ) )
274
+ private static void CheckTypeAttribute ( SymbolAnalysisContext context , XElement element , Location location )
275
+ {
276
+ var nameAttribute = element . Attributes ( ) . FirstOrDefault ( a => a . Name . LocalName . Equals ( "type" ) ) ;
277
+ if ( nameAttribute == null )
278
+ {
279
+ var diagnostic = Diagnostic . Create ( MissingTypeAttributeRule , location , element . Name . LocalName ) ;
280
+ context . ReportDiagnostic ( diagnostic ) ;
281
+ }
282
+ else
283
+ {
284
+ var typeNameValue = nameAttribute . Value ;
285
+ if ( ! ValidTypeAttributeValues . Contains ( typeNameValue ) )
234
286
{
235
- CheckNameAttribute ( context , module , symbol . Locations [ 0 ] ) ;
287
+ var diagnostic = Diagnostic . Create ( InvalidTypeAttributeRule , location , typeNameValue ) ;
288
+ context . ReportDiagnostic ( diagnostic ) ;
236
289
}
237
290
}
238
291
}
292
+
293
+ private static readonly List < string > ValidTypeAttributeValues = new List < string >
294
+ {
295
+ "Standard Module" ,
296
+ "Class Module" ,
297
+ "Document" ,
298
+ "User Form"
299
+ } ;
239
300
}
240
301
}
0 commit comments