Skip to content

Commit

Permalink
Add extension method to create Diagnostic (#126)
Browse files Browse the repository at this point in the history
Borrowing a technique from the roslyn-analyzers repo. Creating a set of
extension methods to create a Diagnostic given a `SyntaxNode` or a
`Location`. Simplifies reading the code slightly.
  • Loading branch information
MattKotsenas authored Jun 25, 2024
1 parent 2dcad69 commit 9b41015
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void Analyze(OperationAnalysisContext context, ImmutableArray<IMe
NameSyntax? memberName = context.Operation.Syntax.DescendantNodes().OfType<MemberAccessExpressionSyntax>().Select(mae => mae.Name).DefaultIfNotSingle();
Location location = memberName?.GetLocation() ?? invocationOperation.Syntax.GetLocation();

context.ReportDiagnostic(Diagnostic.Create(Rule, location));
context.ReportDiagnostic(location.CreateDiagnostic(Rule));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)

if (!string.Equals(mockedMethodTypeName, lambdaParameterTypeName, StringComparison.Ordinal))
{
Diagnostic diagnostic = Diagnostic.Create(Rule, callbackLambda.ParameterList.GetLocation());
Diagnostic diagnostic = callbackLambda.ParameterList.CreateDiagnostic(Rule);
context.ReportDiagnostic(diagnostic);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static void AnalyzeConcrete(
if (constructorArguments != null
&& IsConstructorMismatch(context, objectCreation, genericName, constructorArguments))
{
Diagnostic diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation());
Diagnostic diagnostic = objectCreation.ArgumentList.CreateDiagnostic(Rule);
context.ReportDiagnostic(diagnostic);
}
}
Expand Down Expand Up @@ -159,7 +159,7 @@ private static void AnalyzeAbstract(

Debug.Assert(objectCreation.ArgumentList != null, "objectCreation.ArgumentList != null");

Diagnostic diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation());
Diagnostic diagnostic = objectCreation.ArgumentList.CreateDiagnostic(Rule);
context.ReportDiagnostic(diagnostic);
}

Expand Down
22 changes: 22 additions & 0 deletions src/Moq.Analyzers/DiagnosticExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Moq.Analyzers;

internal static class DiagnosticExtensions
{
/// <summary>
/// Create a <see cref="Diagnostic"/> with the given <paramref name="descriptor"/> at the <paramref name="node"/>'s location.
/// </summary>
/// <param name="node">The <see cref="SyntaxNode"/> to use for a <see cref="Location"/> for a diagnostic.</param>
/// <param name="descriptor">The <see cref="DiagnosticDescriptor"/> to use when creating the diagnostic.</param>
/// <returns>A <see cref="Diagnostic"/> with the given <paramref name="descriptor"/> at the <paramref name="node"/>'s location.</returns>
public static Diagnostic CreateDiagnostic(this SyntaxNode? node, DiagnosticDescriptor descriptor) =>
Diagnostic.Create(descriptor, node?.GetLocation());

/// <summary>
/// Create a <see cref="Diagnostic"/> with the given <paramref name="descriptor"/> at the <paramref name="location"/>.
/// </summary>
/// <param name="location">The <see cref="Location"/> to use for a diagnostic.</param>
/// <param name="descriptor">The <see cref="DiagnosticDescriptor"/> to use when creating the diagnostic.</param>
/// <returns>A <see cref="Diagnostic"/> with the given <paramref name="descriptor"/> at the <paramref name="location"/>.</returns>
public static Diagnostic CreateDiagnostic(this Location? location, DiagnosticDescriptor descriptor) =>
Diagnostic.Create(descriptor, location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
{
Debug.Assert(objectCreation.ArgumentList != null, "objectCreation.ArgumentList != null");

Diagnostic diagnostic = Diagnostic.Create(Rule, objectCreation.ArgumentList?.GetLocation());
Diagnostic diagnostic = objectCreation.ArgumentList.CreateDiagnostic(Rule);
context.ReportDiagnostic(diagnostic);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
ISymbol? mockedMethodSymbol = context.SemanticModel.GetSymbolInfo(mockedMethodCall, context.CancellationToken).Symbol;
if (mockedMethodSymbol == null) return;

Diagnostic diagnostic = Diagnostic.Create(Rule, mockedMethodCall.GetLocation());
Diagnostic diagnostic = mockedMethodCall.CreateDiagnostic(Rule);
context.ReportDiagnostic(diagnostic);
}
}

0 comments on commit 9b41015

Please sign in to comment.