-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add analyzer and codefixer for WebHost.CreateDefaultBuilder to Host.CreateDefaultBuilder.ConfigureWebHostDefaults #63246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
109 changes: 109 additions & 0 deletions
109
...lyzers/WebApplicationBuilder/UseCreateHostBuilderInsteadOfCreateWebHostBuilderAnalyzer.cs
This file contains hidden or 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,109 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Linq; | ||
| using Microsoft.AspNetCore.App.Analyzers.Infrastructure; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
|
|
||
| namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class UseCreateHostBuilderInsteadOfCreateWebHostBuilderAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create( | ||
| DiagnosticDescriptors.UseCreateHostBuilderInsteadOfCreateWebHostBuilder | ||
| ); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
| context.RegisterCompilationStartAction(context => | ||
| { | ||
| var compilation = context.Compilation; | ||
| var wellKnownTypes = WellKnownTypes.GetOrCreate(compilation); | ||
|
|
||
| context.RegisterOperationAction(context => | ||
| { | ||
| var invocation = (IInvocationOperation)context.Operation; | ||
| var targetMethod = invocation.TargetMethod; | ||
|
|
||
| // Check if this is WebHost.CreateDefaultBuilder | ||
| if (IsWebHostCreateDefaultBuilderCall(targetMethod)) | ||
| { | ||
| var diagnostic = Diagnostic.Create( | ||
| DiagnosticDescriptors.UseCreateHostBuilderInsteadOfCreateWebHostBuilder, | ||
| invocation.Syntax.GetLocation() | ||
| ); | ||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
| }, OperationKind.Invocation); | ||
|
|
||
| context.RegisterSyntaxNodeAction(context => | ||
| { | ||
| var methodDeclaration = (MethodDeclarationSyntax)context.Node; | ||
| var semantic = context.SemanticModel; | ||
| var symbol = semantic.GetDeclaredSymbol(methodDeclaration); | ||
|
|
||
| // Check if this method returns IWebHostBuilder | ||
| if (symbol != null && IsWebHostBuilderReturnType(symbol)) | ||
| { | ||
| // Check if the method body contains WebHost.CreateDefaultBuilder | ||
| if (ContainsWebHostCreateDefaultBuilder(methodDeclaration)) | ||
| { | ||
| var diagnostic = Diagnostic.Create( | ||
| DiagnosticDescriptors.UseCreateHostBuilderInsteadOfCreateWebHostBuilder, | ||
| methodDeclaration.ReturnType.GetLocation() | ||
| ); | ||
| context.ReportDiagnostic(diagnostic); | ||
| } | ||
| } | ||
| }, SyntaxKind.MethodDeclaration); | ||
| }); | ||
| } | ||
|
|
||
| private static bool IsWebHostCreateDefaultBuilderCall(IMethodSymbol method) | ||
| { | ||
| // Check if this is WebHost.CreateDefaultBuilder (not other WebHost methods) | ||
| if (method.Name == "CreateDefaultBuilder" && | ||
BrennanConroy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| method.ContainingType?.Name == "WebHost" && | ||
| method.ContainingType?.ContainingNamespace?.ToDisplayString() == "Microsoft.AspNetCore") | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static bool IsWebHostBuilderReturnType(IMethodSymbol method) | ||
| { | ||
| // Check if the return type is IWebHostBuilder | ||
| var returnType = method.ReturnType; | ||
| return returnType.Name == "IWebHostBuilder" && | ||
| returnType.ContainingNamespace?.ToDisplayString() == "Microsoft.AspNetCore.Hosting"; | ||
| } | ||
|
|
||
| private static bool ContainsWebHostCreateDefaultBuilder(MethodDeclarationSyntax methodDeclaration) | ||
| { | ||
| // Check if the method contains WebHost.CreateDefaultBuilder calls | ||
| var descendants = methodDeclaration.DescendantNodes().OfType<InvocationExpressionSyntax>(); | ||
|
|
||
| foreach (var invocation in descendants) | ||
| { | ||
| if (invocation.Expression is MemberAccessExpressionSyntax memberAccess && | ||
| memberAccess.Expression is IdentifierNameSyntax identifier && | ||
| identifier.Identifier.ValueText == "WebHost" && | ||
| memberAccess.Name.Identifier.ValueText == "CreateDefaultBuilder") | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.