-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
base: main
Are you sure you want to change the base?
Conversation
} | ||
"; | ||
|
||
var fixedSource = @" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CyrusNajmabadi wonder if you have any insight into making the formatting a bit nicer here? See fixed code example below not having 'nice' indentation like the original code above.
} | ||
|
||
private static readonly HashSet<string> WebHostBuilderMethods = new HashSet<string> | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded list because we don't have an easy way to look at chained methods and see if they implement the pattern IWebHostBuilder MethodName(this IWebHostBuilder, ...)
Should we include all of the methods from
https://github.com/search?q=repo%3Adotnet%2Faspnetcore%20%22this%20iwebhostbuilder%22&type=code
?
src/Framework/AspNetCoreAnalyzers/src/Analyzers/DiagnosticDescriptors.cs
Outdated
Show resolved
Hide resolved
...Analyzers/WebApplicationBuilder/UseCreateHostBuilderInsteadOfCreateWebHostBuilderAnalyzer.cs
Outdated
Show resolved
Hide resolved
|
||
private static bool IsWebHostBuilderType(TypeSyntax typeSyntax) | ||
{ | ||
return typeSyntax.ToString().Contains("IWebHostBuilder"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Equals
instead of Contains
to avoid detecting non-related types. We do a check in the analyzer for the exact return type but it might help to harden it here too.
.../AspNetCoreAnalyzers/src/CodeFixes/UseCreateHostBuilderInsteadOfCreateWebHostBuilderFixer.cs
Outdated
Show resolved
Hide resolved
.../AspNetCoreAnalyzers/src/CodeFixes/UseCreateHostBuilderInsteadOfCreateWebHostBuilderFixer.cs
Outdated
Show resolved
Hide resolved
{ | ||
// Create a placeholder for the Host.CreateDefaultBuilder().ConfigureWebHostDefaults(...) call | ||
// This will be replaced later, but we need something to chain the remaining methods to | ||
var placeholder = SyntaxFactory.IdentifierName("HOST_PLACEHOLDER"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern feels slightly fragile (maybe less so if we null check above) but I dunno if there is a better way to do the swarp here... 🤔
No description provided.