-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMockGetShouldNotTakeLiteralsAnalyzer.cs
More file actions
87 lines (76 loc) · 3.4 KB
/
Copy pathMockGetShouldNotTakeLiteralsAnalyzer.cs
File metadata and controls
87 lines (76 loc) · 3.4 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
using Microsoft.CodeAnalysis.Operations;
namespace Moq.Analyzers;
/// <summary>
/// Mock.Get() should not take literals.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MockGetShouldNotTakeLiteralsAnalyzer : MoqDiagnosticAnalyzerBase
{
private static readonly LocalizableString Title = "Moq: Mock.Get() should not take literals";
private static readonly LocalizableString Message = "Mock.Get() should not take literal '{0}'";
private static readonly LocalizableString Description = "Mock.Get() should not take literals.";
private static readonly DiagnosticDescriptor Rule = new(
DiagnosticIds.MockGetShouldNotTakeLiterals,
Title,
Message,
DiagnosticCategory.Usage,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: Description,
helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/{ThisAssembly.GitCommitId}/docs/rules/{DiagnosticIds.MockGetShouldNotTakeLiterals}.md");
/// <inheritdoc />
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
private protected override void RegisterCompilationActions(CompilationStartAnalysisContext context, MoqKnownSymbols knownSymbols)
{
// Look for the Mock.Get() method
ImmutableArray<IMethodSymbol> getMethods = knownSymbols.MockGet;
if (getMethods.IsEmpty)
{
return;
}
context.RegisterOperationAction(
operationAnalysisContext => Analyze(operationAnalysisContext, getMethods),
OperationKind.Invocation);
}
private static void Analyze(OperationAnalysisContext context, ImmutableArray<IMethodSymbol> wellKnownGetMethods)
{
if (context.Operation is not IInvocationOperation invocationOperation)
{
return;
}
IMethodSymbol targetMethod = invocationOperation.TargetMethod;
if (!targetMethod.IsInstanceOf(wellKnownGetMethods))
{
return;
}
// Check if any argument is a literal
foreach (IArgumentOperation argument in invocationOperation.Arguments)
{
if (IsLiteralValue(argument.Value))
{
string literalText = GetLiteralText(argument.Value);
context.ReportDiagnostic(argument.Syntax.GetLocation().CreateDiagnostic(Rule, literalText));
}
}
}
private static bool IsLiteralValue(IOperation operation)
{
return operation.Kind switch
{
OperationKind.Literal => true,
OperationKind.DefaultValue => true,
OperationKind.Conversion when operation is IConversionOperation conversionOperation => IsLiteralValue(conversionOperation.Operand),
_ => false,
};
}
private static string GetLiteralText(IOperation operation)
{
return operation.Kind switch
{
OperationKind.Literal when operation is ILiteralOperation literal => literal.ConstantValue.Value?.ToString() ?? "null",
OperationKind.DefaultValue when operation is IDefaultValueOperation defaultValue => $"default({defaultValue.Type?.ToDisplayString() ?? string.Empty})",
OperationKind.Conversion when operation is IConversionOperation conversionOperation => GetLiteralText(conversionOperation.Operand),
_ => operation.Syntax.ToString(),
};
}
}