-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValueObjectDefaultExpressionAnalyzer.cs
More file actions
63 lines (51 loc) · 2.76 KB
/
ValueObjectDefaultExpressionAnalyzer.cs
File metadata and controls
63 lines (51 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Architect.DomainModeling.Analyzer.Analyzers;
/// <summary>
/// Prevents the use of default expressions and literals on struct ValueObject and WrapperValueObject types, so that validation cannot be circumvented.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class ValueObjectDefaultExpressionAnalyzer : DiagnosticAnalyzer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", Justification = "False positive.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("MicrosoftCodeAnalysisReleaseTracking", "RS2008:Enable analyzer release tracking", Justification = "Not yet implemented.")]
private static readonly DiagnosticDescriptor DiagnosticDescriptor = new DiagnosticDescriptor(
id: "ValueObjectDefaultExpression",
title: "Default expression instantiating unvalidated value object",
messageFormat: "A 'default' expression would create an unvalidated instance of value object {0}. Use a parameterized constructor, or use IsDefault() to merely compare.",
category: "Design",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [DiagnosticDescriptor];
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.RegisterSyntaxNodeAction(AnalyzeDefaultExpressionOrLiteral,
SyntaxKind.DefaultExpression,
SyntaxKind.DefaultLiteralExpression);
}
private static void AnalyzeDefaultExpressionOrLiteral(SyntaxNodeAnalysisContext context)
{
var defaultExpressionOrLiteral = (ExpressionSyntax)context.Node;
var typeInfo = context.SemanticModel.GetTypeInfo(defaultExpressionOrLiteral, context.CancellationToken);
if (typeInfo.Type is not { } type)
return;
// Only for structs
if (!type.IsValueType)
return;
// Only with WrapperValueObjectAttribute<TValue>
if (!type.GetAttributes().Any(attr => attr.AttributeClass is
{ Arity: 0, Name: "ValueObjectAttribute", ContainingNamespace: { Name: "DomainModeling", ContainingNamespace: { Name: "Architect", ContainingNamespace.IsGlobalNamespace: true, } }, } or
{ Arity: 1, Name: "WrapperValueObjectAttribute", ContainingNamespace: { Name: "DomainModeling", ContainingNamespace: { Name: "Architect", ContainingNamespace.IsGlobalNamespace: true, } }, }))
return;
var diagnostic = Diagnostic.Create(
DiagnosticDescriptor,
context.Node.GetLocation(),
type.Name);
context.ReportDiagnostic(diagnostic);
}
}