Skip to content

Add build-time analyzer for invalid Virtualize spacer elements - #66968

Merged
ilonatommy merged 22 commits into
dotnet:mainfrom
Yuvan111:fix-43102-spacerelement-warn
Sep 10, 2026
Merged

ilonatommy merged 22 commits into
dotnet:mainfrom
Yuvan111:fix-43102-spacerelement-warn

Conversation

@Yuvan111

@Yuvan111 Yuvan111 commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Add build-time analyzer for invalid Virtualize spacer elements

Description

Adds the BL0019 build-time analyzer to detect invalid SpacerElement values used by Blazor Virtualize components.

Virtualize renders div spacer elements by default. A div is invalid as a direct child of restricted HTML elements such as tbody and ul. The analyzer reports a warning and identifies the required spacer element.

image

Supported Elements

tbody, thead, tfoot require SpacerElement="tr".
ul, ol require SpacerElement="li".

Implementation

  1. Adds the BL0019 diagnostic descriptor and localized resources.
  2. Analyzes generated RenderTreeBuilder operations at build time.
  3. Tracks the direct HTML parent of Virtualize.
  4. Validates explicit constant SpacerElement values.
  5. Handles Razor-generated type-inference helper methods.
  6. Avoids diagnostics when the parent or spacer value cannot be determined safely.
  7. Uses case-insensitive HTML element comparisons.
  8. Does not report diagnostics for unrestricted or component parents.

Testing

Added analyzer tests covering:

image
  1. Restricted parents with the default div spacer.
  2. Valid and invalid explicit spacer elements.
  3. Parents with multiple valid spacer options.
  4. Unrestricted and component parents.
  5. Dynamic spacer values.
  6. Razor-generated type-inference helpers.

Fixes #43102

@github-actions github-actions Bot added the area-blazor Includes: Blazor, Razor Components label Jun 2, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 2, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@kotlarmilos

Copy link
Copy Markdown
Member

I believe this should be a build-time analyzer rather than a runtime warning @skrustev Please take a look since you did something similar in #67969

@skrustev

skrustev commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

@Yuvan111 @kotlarmilos
Yeah, I agree. This can and should be done as an analyzer. The issue also states it as such. There are already some analyzers available, that you can take a look at how they are done and do something similar. Even one about the Virtualize component as well here

Copilot AI review requested due to automatic review settings August 6, 2026 04:51

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 runtime warning in the Blazor Web.JS Virtualize interop to help developers avoid invalid HTML when Virtualize is rendered under parent elements that don’t allow <div> children (notably table sections and lists).

Changes:

  • Add warnIfSpacerElementIsInvalid to emit a console.warn when the spacer tag doesn’t match expected tags for TBODY/TABLE/THEAD/TFOOT (tr) or UL/OL (li).
  • Add Jest coverage to verify warnings fire for invalid spacer usage and do not fire for valid spacer configurations.
  • Add test helpers to mock browser globals (CSS/IntersectionObserver/ResizeObserver) for deterministic unit tests.

Reviewed changes

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

File Description
src/Components/Web.JS/src/Virtualize.ts Emits a warning when Virtualize is initialized with a spacer element that’s invalid for certain parent elements.
src/Components/Web.JS/test/Virtualize.test.ts Adds unit tests validating the new warning behavior across table/list scenarios.

@Yuvan111 Yuvan111 changed the title Add warnings for incorrect spacer element usage based on parent element Add build-time analyzer for invalid Virtualize spacer elements Aug 6, 2026
@Yuvan111

Yuvan111 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@kotlarmilos , @skrustev
Thanks for the suggestions. I have updated the PR to implement this as an analyzer and removed the runtime warning approach. Please let me know if there's anything else that should be adjusted.

@Yuvan111

Copy link
Copy Markdown
Contributor Author

@PureWeen, Thank you for the detailed review. I have addressed all the identified issues.

@PureWeen

Copy link
Copy Markdown
Member

Thanks for the update. At the current head (315fe086dbfd838cd9caa2a4730fdd7e88473893), all other findings from my earlier review are resolved. One narrow diagnostic-location issue remains: a real Razor build with direct <Virtualize TItem="string" ...> lowering under <tbody> still reports BL0017 at VirtualizeCases_razor.g.cs(106,13) instead of the mapped .razor source. The identical direct-path regression expects Pages/VirtualizeTest.razor but receives Test0.cs; the other 39 Virtualize analyzer tests pass.

Could you route direct OpenComponent<Virtualize<T>> diagnostics through mapped Razor source selection and add a direct/explicit-TItem regression test? The existing mapped-location coverage exercises the inferred type-helper path only.

This comment was written by GitHub Copilot with human direction and help.

@Yuvan111

Yuvan111 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@PureWeen, Addressed by mapping direct OpenComponent<Virtualize> diagnostics to the Razor source and adding a regression test for the explicit TItem path.

@PureWeen

PureWeen commented Sep 3, 2026

Copy link
Copy Markdown
Member

@Yuvan111, the explicit-TItem diagnostic-location issue from the earlier review looks fixed at the current head. This PR is now conflicting with main, though, and the existing CI result is from the older base.

Can you resolve the conflict against main and let the checks run again? Once that is done, I would like to do one more review against the refreshed head.

Prepared with GitHub Copilot and reviewed by a human.

@Yuvan111

Yuvan111 commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

@PureWeen , Thanks for the review. I've resolved the conflicts with the latest main, updated the PR, and reran the checks on the refreshed head. Please let me know if you have any further feedback during the follow-up review.

@ilonatommy

Copy link
Copy Markdown
Member

Supported Elements

tbody, thead, tfoot require SpacerElement="tr".
ul, ol require SpacerElement="li".
tr requires SpacerElement="td" or SpacerElement="th".
select requires SpacerElement="option".

While tbody/thead/tfoot/tr/ul/ol do not accept <div> tbody content model, thead content model, tfoot content model, tr content element, ul content element, ol content element, select has no problem with <div> select content model

or div elements.

That is introducing a false positive.

What's more, writing Virtualize component into a tr is technically possible but it does not seem like a valid case for such a component. It would create a structure like:

<tr>
    <td style="height: 500px"></td>
    <!-- rendered items -->
    <td style="height: 1000px"></td>
</tr>

where each td is a cell in that row - horizontally. We don't support horizontal scrolling. And there is no useful alternative. I would remove tr from the analyzed elements as well.

@Yuvan111

Yuvan111 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

@ilonatommy, Thanks, I removed select and tr from the analyzer and updated the tests accordingly.

Comment thread docs/list-of-diagnostics.md Resolved
Comment thread docs/list-of-diagnostics.md Resolved
Comment thread docs/list-of-diagnostics.md Resolved
@ilonatommy
ilonatommy enabled auto-merge (squash) September 8, 2026 09:49
@ilonatommy

Copy link
Copy Markdown
Member

/ba-g Build analysis stuck, all the other checks green

@ilonatommy
ilonatommy merged commit ef51aa7 into dotnet:main Sep 10, 2026
32 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-reviewed-approved 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.

Consider adding an analyzer to detect when the Blazor Virtualize component is used in an element that doesn't permit child div elements

6 participants