5using System.Collections.Generic;
6using System.Collections.Immutable;
11 [DiagnosticAnalyzer(LanguageNames.CSharp)]
15 private static readonly DiagnosticDescriptor publicNoArgumentConstructorIsMissingRule =
16 new DiagnosticDescriptor(
17 Constants.AnalyzerIdentifiers.PublicNoArgumentConstructorIsMissing, PublicNoArgumentConstructorIsMissingConstants.Title,
18 PublicNoArgumentConstructorIsMissingConstants.Message, Constants.Categories.Usage,
19 DiagnosticSeverity.Error,
true,
20 helpLinkUri: HelpUrlBuilder.Build(
22 private static readonly DiagnosticDescriptor constructorHasParametersRule =
23 new DiagnosticDescriptor(
24 Constants.AnalyzerIdentifiers.ConstructorHasParameters, ConstructorHasParametersConstants.Title,
25 ConstructorHasParametersConstants.Message, Constants.Categories.Usage,
26 DiagnosticSeverity.Warning,
true,
27 helpLinkUri: HelpUrlBuilder.Build(
31 ImmutableArray.Create(
32 publicNoArgumentConstructorIsMissingRule,
33 constructorHasParametersRule);
37 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
38 context.EnableConcurrentExecution();
39 context.RegisterSyntaxNodeAction(AnalyzeClassDeclaration, SyntaxKind.ClassDeclaration);
42 private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
44 var hasPublicNoArgumentConstructor =
false;
45 var hasNonPublicNoArgumentConstructor =
false;
46 var classNode = (ClassDeclarationSyntax)context.Node;
47 var classSymbol = context.SemanticModel.GetDeclaredSymbol(classNode);
49 if (classSymbol.IsStereotype() && !(classSymbol?.IsAbstract).Value)
51 foreach (var constructor
in classSymbol?.Constructors)
53 if (!constructor.IsStatic)
55 if (constructor.DeclaredAccessibility == Accessibility.Public)
57 if (constructor.Parameters.Length == 0)
59 hasPublicNoArgumentConstructor =
true;
61 else if(classSymbol.IsEditableStereotype())
63 foreach (var location
in constructor.Locations)
65 context.ReportDiagnostic(Diagnostic.Create(
66 constructorHasParametersRule, location));
70 else if (constructor.Parameters.Length == 0)
72 hasNonPublicNoArgumentConstructor =
true;
77 if (!hasPublicNoArgumentConstructor)
79 var properties =
new Dictionary<string, string>()
81 [PublicNoArgumentConstructorIsMissingConstants.HasNonPublicNoArgumentConstructor] = hasNonPublicNoArgumentConstructor.ToString()
82 }.ToImmutableDictionary();
84 context.ReportDiagnostic(Diagnostic.Create(
85 publicNoArgumentConstructorIsMissingRule,
86 classNode.Identifier.GetLocation(), properties));
override ImmutableArray< DiagnosticDescriptor > SupportedDiagnostics
override void Initialize(AnalysisContext context)