1 | using System.Collections.Immutable;
|
---|
2 | using Microsoft.CodeAnalysis;
|
---|
3 | using Microsoft.CodeAnalysis.CSharp;
|
---|
4 | using Microsoft.CodeAnalysis.CSharp.Syntax;
|
---|
5 | using Microsoft.CodeAnalysis.Diagnostics;
|
---|
6 |
|
---|
7 | namespace 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 | }
|
---|