Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ internal static async Task RegisterCodeFixAsync(
string? fixKindPropertyKey,
Func<Document, InvocationExpressionSyntax, string, string?, CancellationToken, Task<Document>> fixAssertAsync)
{
SyntaxNode root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

Diagnostic diagnostic = context.Diagnostics[0];
if (!diagnostic.Properties.TryGetValue(properAssertMethodNamePropertyKey, out string? properAssertMethodName)
|| properAssertMethodName is null)
Expand All @@ -47,6 +45,8 @@ internal static async Task RegisterCodeFixAsync(
return;
}

SyntaxNode root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

if (root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is not InvocationExpressionSyntax invocationExpr)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,14 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
AssertToAssertAnalyzerHelpers.ProperAssertMethodNameKey,
CodeFixResources.StringAssertToAssertTitle,
fixKindPropertyKey: null,
FixAssertAsync);

private static Task<Document> FixAssertAsync(
Document document,
InvocationExpressionSyntax invocationExpr,
string properAssertMethodName,
string? fixKind,
CancellationToken cancellationToken)
=> FixStringAssertAsync(document, invocationExpr, properAssertMethodName, cancellationToken);
(doc, expr, methodName, _, ct) => FixStringAssertAsync(doc, expr, methodName, ct));

private static async Task<Document> FixStringAssertAsync(
Document document,
InvocationExpressionSyntax invocationExpr,
string properAssertMethodName,
CancellationToken cancellationToken)
{
// Check if the invocation expression has a member access expression
if (invocationExpr.Expression is not MemberAccessExpressionSyntax memberAccessExpr)
{
return document;
Expand All @@ -73,8 +64,6 @@ private static async Task<Document> FixStringAssertAsync(
ArgumentListSyntax newArgumentList = invocationExpr.ArgumentList.WithArguments(SyntaxFactory.SeparatedList<ArgumentSyntax>(newArguments));
InvocationExpressionSyntax newInvocationExpr = invocationExpr.WithArgumentList(newArgumentList);

// Replace StringAssert with Assert in the member access expression
// Change StringAssert.MethodName to Assert.ProperMethodName
MemberAccessExpressionSyntax newMemberAccess = memberAccessExpr.WithExpression(SyntaxFactory.IdentifierName("Assert"))
.WithName(SyntaxFactory.IdentifierName(properAssertMethodName));
newInvocationExpr = newInvocationExpr.WithExpression(newMemberAccess);
Expand Down
Loading