-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add BL0010 analyzer: Recommend InvokeVoidAsync over InvokeAsync<object> #64623
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
Open
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/consider-razor-analyzer-recommendation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+691
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eb42a34
Initial plan
Copilot 33c4165
Add BL0010 analyzer: Recommend InvokeVoidAsync over InvokeAsync<object>
Copilot ef2960b
Address code review feedback: use symbol comparison instead of string…
Copilot 61397d9
Add nullable enable directive and test for assigned result scenario
Copilot 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
166 changes: 166 additions & 0 deletions
166
src/Components/Analyzers/src/InvokeAsyncOfObjectAnalyzer.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,166 @@ | ||
| // 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 Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Analyzers; | ||
|
|
||
| /// <summary> | ||
| /// Analyzer that detects usage of InvokeAsync<object> and recommends using InvokeVoidAsync instead. | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class InvokeAsyncOfObjectAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| private const string JSRuntimeExtensionsTypeName = "Microsoft.JSInterop.JSRuntimeExtensions"; | ||
| private const string JSObjectReferenceExtensionsTypeName = "Microsoft.JSInterop.JSObjectReferenceExtensions"; | ||
| private const string JSInProcessRuntimeExtensionsTypeName = "Microsoft.JSInterop.JSInProcessRuntimeExtensions"; | ||
| private const string JSInProcessObjectReferenceExtensionsTypeName = "Microsoft.JSInterop.JSInProcessObjectReferenceExtensions"; | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
| ImmutableArray.Create(DiagnosticDescriptors.UseInvokeVoidAsyncForObjectReturn); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.EnableConcurrentExecution(); | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
|
|
||
| context.RegisterCompilationStartAction(compilationContext => | ||
| { | ||
| // Cache type lookups once per compilation | ||
| var ijsRuntimeType = compilationContext.Compilation.GetTypeByMetadataName("Microsoft.JSInterop.IJSRuntime"); | ||
| var ijsObjectReferenceType = compilationContext.Compilation.GetTypeByMetadataName("Microsoft.JSInterop.IJSObjectReference"); | ||
| var ijsInProcessRuntimeType = compilationContext.Compilation.GetTypeByMetadataName("Microsoft.JSInterop.IJSInProcessRuntime"); | ||
| var ijsInProcessObjectReferenceType = compilationContext.Compilation.GetTypeByMetadataName("Microsoft.JSInterop.IJSInProcessObjectReference"); | ||
| var jsRuntimeExtensionsType = compilationContext.Compilation.GetTypeByMetadataName(JSRuntimeExtensionsTypeName); | ||
| var jsObjectReferenceExtensionsType = compilationContext.Compilation.GetTypeByMetadataName(JSObjectReferenceExtensionsTypeName); | ||
| var jsInProcessRuntimeExtensionsType = compilationContext.Compilation.GetTypeByMetadataName(JSInProcessRuntimeExtensionsTypeName); | ||
| var jsInProcessObjectReferenceExtensionsType = compilationContext.Compilation.GetTypeByMetadataName(JSInProcessObjectReferenceExtensionsTypeName); | ||
| var objectType = compilationContext.Compilation.GetSpecialType(SpecialType.System_Object); | ||
|
|
||
| if (ijsRuntimeType is null && ijsObjectReferenceType is null) | ||
| { | ||
| // JSInterop types are not available | ||
| return; | ||
| } | ||
|
|
||
| compilationContext.RegisterOperationAction(operationContext => | ||
| { | ||
| var invocation = (IInvocationOperation)operationContext.Operation; | ||
| var targetMethod = invocation.TargetMethod; | ||
|
|
||
| // Check if the method is named InvokeAsync and is generic | ||
| if (targetMethod.Name != "InvokeAsync" || !targetMethod.IsGenericMethod) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Check if the type argument is object | ||
| if (targetMethod.TypeArguments.Length != 1 || | ||
| !SymbolEqualityComparer.Default.Equals(targetMethod.TypeArguments[0], objectType)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Check if the method is on IJSRuntime, IJSObjectReference, or their in-process variants | ||
| // This includes extension methods on these types | ||
| var containingType = targetMethod.ContainingType; | ||
| var receiverType = GetReceiverType(invocation); | ||
|
|
||
| if (!IsJSInteropType(receiverType, ijsRuntimeType, ijsObjectReferenceType, ijsInProcessRuntimeType, ijsInProcessObjectReferenceType) && | ||
| !IsJSInteropExtensionClass(containingType, jsRuntimeExtensionsType, jsObjectReferenceExtensionsType, jsInProcessRuntimeExtensionsType, jsInProcessObjectReferenceExtensionsType)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| operationContext.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.UseInvokeVoidAsyncForObjectReturn, | ||
| invocation.Syntax.GetLocation())); | ||
| }, OperationKind.Invocation); | ||
| }); | ||
| } | ||
|
|
||
| private static ITypeSymbol? GetReceiverType(IInvocationOperation invocation) | ||
| { | ||
| // For extension methods, the first argument is the receiver | ||
| if (invocation.TargetMethod.IsExtensionMethod && invocation.Arguments.Length > 0) | ||
| { | ||
| return invocation.Arguments[0].Value.Type; | ||
| } | ||
|
|
||
| // For instance methods | ||
| return invocation.Instance?.Type; | ||
| } | ||
|
|
||
| private static bool IsJSInteropType( | ||
| ITypeSymbol? type, | ||
| INamedTypeSymbol? ijsRuntimeType, | ||
| INamedTypeSymbol? ijsObjectReferenceType, | ||
| INamedTypeSymbol? ijsInProcessRuntimeType, | ||
| INamedTypeSymbol? ijsInProcessObjectReferenceType) | ||
| { | ||
| if (type is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the type implements any of the JSInterop interfaces | ||
| if (ijsRuntimeType is not null && ImplementsInterface(type, ijsRuntimeType)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (ijsObjectReferenceType is not null && ImplementsInterface(type, ijsObjectReferenceType)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (ijsInProcessRuntimeType is not null && ImplementsInterface(type, ijsInProcessRuntimeType)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (ijsInProcessObjectReferenceType is not null && ImplementsInterface(type, ijsInProcessObjectReferenceType)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static bool ImplementsInterface(ITypeSymbol type, INamedTypeSymbol interfaceType) | ||
| { | ||
| if (SymbolEqualityComparer.Default.Equals(type, interfaceType)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| foreach (var iface in type.AllInterfaces) | ||
| { | ||
| if (SymbolEqualityComparer.Default.Equals(iface, interfaceType)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static bool IsJSInteropExtensionClass( | ||
| INamedTypeSymbol containingType, | ||
| INamedTypeSymbol? jsRuntimeExtensionsType, | ||
| INamedTypeSymbol? jsObjectReferenceExtensionsType, | ||
| INamedTypeSymbol? jsInProcessRuntimeExtensionsType, | ||
| INamedTypeSymbol? jsInProcessObjectReferenceExtensionsType) | ||
| { | ||
| // Use symbol equality comparison instead of string comparison | ||
| return SymbolEqualityComparer.Default.Equals(containingType, jsRuntimeExtensionsType) || | ||
| SymbolEqualityComparer.Default.Equals(containingType, jsObjectReferenceExtensionsType) || | ||
| SymbolEqualityComparer.Default.Equals(containingType, jsInProcessRuntimeExtensionsType) || | ||
| SymbolEqualityComparer.Default.Equals(containingType, jsInProcessObjectReferenceExtensionsType); | ||
| } | ||
| } | ||
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
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.
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.
Add
#nullable enabledirective after the namespace declaration to match the coding conventions used in other analyzers in this codebase. This ensures consistent nullable reference type handling across all analyzer files.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.
Added
#nullable enabledirective in commit 61397d9.