Skip to content
Merged
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
@@ -0,0 +1,26 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;

namespace Meziantou.Analyzer.Rules;

internal sealed class NamedParameterFixAllProvider : DocumentBasedFixAllProvider
{
public static NamedParameterFixAllProvider Instance { get; } = new();

protected override string GetFixAllTitle(FixAllContext fixAllContext) => "Add parameter name";

protected override async Task<Document?> FixAllAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
{
if (diagnostics.IsEmpty)
return null;

foreach (var diagnostic in diagnostics.OrderByDescending(d => d.Location.SourceSpan.Start))
{
document = await NamedParameterFixer.AddParameterName(document, diagnostic.Location.SourceSpan, fixAllContext.CancellationToken).ConfigureAwait(false);
}

return document;
}
}
33 changes: 21 additions & 12 deletions src/Meziantou.Analyzer.CodeFixers/Rules/NamedParameterFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Text;

namespace Meziantou.Analyzer.Rules;

Expand All @@ -14,21 +15,17 @@ public sealed class NamedParameterFixer : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(RuleIdentifiers.UseNamedParameter);

public override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}
public override FixAllProvider GetFixAllProvider() => NamedParameterFixAllProvider.Instance;

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
// In case the ArrayCreationExpressionSyntax is wrapped in an ArgumentSyntax or some other node with the same span,
// get the innermost node for ties.
var nodeToFix = root?.FindNode(context.Span, getInnermostNodeForTie: true);
if (nodeToFix is null)
var diagnostic = context.Diagnostics.FirstOrDefault();
if (root is null || diagnostic is null)
return;

var argument = nodeToFix.FirstAncestorOrSelf<ArgumentSyntax>();
var argumentSpan = diagnostic.Location.SourceSpan;
var argument = FindArgument(root, argumentSpan);
if (argument is null || argument.NameColon is not null)
return;

Expand All @@ -46,18 +43,22 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var title = "Add parameter name";
var codeAction = CodeAction.Create(
title,
ct => AddParameterName(context.Document, nodeToFix, ct),
ct => AddParameterName(context.Document, argumentSpan, ct),
equivalenceKey: title);

context.RegisterCodeFix(codeAction, context.Diagnostics);
}

private static async Task<Document> AddParameterName(Document document, SyntaxNode nodeToFix, CancellationToken cancellationToken)
internal static async Task<Document> AddParameterName(Document document, TextSpan argumentSpan, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
if (root is null)
return document;

var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
var semanticModel = editor.SemanticModel;

var argument = nodeToFix.FirstAncestorOrSelf<ArgumentSyntax>();
var argument = FindArgument(root, argumentSpan);
if (argument is null || argument.NameColon is not null)
return document;

Expand All @@ -75,6 +76,14 @@ private static async Task<Document> AddParameterName(Document document, SyntaxNo
return editor.GetChangedDocument();
}

private static ArgumentSyntax? FindArgument(SyntaxNode root, TextSpan argumentSpan)
{
// In case the literal is wrapped in an ArgumentSyntax or some other node with the same span,
// get the innermost node for ties.
var nodeToFix = root.FindNode(argumentSpan, getInnermostNodeForTie: true);
return nodeToFix.FirstAncestorOrSelf<ArgumentSyntax>();
}

private static ImmutableArray<IParameterSymbol>? FindParameters(SemanticModel semanticModel, SyntaxNode? node, CancellationToken cancellationToken)
{
while (node is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,41 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task BatchFix_MultipleArgumentsInSingleInvocation()
{
const string SourceCode = """
class TypeName
{
private void InsertStatus(object reviewStatuses, object courseStatuses, object paymentInfos, object utcStatuses, object dmvStatuses)
{
}

public void Test()
{
this.InsertStatus([|null|], [|null|], [|null|], utcStatuses: null, [|null|]);
}
}
""";
const string CodeFix = """
class TypeName
{
private void InsertStatus(object reviewStatuses, object courseStatuses, object paymentInfos, object utcStatuses, object dmvStatuses)
{
}

public void Test()
{
this.InsertStatus(reviewStatuses: null, courseStatuses: null, paymentInfos: null, utcStatuses: null, dmvStatuses: null);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ShouldBatchFixCodeWith(CodeFix)
.ValidateAsync();
}

[Fact]
public async Task True_WithOptions_ShouldNotReportDiagnostic()
{
Expand Down
Loading