Skip to content

Commit 6d083a0

Browse files
authored
Address more feedback from review
1 parent 3927aae commit 6d083a0

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

src/Framework/AspNetCoreAnalyzers/src/Analyzers/Microsoft.AspNetCore.App.Analyzers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="All" />
13+
<Reference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="All" />
1414

1515
<InternalsVisibleTo Include="Microsoft.AspNetCore.App.Analyzers.Test" />
1616
<InternalsVisibleTo Include="Microsoft.AspNetCore.App.CodeFixes" />

src/Framework/AspNetCoreAnalyzers/src/CodeFixes/DetectMismatchedParameterOptionalityFixer.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,29 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
2727
{
2828
context.RegisterCodeFix(
2929
CodeAction.Create("Fix mismatched route parameter and argument optionality",
30-
cancellationToken => FixMismatchedParameterOptionality(context, cancellationToken),
30+
cancellationToken => FixMismatchedParameterOptionality(diagnostic, context.Document, cancellationToken),
3131
equivalenceKey: DiagnosticDescriptors.DetectMismatchedParameterOptionality.Id),
3232
diagnostic);
3333
}
3434

3535
return Task.CompletedTask;
3636
}
3737

38-
private static async Task<Document> FixMismatchedParameterOptionality(CodeFixContext context, CancellationToken cancellationToken)
38+
private static async Task<Document> FixMismatchedParameterOptionality(Diagnostic diagnostic, Document document, CancellationToken cancellationToken)
3939
{
40-
DocumentEditor editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken);
41-
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
40+
DocumentEditor editor = await DocumentEditor.CreateAsync(document, cancellationToken);
41+
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
4242

4343
if (root == null)
4444
{
45-
return context.Document;
46-
}
47-
48-
var diagnostic = context.Diagnostics.SingleOrDefault();
49-
50-
if (diagnostic == null)
51-
{
52-
return context.Document;
45+
return document;
5346
}
5447

5548
var param = root.FindNode(diagnostic.Location.SourceSpan);
56-
if (param != null && param is ParameterSyntax parameterSyntax)
49+
if (param is ParameterSyntax { Type: { } parameterType } parameterSyntax)
5750
{
58-
if (parameterSyntax.Type != null)
59-
{
60-
var newParam = parameterSyntax.WithType(SyntaxFactory.NullableType(parameterSyntax.Type));
61-
editor.ReplaceNode(parameterSyntax, newParam);
62-
}
51+
var newParam = parameterSyntax.WithType(SyntaxFactory.NullableType(parameterType));
52+
editor.ReplaceNode(parameterSyntax, newParam);
6353
}
6454

6555
return editor.GetChangedDocument();

0 commit comments

Comments
 (0)