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 ObsoleteStorableConstructorAnalyzer : DiagnosticAnalyzer {
|
---|
9 | public const string DiagnosticId = "ObsoleteStorableConstructor";
|
---|
10 |
|
---|
11 | private static readonly LocalizableString Title = new LocalizableResourceString(
|
---|
12 | nameof(Resources.ObsoleteStorableConstructorAnalyzerTitle), Resources.ResourceManager, typeof(Resources));
|
---|
13 |
|
---|
14 | private static readonly LocalizableString MessageFormat = new LocalizableResourceString(
|
---|
15 | nameof(Resources.ObsoleteStorableConstructorAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources));
|
---|
16 |
|
---|
17 | private static readonly LocalizableString Description = new LocalizableResourceString(
|
---|
18 | nameof(Resources.ObsoleteStorableConstructorAnalyzerDescription), Resources.ResourceManager, typeof(Resources));
|
---|
19 |
|
---|
20 | private const string Category = nameof(DiagnosticCategory.Persistence);
|
---|
21 |
|
---|
22 | private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
|
---|
23 | DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);
|
---|
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
|
---|
35 | || namedTypeSymbol.TypeKind == TypeKind.Enum
|
---|
36 | || namedTypeSymbol.TypeKind == TypeKind.Interface) return;
|
---|
37 |
|
---|
38 | foreach (var ctor in namedTypeSymbol.Constructors) {
|
---|
39 | var attr = ctor.GetAttributes();
|
---|
40 | if (attr.Any(x => x.AttributeClass.Name == "StorableConstructorAttribute")) {
|
---|
41 | var @params = ctor.Parameters;
|
---|
42 | if (@params.Length != 1 || @params[0].Type.SpecialType == SpecialType.System_Boolean) {
|
---|
43 | var diagnostic = Diagnostic.Create(Rule, ctor.Locations[0], namedTypeSymbol.Name);
|
---|
44 | context.ReportDiagnostic(diagnostic);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|