Skip to content

Commit

Permalink
Fix xUnit1051 move argument list creation inside of change document (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
campersau authored Sep 28, 2024
1 parent 98d4cd7 commit cdaa559
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/xunit.analyzers.fixes/X1000/UseCancellationTokenFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
if (root.FindNode(diagnostic.Location.SourceSpan) is not InvocationExpressionSyntax invocation)
return;

var arguments = new List<ArgumentSyntax>(invocation.ArgumentList.Arguments);
var arguments = invocation.ArgumentList.Arguments;

for (var argumentIndex = 0; argumentIndex < arguments.Count; argumentIndex++)
{
Expand All @@ -76,24 +76,26 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
"CancellationToken"
);
if (parameterIndex < arguments.Count)
var args = new List<ArgumentSyntax>(arguments);
if (parameterIndex < args.Count)
{
arguments[parameterIndex] = arguments[parameterIndex].WithExpression(testContextCancellationTokenExpression);
args[parameterIndex] = args[parameterIndex].WithExpression(testContextCancellationTokenExpression);
}
else
{
var argument = Argument(testContextCancellationTokenExpression);
if (parameterIndex > arguments.Count || arguments.Any(arg => arg.NameColon is not null))
if (parameterIndex > args.Count || args.Any(arg => arg.NameColon is not null))
{
argument = argument.WithNameColon(NameColon(parameterName));
}
arguments.Add(argument);
args.Add(argument);
}
editor.ReplaceNode(
invocation,
invocation
.WithArgumentList(ArgumentList(SeparatedList(arguments)))
.WithArgumentList(ArgumentList(SeparatedList(args)))
);
return editor.GetChangedDocument();
Expand Down

0 comments on commit cdaa559

Please sign in to comment.