Skip to content

Commit

Permalink
Apply feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed May 8, 2023
1 parent fc8992d commit bbf1ebe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
Document document = context.Document;
SyntaxNode root = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
SyntaxNode node = root.FindNode(context.Span);
SemanticModel model = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

string title = MicrosoftNetCoreAnalyzersResources.AvoidConstArraysCodeFixTitle;
context.RegisterCodeFix(CodeAction.Create(
title,
async ct => await ExtractConstArrayAsync(document, root, node, model, context.Diagnostics.First().Properties, ct).ConfigureAwait(false),
equivalenceKey: title),
MicrosoftNetCoreAnalyzersResources.AvoidConstArraysCodeFixTitle,
async ct => await ExtractConstArrayAsync(document, root, node, context.Diagnostics[0].Properties, ct).ConfigureAwait(false),
equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.AvoidConstArraysCodeFixTitle)),
context.Diagnostics);
}

private static async Task<Document> ExtractConstArrayAsync(Document document, SyntaxNode root, SyntaxNode node,
SemanticModel model, ImmutableDictionary<string, string?> properties, CancellationToken cancellationToken)
ImmutableDictionary<string, string?> properties, CancellationToken cancellationToken)
{
SemanticModel model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
SyntaxGenerator generator = editor.Generator;
IArrayCreationOperation arrayArgument = GetArrayCreationOperation(node, model, cancellationToken, out bool isInvoked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override void Initialize(AnalysisContext context)
argumentOperation = invocationOperation.Arguments.FirstOrDefault();
if (argumentOperation is not null)
{
if (argumentOperation.Children.First() is not IConversionOperation conversionOperation
if (argumentOperation.Value is not IConversionOperation conversionOperation
|| conversionOperation.Operand is not IArrayCreationOperation arrayCreation)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,44 @@ public void B()
");
}

[Fact]
public async Task IdentifyConstArrays_LambdaArrayCreationTwoParams()
{
await VerifyCS.VerifyCodeFixAsync(@"
using System;
using System.Linq;
namespace Z
{
public class A
{
public void B()
{
var x = new string[] { ""a"", ""b"" };
var y = x.Select((z1, z2) => {|CA1861:new[] { ""c"" }|});
}
}
}
", @"
using System;
using System.Linq;
namespace Z
{
public class A
{
private static readonly string[] selector = new[] { ""c"" };
public void B()
{
var x = new string[] { ""a"", ""b"" };
var y = x.Select((z1, z2) => selector);
}
}
}
");
}

[Fact]
public async Task IdentifyConstArrays_LambdaInvokedArrayCreation()
{
Expand Down

0 comments on commit bbf1ebe

Please sign in to comment.