source: trunk/tools/PersistenceCodeFix/PersistenceCodeFix/PersistenceCodeFix/Analyzers/MissingStorableType/MissingStorableTypeAnalyzer.cs @ 14985

Last change on this file since 14985 was 14985, checked in by jkarder, 5 years ago

#2520: worked on persistence code fix

  • split existing analyzer and code fix
  • added analyzer and code fix for missing storable ctors
File size: 2.0 KB
Line 
1using System.Collections.Immutable;
2using System.Linq;
3using Microsoft.CodeAnalysis;
4using Microsoft.CodeAnalysis.Diagnostics;
5
6namespace PersistenceCodeFix {
7  [DiagnosticAnalyzer(LanguageNames.CSharp)]
8  public sealed class MissingStorableTypeAnalyzer : DiagnosticAnalyzer {
9    public const string DiagnosticId = "MissingStorableType";
10
11    private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.MissingStorableTypeAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
12    private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.MissingStorableTypeAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
13    private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.MissingStorableTypeAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
14    private const string Category = nameof(DiagnosticCategory.Persistence);
15
16    private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Info, isEnabledByDefault: true, description: Description);
17
18    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
19
20    public override void Initialize(AnalysisContext context) {
21      context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType);
22    }
23
24    private static void AnalyzeNamedType(SymbolAnalysisContext context) {
25      var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;
26      if (namedTypeSymbol.IsStatic) return;
27      if (namedTypeSymbol.TypeKind == TypeKind.Delegate) return;
28
29      var attr = context.Symbol.GetAttributes();
30      if (!attr.Any(a => a.AttributeClass.Name == "StorableTypeAttribute")) {
31        var diagnostic = Diagnostic.Create(Rule, namedTypeSymbol.Locations[0], namedTypeSymbol.Name);
32        context.ReportDiagnostic(diagnostic);
33      }
34    }
35  }
36}
Note: See TracBrowser for help on using the repository browser.