-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathInspectionXmlDocAnalyzer.cs
386 lines (344 loc) · 21.4 KB
/
InspectionXmlDocAnalyzer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
namespace RubberduckCodeAnalysis
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class InspectionXmlDocAnalyzer : DiagnosticAnalyzer
{
public const string MissingInspectionSummaryElement = "MissingInspectionSummaryElement";
private static readonly DiagnosticDescriptor MissingSummaryElementRule = new DiagnosticDescriptor(
MissingInspectionSummaryElement,
new LocalizableResourceString(nameof(Resources.MissingSummaryElement), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingSummaryElementMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingInspectionSummaryElementDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingInspectionWhyElement = "MissingInspectionWhyElement";
private static readonly DiagnosticDescriptor MissingWhyElementRule = new DiagnosticDescriptor(
MissingInspectionWhyElement,
new LocalizableResourceString(nameof(Resources.MissingInspectionWhyElement), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingInspectionWhyElementMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingInspectionWhyElementDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingReferenceElement = "MissingReferenceElement";
private static readonly DiagnosticDescriptor MissingReferenceElementRule = new DiagnosticDescriptor(
MissingReferenceElement,
new LocalizableResourceString(nameof(Resources.MissingInspectionReferenceElement), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingInspectionReferenceElementMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingInspectionReferenceElementDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingRequiredLibraryAttribute = "MissingRequiredLibraryAttribute";
private static readonly DiagnosticDescriptor MissingRequiredLibAttributeRule = new DiagnosticDescriptor(
MissingRequiredLibraryAttribute,
new LocalizableResourceString(nameof(Resources.MissingRequiredLibAttribute), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingRequiredLibAttributeMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingRequiredLibAttributeDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingHostAppElement = "MissingHostAppElement";
private static readonly DiagnosticDescriptor MissingHostAppElementRule = new DiagnosticDescriptor(
MissingHostAppElement,
new LocalizableResourceString(nameof(Resources.MissingInspectionHostAppElement), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingInspectionHostAppElementMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingInspectionHostAppElementDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingRequiredHostAttribute = "MissingRequiredHostAttribute";
private static readonly DiagnosticDescriptor MissingRequiredHostAttributeRule = new DiagnosticDescriptor(
MissingRequiredHostAttribute,
new LocalizableResourceString(nameof(Resources.MissingRequiredHostAttribute), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingRequiredHostAttributeMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingRequiredHostAttributeDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingExampleElement = "MissingExampleElement";
private static readonly DiagnosticDescriptor MissingExampleElementRule = new DiagnosticDescriptor(
MissingExampleElement,
new LocalizableResourceString(nameof(Resources.MissingExampleElement), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingExampleElementMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Warning,
true,
new LocalizableResourceString(nameof(Resources.MissingExampleElementDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingModuleElement = "MissingModuleElement";
private static readonly DiagnosticDescriptor MissingModuleElementRule = new DiagnosticDescriptor(
MissingModuleElement,
new LocalizableResourceString(nameof(Resources.MissingModuleElement), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingModuleElementMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingModuleElementDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingNameAttribute = "MissingNameAttribute";
private static readonly DiagnosticDescriptor MissingNameAttributeRule = new DiagnosticDescriptor(
MissingNameAttribute,
new LocalizableResourceString(nameof(Resources.MissingNameAttribute), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingNameAttributeMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingNameAttributeDescription), Resources.ResourceManager, typeof(Resources))
);
public const string DuplicateNameAttribute = "DuplicateNameAttribute";
private static readonly DiagnosticDescriptor DuplicateNameAttributeRule = new DiagnosticDescriptor(
DuplicateNameAttribute,
new LocalizableResourceString(nameof(Resources.DuplicateNameAttribute), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.DuplicateNameAttributeMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.DuplicateNameAttributeDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingTypeAttribute = "MissingTypeAttribute";
private static readonly DiagnosticDescriptor MissingTypeAttributeRule = new DiagnosticDescriptor(
MissingTypeAttribute,
new LocalizableResourceString(nameof(Resources.MissingTypeAttribute), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingTypeAttributeMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingTypeAttributeDescription), Resources.ResourceManager, typeof(Resources))
);
public const string InvalidTypeAttribute = "InvalidTypeAttribute";
private static readonly DiagnosticDescriptor InvalidTypeAttributeRule = new DiagnosticDescriptor(
InvalidTypeAttribute,
new LocalizableResourceString(nameof(Resources.InvalidTypeAttribute), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.InvalidTypeAttributeMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.InvalidTypeAttributeDescription), Resources.ResourceManager, typeof(Resources))
);
public const string MissingHasResultAttribute = "MissingHasResultAttribute";
private static readonly DiagnosticDescriptor MissingHasResultAttributeRule = new DiagnosticDescriptor(
MissingHasResultAttribute,
new LocalizableResourceString(nameof(Resources.MissingHasResultAttribute), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.MissingHasResultAttributeMessageFormat), Resources.ResourceManager, typeof(Resources)),
new LocalizableResourceString(nameof(Resources.XmlDocAnalyzerCategory), Resources.ResourceManager, typeof(Resources)).ToString(),
DiagnosticSeverity.Error,
true,
new LocalizableResourceString(nameof(Resources.MissingHasResultAttributeDescription), Resources.ResourceManager, typeof(Resources))
);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(
MissingSummaryElementRule,
MissingWhyElementRule,
MissingReferenceElementRule,
MissingRequiredLibAttributeRule,
MissingHasResultAttributeRule,
MissingNameAttributeRule,
MissingModuleElementRule,
MissingExampleElementRule,
MissingTypeAttributeRule,
InvalidTypeAttributeRule,
DuplicateNameAttributeRule,
MissingHostAppElementRule,
MissingRequiredHostAttributeRule
);
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
}
private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;
if (!IsInspectionClass(namedTypeSymbol))
{
return;
}
var xml = XDocument.Parse(namedTypeSymbol.GetDocumentationCommentXml()).Element("member");
CheckSummaryElement(context, namedTypeSymbol, xml);
CheckWhyElement(context, namedTypeSymbol, xml);
CheckExampleElement(context, namedTypeSymbol, xml);
var attributes = namedTypeSymbol.GetAttributes();
var requiredLibraryAttributes = attributes
.Where(a => a.AttributeClass.Name == "RequiredLibraryAttribute")
.ToList();
var requiredHostAttributes = attributes
.Where(a => a.AttributeClass.Name == "RequiredHostAttribute")
.ToList();
CheckAttributeRelatedElementElements(context, namedTypeSymbol, xml, requiredLibraryAttributes, "reference", MissingReferenceElementRule);
CheckAttributeRelatedElementElements(context, namedTypeSymbol, xml, requiredHostAttributes, "hostApp", MissingHostAppElementRule);
CheckXmlRelatedAttribute(context, namedTypeSymbol, xml, requiredLibraryAttributes, "reference", MissingRequiredLibAttributeRule);
CheckXmlRelatedAttribute(context, namedTypeSymbol, xml, requiredHostAttributes, "hostApp", MissingRequiredHostAttributeRule);
}
private static bool IsInspectionClass(INamedTypeSymbol namedTypeSymbol)
{
return namedTypeSymbol.TypeKind == TypeKind.Class && !namedTypeSymbol.IsAbstract
&& namedTypeSymbol.ContainingNamespace.ToString().StartsWith("Rubberduck.CodeAnalysis.Inspections.Concrete")
&& namedTypeSymbol.AllInterfaces.Any(i => i.Name == "IInspection");
}
private static void CheckSummaryElement(SymbolAnalysisContext context, INamedTypeSymbol symbol, XElement xml)
{
if (xml.Element("summary") == null)
{
var diagnostic = Diagnostic.Create(MissingSummaryElementRule, symbol.Locations[0], symbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
private static void CheckWhyElement(SymbolAnalysisContext context, INamedTypeSymbol symbol, XElement xml)
{
if (xml.Element("why") == null)
{
var diagnostic = Diagnostic.Create(MissingWhyElementRule, symbol.Locations[0], symbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
private static string CheckNameAttributeAndReturnValue(SymbolAnalysisContext context, XElement element, Location location)
{
var nameAttribute = element.Attributes().FirstOrDefault(a => a.Name.LocalName.Equals("name"));
if (nameAttribute == null)
{
var diagnostic = Diagnostic.Create(MissingNameAttributeRule, location, element.Name.LocalName);
context.ReportDiagnostic(diagnostic);
}
return nameAttribute?.Value;
}
private static void CheckAttributeRelatedElementElements(SymbolAnalysisContext context, INamedTypeSymbol symbol, XElement xml, ICollection<AttributeData> requiredAttributes, string xmlElementName, DiagnosticDescriptor requiredElementDescriptor)
{
if (requiredAttributes.Any() && !xml.Elements(xmlElementName).Any())
{
var diagnostic = Diagnostic.Create(requiredElementDescriptor, symbol.Locations[0], symbol.Name);
context.ReportDiagnostic(diagnostic);
}
var xmlElementNames = new List<string>();
foreach (var element in xml.Elements(xmlElementName))
{
var name = CheckNameAttributeAndReturnValue(context, element, symbol.Locations[0]);
if (name != null)
{
xmlElementNames.Add(name);
}
}
CheckForDuplicateNames(context, symbol, xmlElementName, xmlElementNames);
var requiredNames = requiredAttributes
.Where(a => a.ConstructorArguments.Length > 0)
.Select(a => a.ConstructorArguments[0].Value.ToString())
.ToList();
foreach (var requiredName in requiredNames)
{
if (requiredNames.All(lib => lib != requiredName))
{
var diagnostic = Diagnostic.Create(requiredElementDescriptor, symbol.Locations[0], symbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
}
private static void CheckForDuplicateNames(SymbolAnalysisContext context, INamedTypeSymbol symbol, string xmlElementName, List<string> names)
{
var duplicateNames = names
.GroupBy(name => name)
.Where(group => @group.Count() > 1)
.Select(group => @group.Key);
foreach (var name in duplicateNames)
{
var diagnostic = Diagnostic.Create(DuplicateNameAttributeRule, symbol.Locations[0], name, xmlElementName);
context.ReportDiagnostic(diagnostic);
}
}
private static void CheckXmlRelatedAttribute(SymbolAnalysisContext context, INamedTypeSymbol symbol, XElement xml, IEnumerable<AttributeData> requiredAttributes, string xmlElementName, DiagnosticDescriptor requiredAttributeDescriptor)
{
var requiredNames = requiredAttributes
.Where(a => a.ConstructorArguments.Length > 0)
.Select(a => a.ConstructorArguments[0].Value.ToString())
.ToList();
foreach (var element in xml.Elements(xmlElementName))
{
var name = element.Attribute("name")?.Value;
if (name == null || requiredNames.All(lib => lib != name))
{
var diagnostic = Diagnostic.Create(requiredAttributeDescriptor, symbol.Locations[0], symbol.Name, name);
context.ReportDiagnostic(diagnostic);
}
}
}
private static void CheckExampleElement(SymbolAnalysisContext context, INamedTypeSymbol symbol, XElement xml)
{
if (!xml.Elements("example").Any())
{
var diagnostic = Diagnostic.Create(MissingExampleElementRule, symbol.Locations[0], symbol.Name);
context.ReportDiagnostic(diagnostic);
return;
}
var examples = xml.Elements("example");
foreach (var example in examples)
{
CheckHasResultAttribute(context, example, symbol.Locations[0]);
CheckModuleElements(context, symbol, example);
}
}
private static void CheckModuleElements(SymbolAnalysisContext context, INamedTypeSymbol symbol, XElement example)
{
if (!example.Elements("module").Any())
{
var diagnostic = Diagnostic.Create(MissingModuleElementRule, symbol.Locations[0]);
context.ReportDiagnostic(diagnostic);
}
var moduleNames = new List<string>();
foreach (var module in example.Elements("module"))
{
var moduleName = CheckNameAttributeAndReturnValue(context, module, symbol.Locations[0]);
if (moduleName != null)
{
moduleNames.Add(moduleName);
}
CheckTypeAttribute(context, module, symbol.Locations[0]);
}
CheckForDuplicateNames(context, symbol, "module", moduleNames);
}
private static void CheckHasResultAttribute(SymbolAnalysisContext context, XElement element, Location location)
{
if (!element.Attributes().Any(a => a.Name.LocalName.Equals("hasresult", System.StringComparison.InvariantCultureIgnoreCase)))
{
var diagnostic = Diagnostic.Create(MissingHasResultAttributeRule, location);
context.ReportDiagnostic(diagnostic);
}
}
private static void CheckTypeAttribute(SymbolAnalysisContext context, XElement element, Location location)
{
var nameAttribute = element.Attributes().FirstOrDefault(a => a.Name.LocalName.Equals("type"));
if (nameAttribute == null)
{
var diagnostic = Diagnostic.Create(MissingTypeAttributeRule, location, element.Name.LocalName);
context.ReportDiagnostic(diagnostic);
}
else
{
var typeNameValue = nameAttribute.Value;
if (!ValidTypeAttributeValues.Contains(typeNameValue))
{
var diagnostic = Diagnostic.Create(InvalidTypeAttributeRule, location, typeNameValue);
context.ReportDiagnostic(diagnostic);
}
}
}
private static readonly List<string> ValidTypeAttributeValues = new List<string>
{
"Standard Module",
"Class Module",
"Predeclared Class",
"Interface Module",
"Document Module",
"UserForm Module",
};
}
}