Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Blazor analyzer diagnostic (BL0017) intended to warn when JavaScript interop is invoked outside OnAfterRender/OnAfterRenderAsync without first ensuring the renderer is interactive via RendererInfo.IsInteractive.
Changes:
- Added
JsInteropUsageWithoutCheckAnalyzerto detect (unguarded) JS interop usage outsideOnAfterRender*. - Added
BL0017descriptor + localized resources, and extended analyzer API constants. - Added a new analyzer test suite and documented
BL0017in the diagnostics list.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Components/Analyzers/test/JsInteropUsageWithoutCheckAnalyzerTest.cs | Adds coverage for the new analyzer’s guard-detection behavior. |
| src/Components/Analyzers/src/Resources.resx | Adds BL0017 title/format/description strings. |
| src/Components/Analyzers/src/JsInteropUsageWithoutCheckAnalyzer.cs | Implements the new analyzer logic and flow/guard tracking. |
| src/Components/Analyzers/src/DiagnosticDescriptors.cs | Registers the new BL0017 diagnostic descriptor. |
| src/Components/Analyzers/src/ComponentsApi.cs | Adds metadata constants for RendererInfo/JSInterop types. |
| docs/list-of-diagnostics.md | Documents BL0017 in the public diagnostics list. |
|
Thank you for this work! I haven't read it in details, but wouldn't this analyzer throw false positive when we have no prerendering? E.g. in the case of component having |
|
@dariatiurina Yeah, just as I was PR-ing I figured it would need to check the render mode that is for the component or if set globally. Also since WebAssembly from my understanding is only in interactive mode anyway, so will need to see how can that be checked as well. Not sure if there's some different API to check that, but I am looking into it. |
But then we also need to check for the inner component's rendermode that gets inherited from the parent component, which we, as far as I know, can't do in the analyzer. |
I think we can afford false positives in this case, building the whole component dependency tree would be an overkill. I think checking component's own settings should be enough. When setting the rules we should take into consideration that false positive is just a harmless, redundant warning. False negative, on the other hand is a real, runtime crash risk. From this reason I would just check if we're wrapped in nit: |
|
@Youssef1313 @dariatiurina @ilonatommy The only somewhat sure way to limit the cases we want to warn is to warn for OnInitialized/OnParametersSet/EventHandlers. From my understanding they are almost sure to trigger in the prerendering phases and cover the most cases that could cause issues? This sure makes things a bit more complicated though to know which methods are called where and find their implementation. Otherwise the other solution I see is to leave it as is and warn for every case(where it is not checked with |
…ion tests for JSInterop.
|
If we want to get this in .NET 11, we should either remember to backport after merging, or simply target release/11.0. |
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
…later executed without IsInteractive..
…lude `is` check as well.
|
@skrustev The earlier control-flow issue still appears to be present. Could you revisit the unsafe cases and JS calls inside conditions? |
…warning on other unrelated types.
…nt IsInteractive for them as well.
…nderTreeBuilder before inspecting its value argument.
This. ensures lambdas will be caught if assigned to parameter.
…ts. Small fix to conversion delegates missing cases.
…ut errors. Fix failing test snippets.
Add JSInterop analyzer ensuring 'IsInteractive' is checked before using it.
Description
Add analyzer that shows warning when JSInterop calls like
InvokeAsyncare done outsideOnAfterRender/OnAfterRenderAsyncwithout checking forRendererInfo.IsInteractive.Fixes #65233