[14985] | 1 | using System.Collections.Immutable;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using Microsoft.CodeAnalysis;
|
---|
| 4 | using Microsoft.CodeAnalysis.Diagnostics;
|
---|
| 5 |
|
---|
| 6 | namespace PersistenceCodeFix {
|
---|
| 7 | [DiagnosticAnalyzer(LanguageNames.CSharp)]
|
---|
| 8 | public sealed class MissingStorableTypeAnalyzer : DiagnosticAnalyzer {
|
---|
| 9 | public const string DiagnosticId = "MissingStorableType";
|
---|
| 10 |
|
---|
[16475] | 11 | private static readonly LocalizableString Title = new LocalizableResourceString(
|
---|
| 12 | nameof(Resources.MissingStorableTypeAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
|
---|
| 13 |
|
---|
| 14 | private static readonly LocalizableString MessageFormat = new LocalizableResourceString(
|
---|
| 15 | nameof(Resources.MissingStorableTypeAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
|
---|
| 16 |
|
---|
| 17 | private static readonly LocalizableString Description = new LocalizableResourceString(
|
---|
| 18 | nameof(Resources.MissingStorableTypeAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
|
---|
| 19 |
|
---|
[14985] | 20 | private const string Category = nameof(DiagnosticCategory.Persistence);
|
---|
| 21 |
|
---|
[16475] | 22 | private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
|
---|
| 23 | DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Info, isEnabledByDefault: true, description: Description);
|
---|
[14985] | 24 |
|
---|
| 25 | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
|
---|
| 26 |
|
---|
| 27 | public override void Initialize(AnalysisContext context) {
|
---|
| 28 | context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | private static void AnalyzeNamedType(SymbolAnalysisContext context) {
|
---|
| 32 | var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;
|
---|
| 33 | if (namedTypeSymbol.IsStatic) return;
|
---|
| 34 | if (namedTypeSymbol.TypeKind == TypeKind.Delegate) return;
|
---|
| 35 |
|
---|
| 36 | var attr = context.Symbol.GetAttributes();
|
---|
| 37 | if (!attr.Any(a => a.AttributeClass.Name == "StorableTypeAttribute")) {
|
---|
| 38 | var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name);
|
---|
| 39 | context.ReportDiagnostic(diagnostic);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 | }
|
---|