Adding analyzer to warn about JSInterop calls not wrapped in a try catch block - #67530
Conversation
|
Thanks for your PR, @MayaKirova. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
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
JSInteropAnalyzerto report a warning for JS interop invocations not inside a try/catch. - Adds
JSInteropCodeFixProviderto 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. |
ilonatommy
left a comment
There was a problem hiding this comment.
Fast pass without the fixer.
… to string type names. Add more tests accordingly.
… block with at least one catch as guarded. Adding tests accordingly.
…ncompatible code.
|
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. |
…c, InvokeConstructorAsync).
…dd some tests for calls inside lambda expressions.
Those are covered I believe. I also added some tests: ff91d9e Let me know if I'm missing anything else. :) |
Co-authored-by: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com>
….com/MayaKirova/aspnetcore into mkirova/unguarded-JSInterop-analyzer
Co-authored-by: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com>
ilonatommy
left a comment
There was a problem hiding this comment.
An idea for simplification, not tested
|
/ba-g failures not connected |
Adding analyzer to warn about JSInterop calls not wrapped in a try catch block
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:
IJSRuntimeextension call (InvokeVoidAsync)IJSObjectReferenceextension call (InvokeVoidAsync)IJSRuntime.InvokeAsync<T>callIJSObjectReference.InvokeAsync<T>callIJSInProcessRuntime.Invoke<T>callIJSInProcessObjectReference.Invoke<T>calltry/catchifinsidetry/catch)InvokeVoidAsynctry/catchInvokeAsync<T>try/catchInvokeVoidAsynctry/catchFixes #67306