Repository navigation
Expand file tree
/
Copy pathEntityVariableNameAnalyzer.cs
More file actions
133 lines (110 loc) · 5.1 KB
/
Copy pathEntityVariableNameAnalyzer.cs
File metadata and controls
133 lines (110 loc) · 5.1 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace MagicCSharp.Analyzers;
/// <summary>MCS0019: a variable holding one of the codebase's own types is named after the type.</summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class EntityVariableNameAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "MCS0019";
private const string UseCaseSuffix = "UseCase";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, "Entity variable must carry the full type-derived name",
"Variable '{0}' holds a '{1}'; name it '{2}'", "Naming", DiagnosticSeverity.Error, true,
"A variable created with 'new' or declared by 'foreach' over a class or struct from this codebase or " +
"from MagicCSharp is named after its type ('var leaseEdit = new LeaseEdit()'). A qualifier in front of the full type name is allowed " +
"when two of them share a scope ('previousLeaseEdit'). A use case is named for its operation, dropping the 'UseCase' suffix as " +
"MCS0007 does for dependencies ('var signLease = new SignLeaseUseCase(...)').");
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeForEach, SyntaxKind.ForEachStatement);
context.RegisterSyntaxNodeAction(AnalyzeVariableDeclarator, SyntaxKind.VariableDeclarator);
}
private static void AnalyzeForEach(SyntaxNodeAnalysisContext context)
{
var forEachStatement = (ForEachStatementSyntax)context.Node;
var local = context.SemanticModel.GetDeclaredSymbol(forEachStatement, context.CancellationToken);
if (local == null)
{
return;
}
Check(context, forEachStatement.Identifier, local.Type);
}
private static void AnalyzeVariableDeclarator(SyntaxNodeAnalysisContext context)
{
var declarator = (VariableDeclaratorSyntax)context.Node;
if (declarator.Initializer == null)
{
return;
}
var declaration = declarator.Parent as VariableDeclarationSyntax;
if (declaration == null || !declaration.Type.IsVar)
{
return;
}
var value = declarator.Initializer.Value;
var isObjectCreation = value.IsKind(SyntaxKind.ObjectCreationExpression) || value.IsKind(SyntaxKind.ImplicitObjectCreationExpression);
if (!isObjectCreation)
{
return;
}
var local = context.SemanticModel.GetDeclaredSymbol(declarator, context.CancellationToken) as ILocalSymbol;
if (local == null)
{
return;
}
Check(context, declarator.Identifier, local.Type);
}
private static void Check(SyntaxNodeAnalysisContext context, SyntaxToken identifier, ITypeSymbol type)
{
var namedType = type as INamedTypeSymbol;
if (namedType == null || namedType.IsGenericType)
{
return;
}
if (namedType.TypeKind != TypeKind.Class && namedType.TypeKind != TypeKind.Struct)
{
return;
}
if (!SymbolFacts.IsFirstParty(namedType, context.Compilation))
{
return;
}
var name = identifier.ValueText;
var expectedTypeName = ExpectedTypeName(namedType, context.Compilation);
if (IsTypeDerivedName(name, expectedTypeName))
{
return;
}
context.ReportDiagnostic(Diagnostic.Create(Rule, identifier.GetLocation(), name, namedType.Name,
IdentifierWords.ToCamelCase(expectedTypeName)));
}
/// <summary>
/// The name a variable holding <paramref name="namedType" /> is built from: the type's own name, or — for a use
/// case — the operation it performs, so a local reads the same as the dependency MCS0007 names.
/// </summary>
private static string ExpectedTypeName(INamedTypeSymbol namedType, Compilation compilation)
{
var magicUseCase = compilation.GetTypeByMetadataName(KnownTypeNames.MagicUseCase);
var isUseCase = SymbolFacts.Implements(namedType, magicUseCase);
var hasSuffix = namedType.Name.Length > UseCaseSuffix.Length && namedType.Name.EndsWith(UseCaseSuffix, StringComparison.Ordinal);
if (isUseCase && hasSuffix)
{
return namedType.Name.Substring(0, namedType.Name.Length - UseCaseSuffix.Length);
}
return namedType.Name;
}
private static bool IsTypeDerivedName(string name, string typeName)
{
if (name == IdentifierWords.ToCamelCase(typeName))
{
return true;
}
var hasQualifier = name.Length > typeName.Length && char.IsLower(name[0]);
return hasQualifier && name.EndsWith(typeName, StringComparison.Ordinal);
}
}