-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code fix suggestion for correct registration in ASP.NET core integrat…
…ion (#1992)
- Loading branch information
1 parent
54dbe73
commit 8238eda
Showing
3 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...ker.Extensions.Http.AspNetCore.Analyzers/CodeFixForRegistrationInASPNetCoreIntegration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using System.Collections.Immutable; | ||
using System.Threading.Tasks; | ||
using System.Composition; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System.Threading; | ||
using System.Linq; | ||
|
||
namespace Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore | ||
{ | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CodeFixForRegistrationInASPNetCoreIntegration)), Shared] | ||
public sealed class CodeFixForRegistrationInASPNetCoreIntegration : CodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => | ||
ImmutableArray.Create(DiagnosticDescriptors.CorrectRegistrationExpectedInAspNetIntegration.Id); | ||
|
||
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
||
private const string ExpectedRegistrationMethod = "ConfigureFunctionsWebApplication"; | ||
|
||
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
Diagnostic diagnostic = context.Diagnostics.First(); | ||
context.RegisterCodeFix(new ChangeConfigurationForASPNetIntegration(context.Document, diagnostic), diagnostic); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// CodeAction implementation which fixes the method configuration for ASP.NET Core Integration. | ||
/// </summary> | ||
private sealed class ChangeConfigurationForASPNetIntegration : CodeAction | ||
{ | ||
private readonly Document _document; | ||
private readonly Diagnostic _diagnostic; | ||
|
||
internal ChangeConfigurationForASPNetIntegration(Document document, Diagnostic diagnostic) | ||
{ | ||
this._document = document; | ||
this._diagnostic = diagnostic; | ||
} | ||
|
||
public override string Title => "Change configuration for ASP.Net Core Integration"; | ||
|
||
public override string EquivalenceKey => null; | ||
|
||
/// <summary> | ||
/// Returns an updated Document with correct method configuration for ASP.NET Core Integration. | ||
/// </summary> | ||
protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken) | ||
{ | ||
SyntaxNode root = await _document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
var currentNode = root.FindNode(this._diagnostic.Location.SourceSpan).FirstAncestorOrSelf<IdentifierNameSyntax>(); | ||
|
||
var newNode = currentNode.ReplaceNode(currentNode, SyntaxFactory.IdentifierName(ExpectedRegistrationMethod)); | ||
|
||
SyntaxNode newSyntaxRoot = root.ReplaceNode(currentNode, newNode); | ||
|
||
return _document.WithSyntaxRoot(newSyntaxRoot); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters