Skip to content

Adding analyzer to warn about JSInterop calls not wrapped in a try catch block - #67530

Merged
ilonatommy merged 29 commits into
dotnet:mainfrom
MayaKirova:mkirova/unguarded-JSInterop-analyzer
Jul 17, 2026
Merged

Adding analyzer to warn about JSInterop calls not wrapped in a try catch block#67530
ilonatommy merged 29 commits into
dotnet:mainfrom
MayaKirova:mkirova/unguarded-JSInterop-analyzer

Conversation

@MayaKirova

Copy link
Copy Markdown
Contributor

Adding analyzer to warn about JSInterop calls not wrapped in a try catch block

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

Added analyzer that shows warnning and code fixer that wraps the JSInterop calls (InvokeAsync, InvokeVoidAsync etc.) in try/catch block.

Also added some tests:

Area Test Scenario Expected Result
Analyzer Unguarded IJSRuntime extension call (InvokeVoidAsync) Warning is reported
Analyzer Unguarded IJSObjectReference extension call (InvokeVoidAsync) Warning is reported
Analyzer Unguarded IJSRuntime.InvokeAsync<T> call Warning is reported
Analyzer Unguarded IJSObjectReference.InvokeAsync<T> call Warning is reported
Analyzer Unguarded IJSInProcessRuntime.Invoke<T> call Warning is reported
Analyzer Unguarded IJSInProcessObjectReference.Invoke<T> call Warning is reported
Analyzer JS interop call already inside try/catch No diagnostic is reported
Analyzer JS interop call nested in guarded flow (if inside try/catch) No diagnostic is reported
Code Fixer Unguarded awaited InvokeVoidAsync Call is wrapped in try/catch
Code Fixer Unguarded awaited InvokeAsync<T> Call is wrapped in try/catch
Code Fixer Unguarded non-awaited InvokeVoidAsync Call is wrapped in try/catch

Fixes #67306

Copilot AI review requested due to automatic review settings July 1, 2026 13:15
@MayaKirova
MayaKirova requested a review from a team as a code owner July 1, 2026 13:15
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 1, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @MayaKirova. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new Components analyzer/code fix intended to warn on unguarded JS interop calls and offer an automatic “wrap in try/catch” fix, with accompanying unit tests and localized resources.

Changes:

  • Introduces JSInteropAnalyzer to report a warning for JS interop invocations not inside a try/catch.
  • Adds JSInteropCodeFixProvider to wrap the targeted invocation in a try/catch block.
  • Adds analyzer/code-fix tests plus new diagnostic descriptor + resource strings.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/Components/Analyzers/src/JSInteropAnalyzer.cs New analyzer for detecting unguarded JS interop calls.
src/Components/Analyzers/src/JSInteropCodeFixProvider.cs New code fix provider to wrap calls in try/catch.
src/Components/Analyzers/src/DiagnosticDescriptors.cs Adds BL0015 descriptor for the new analyzer.
src/Components/Analyzers/src/Resources.resx Adds localized strings for the new diagnostic and fix title.
src/Components/Analyzers/test/JSInteropAnalyzerTest.cs New tests validating diagnostics are reported/suppressed appropriately.
src/Components/Analyzers/test/JSInteropCodeFixProviderTest.cs New tests validating the code fix output.

Comment thread src/Components/Analyzers/src/Resources.resx
Comment thread src/Components/Analyzers/src/DiagnosticDescriptors.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropCodeFixProvider.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropCodeFixProvider.cs Outdated
Comment thread src/Components/Analyzers/test/JSInteropCodeFixProviderTest.cs Outdated
Comment thread src/Components/Analyzers/test/JSInteropCodeFixProviderTest.cs
Comment thread src/Components/Analyzers/test/JSInteropCodeFixProviderTest.cs

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fast pass without the fixer.

Comment thread src/Components/Analyzers/src/Resources.resx Outdated
Comment thread src/Components/Analyzers/src/DiagnosticDescriptors.cs
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
@ilonatommy ilonatommy added area-blazor Includes: Blazor, Razor Components analyzer Indicates an issue which is related to analyzer experience labels Jul 1, 2026
@oroztocil

Copy link
Copy Markdown
Member

There are additional interop methods on these interfaces since .NET 10 (e.g., IJSRuntime.GetValueAsync). They should probably be covered by the analyzer as well, since they have the same risks.

Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropCodeFixProvider.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropCodeFixProvider.cs Outdated
Comment thread src/Components/Analyzers/src/JSInteropAnalyzer.cs Outdated
@MayaKirova

Copy link
Copy Markdown
Contributor Author

There are additional interop methods on these interfaces since .NET 10 (e.g., IJSRuntime.GetValueAsync). They should probably be covered by the analyzer as well, since they have the same risks.

@oroztocil

Those are covered I believe. I also added some tests: ff91d9e

Let me know if I'm missing anything else. :)

@MayaKirova
MayaKirova requested a review from ilonatommy July 7, 2026 06:23
MayaKirova and others added 3 commits July 7, 2026 12:58
Co-authored-by: Youssef Fahmy <youssefvictor00@gmail.com>
…eption, JSDisconnectedException or generic Exception."

This reverts commit 48fa81a.
… be different types of exceptions thrown."

This reverts commit 218e570.
Comment thread src/Components/Analyzers/src/DiagnosticDescriptors.cs Outdated
MayaKirova and others added 3 commits July 7, 2026 14:44
Co-authored-by: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com>
Comment thread src/Components/Analyzers/test/JSInteropCodeFixProviderTest.cs Outdated
Comment thread src/Components/Analyzers/src/Resources.resx Outdated
Co-authored-by: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com>
Comment thread src/Components/Analyzers/src/JSInteropCodeFixProvider.cs Outdated

@ilonatommy ilonatommy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

An idea for simplification, not tested

Comment thread src/Components/Analyzers/src/JSInteropCodeFixProvider.cs
Comment thread src/Components/Analyzers/src/JSInteropCodeFixProvider.cs
@ilonatommy
ilonatommy enabled auto-merge (squash) July 16, 2026 07:01
@ilonatommy

Copy link
Copy Markdown
Member

/ba-g failures not connected

@ilonatommy
ilonatommy merged commit f95205d into dotnet:main Jul 17, 2026
26 of 27 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

analyzer Indicates an issue which is related to analyzer experience area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] Analyzer to warn about JSInterop calls not wrapped in a try catch block

5 participants