CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
IsOperationMethodPublicAnalyzer.cs
Go to the documentation of this file.
1using Microsoft.CodeAnalysis;
2using Microsoft.CodeAnalysis.CSharp;
3using Microsoft.CodeAnalysis.CSharp.Syntax;
4using Microsoft.CodeAnalysis.Diagnostics;
5using System.Collections.Generic;
6using System.Collections.Immutable;
9
10namespace Csla.Analyzers
11{
12 [DiagnosticAnalyzer(LanguageNames.CSharp)]
14 : DiagnosticAnalyzer
15 {
16 private static readonly DiagnosticDescriptor makeNonPublicRule =
17 new DiagnosticDescriptor(
18 Constants.AnalyzerIdentifiers.IsOperationMethodPublic, IsOperationMethodPublicAnalyzerConstants.Title,
19 IsOperationMethodPublicAnalyzerConstants.Message, Constants.Categories.Design,
20 DiagnosticSeverity.Warning, true,
21 helpLinkUri: HelpUrlBuilder.Build(
22 Constants.AnalyzerIdentifiers.IsOperationMethodPublic, nameof(IsOperationMethodPublicAnalyzer)));
23
24 private static readonly DiagnosticDescriptor makeNonPublicForInterfaceRule =
25 new DiagnosticDescriptor(
26 Constants.AnalyzerIdentifiers.IsOperationMethodPublicForInterface, IsOperationMethodPublicAnalyzerConstants.Title,
27 IsOperationMethodPublicAnalyzerConstants.Message, Constants.Categories.Design,
28 DiagnosticSeverity.Warning, true,
29 helpLinkUri: HelpUrlBuilder.Build(
30 Constants.AnalyzerIdentifiers.IsOperationMethodPublicForInterface, nameof(IsOperationMethodPublicAnalyzer)));
31
32 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
33 ImmutableArray.Create(makeNonPublicRule, makeNonPublicForInterfaceRule);
34
35 public override void Initialize(AnalysisContext context)
36 {
37 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
38 context.EnableConcurrentExecution();
39 context.RegisterSyntaxNodeAction(AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration);
40 }
41
42 private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
43 {
44 var methodNode = (MethodDeclarationSyntax)context.Node;
45 var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
46 var typeSymbol = methodSymbol.ContainingType;
47
48 if (typeSymbol.IsStereotype() && methodSymbol.IsDataPortalOperation() &&
49 methodSymbol.DeclaredAccessibility == Accessibility.Public)
50 {
51 if(typeSymbol.TypeKind == TypeKind.Interface)
52 {
53 context.ReportDiagnostic(Diagnostic.Create(
54 makeNonPublicForInterfaceRule, methodNode.Identifier.GetLocation()));
55 }
56 else
57 {
58 var properties = new Dictionary<string, string>()
59 {
60 [IsOperationMethodPublicAnalyzerConstants.IsSealed] = typeSymbol.IsSealed.ToString()
61 }.ToImmutableDictionary();
62
63 context.ReportDiagnostic(Diagnostic.Create(makeNonPublicRule,
64 methodNode.Identifier.GetLocation(), properties));
65 }
66 }
67 }
68 }
69}
override ImmutableArray< DiagnosticDescriptor > SupportedDiagnostics