source: trunk/tools/PersistenceCodeFix/PersistenceCodeFix/PersistenceCodeFix/Analyzers/ObsoleteStorableClass/ObsoleteStorableClassAnalyzer.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: 1.9 KB
Line 
1using System.Collections.Immutable;
2using Microsoft.CodeAnalysis;
3using Microsoft.CodeAnalysis.CSharp;
4using Microsoft.CodeAnalysis.CSharp.Syntax;
5using Microsoft.CodeAnalysis.Diagnostics;
6
7namespace PersistenceCodeFix {
8  [DiagnosticAnalyzer(LanguageNames.CSharp)]
9  public sealed class ObsoleteStorableClassAnalyzer : DiagnosticAnalyzer {
10    public const string DiagnosticId = "ObsoleteStorableClass";
11
12    private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.ObsoleteStorableClassAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
13    private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.ObsoleteStorableClassAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
14    private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.ObsoleteStorableClassAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
15    private const string Category = nameof(DiagnosticCategory.Persistence);
16
17    private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
18
19    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
20
21    public override void Initialize(AnalysisContext context) {
22      context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
23    }
24
25    private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) {
26      var attributeSyntax = (AttributeSyntax)context.Node;
27      string attrName = attributeSyntax.Name.ToString();
28      if (attrName != "StorableClass") return;
29
30      var diagnostic = Diagnostic.Create(Rule, attributeSyntax.GetLocation(), attrName);
31      context.ReportDiagnostic(diagnostic);
32    }
33  }
34}
Note: See TracBrowser for help on using the repository browser.