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.
DoesOperationHaveAttributeAnalyzer.cs
Go to the documentation of this file.
2using Microsoft.CodeAnalysis;
3using Microsoft.CodeAnalysis.CSharp;
4using Microsoft.CodeAnalysis.CSharp.Syntax;
5using Microsoft.CodeAnalysis.Diagnostics;
6using System.Collections.Immutable;
7
8namespace Csla.Analyzers
9{
10 [DiagnosticAnalyzer(LanguageNames.CSharp)]
12 : DiagnosticAnalyzer
13 {
14 private static readonly DiagnosticDescriptor operationAttributeRule =
15 new DiagnosticDescriptor(
16 Constants.AnalyzerIdentifiers.DoesOperationHaveAttribute, DoesOperationHaveAttributeAnalyzerConstants.Title,
17 DoesOperationHaveAttributeAnalyzerConstants.Message, Constants.Categories.Usage,
18 DiagnosticSeverity.Info, true,
19 helpLinkUri: HelpUrlBuilder.Build(
20 Constants.AnalyzerIdentifiers.DoesOperationHaveAttribute, nameof(DoesOperationHaveAttributeAnalyzer)));
21
22 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(operationAttributeRule);
23
24 public override void Initialize(AnalysisContext context)
25 {
26 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
27 context.EnableConcurrentExecution();
28 context.RegisterSyntaxNodeAction(AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration);
29 }
30
31 private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
32 {
33 var methodNode = (MethodDeclarationSyntax)context.Node;
34
35 var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
36 var typeSymbol = methodSymbol.ContainingType;
37
38 if (typeSymbol.IsStereotype())
39 {
40 var qualification = methodSymbol.IsDataPortalOperation();
41
42 if(qualification.ByNamingConvention && !qualification.ByAttribute)
43 {
44 context.ReportDiagnostic(Diagnostic.Create(
45 operationAttributeRule, methodSymbol.Locations[0]));
46 }
47 }
48 }
49 }
50}
override ImmutableArray< DiagnosticDescriptor > SupportedDiagnostics