source: misc/tools/PersistenceCodeFix/PersistenceCodeFix/PersistenceCodeFix/Analyzers/ObsoleteStorableClass/ObsoleteStorableClassAnalyzer.cs @ 16475

Last change on this file since 16475 was 16475, checked in by jkarder, 3 years ago

#2520: added analyzer and fix for adapting storable ctor signature

File size: 2.0 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(
13      nameof(Resources.ObsoleteStorableClassAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
14
15    private static readonly LocalizableString MessageFormat = new LocalizableResourceString(
16      nameof(Resources.ObsoleteStorableClassAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
17
18    private static readonly LocalizableString Description = new LocalizableResourceString(
19      nameof(Resources.ObsoleteStorableClassAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
20
21    private const string Category = nameof(DiagnosticCategory.Persistence);
22
23    private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
24      DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
25
26    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } }
27
28    public override void Initialize(AnalysisContext context) {
29      context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute);
30    }
31
32    private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) {
33      var attributeSyntax = (AttributeSyntax)context.Node;
34      string attrName = attributeSyntax.Name.ToString();
35      if (attrName != "StorableClass") return;
36
37      var diagnostic = Diagnostic.Create(Rule, attributeSyntax.GetLocation(), attrName);
38      context.ReportDiagnostic(diagnostic);
39    }
40  }
41}
Note: See TracBrowser for help on using the repository browser.