Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Components/Analyzers/src/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ internal static class DiagnosticDescriptors
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: CreateLocalizableResourceString(nameof(Resources.PersistentStateShouldNotHavePropertyInitializer_Description)));

public static readonly DiagnosticDescriptor UseInvokeVoidAsyncForObjectReturn = new(
"BL0010",
CreateLocalizableResourceString(nameof(Resources.UseInvokeVoidAsyncForObjectReturn_Title)),
CreateLocalizableResourceString(nameof(Resources.UseInvokeVoidAsyncForObjectReturn_Format)),
Usage,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: CreateLocalizableResourceString(nameof(Resources.UseInvokeVoidAsyncForObjectReturn_Description)));
}
164 changes: 164 additions & 0 deletions src/Components/Analyzers/src/InvokeAsyncOfObjectAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// 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;

namespace Microsoft.AspNetCore.Components.Analyzers;

Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add #nullable enable directive 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.

Suggested change
#nullable enable

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added #nullable enable directive in commit 61397d9.

/// <summary>
/// Analyzer that detects usage of InvokeAsync&lt;object&gt; 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);
}
}
9 changes: 9 additions & 0 deletions src/Components/Analyzers/src/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,13 @@
<data name="PersistentStateShouldNotHavePropertyInitializer_Title" xml:space="preserve">
<value>Property with [PersistentState] should not have initializer</value>
</data>
<data name="UseInvokeVoidAsyncForObjectReturn_Description" xml:space="preserve">
<value>JavaScript interop calls that do not expect a return value should use InvokeVoidAsync instead of InvokeAsync&lt;object&gt;. Using InvokeAsync&lt;object&gt; may cause serialization issues with non-serializable JavaScript values.</value>
</data>
<data name="UseInvokeVoidAsyncForObjectReturn_Format" xml:space="preserve">
<value>Use 'InvokeVoidAsync' instead of 'InvokeAsync&lt;object&gt;'. Return values of type 'object' cannot be deserialized and may cause serialization errors if the JavaScript function returns a non-serializable value.</value>
</data>
<data name="UseInvokeVoidAsyncForObjectReturn_Title" xml:space="preserve">
<value>Use InvokeVoidAsync instead of InvokeAsync&lt;object&gt;</value>
</data>
</root>
Loading
Loading