Repository navigation
fix(gsx): reject templ bodies with more than one top-level element - #171
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #171 +/- ##
==========================================
- Coverage 84.74% 84.73% -0.01%
==========================================
Files 169 169
Lines 21588 21603 +15
==========================================
+ Hits 18295 18306 +11
- Misses 2633 2637 +4
Partials 660 660 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
| } | ||
| ``` | ||
|
|
||
| A templ body renders exactly one root: a single element, component call, `@expr`, `for` loop, or `if` statement. Local bindings such as `label := <span>...</span>` may precede it. Anything after the root is an error, since only the first would be returned; wrap siblings in a container element instead. This applies to struct method components too, so a `<modal>` belongs inside the root `<div>` rather than after its closing tag. |
There was a problem hiding this comment.
The reference says anything after the root is rejected, but the analyzer skips non-root nodes such as bindings and Go statements regardless of their position. Those statements can still pass tui check, so users may rely on a rule that is not enforced. Either enforce the documented ordering or narrow the documentation to cover only a second root-producing node.
Knowledge Base Used: GSX language analysis and generation
Prompt To Fix With AI
This is a comment left during a code review.
Path: docs/content/reference/gsx-syntax.md
Line: 58
Comment:
**Post-root rule is overstated**
The reference says anything after the root is rejected, but the analyzer skips non-root nodes such as bindings and Go statements regardless of their position. Those statements can still pass `tui check`, so users may rely on a rule that is not enforced. Either enforce the documented ordering or narrow the documentation to cover only a second root-producing node.
**Knowledge Base Used:** [GSX language analysis and generation](https://app.greptile.com/alethi/-/custom-context/knowledge-base/grindlemire/go-tui/-/docs/gsx-language-toolchain.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
A templ body with two top-level elements compiled without complaint, but the generator only returns the first from Render and still emits the rest. A
<modal>placed after the root<div>therefore mounted, registered as an overlay and drew on screen while never joining the element tree, so its buttons never got focus and Enter, Escape, Tab and clicks went nowhere. Three guide snippets shipped in that shape andtui checkpassed all of them (#168 and #170 fixed the docs).The analyzer now walks each component body and reports an error at the second root-producing node (element, component call,
@expr,fororif), with a hint to wrap the siblings in a single container. Let bindings and Go code before the root are still fine, and a lonefor,if/elseor<modal>as the root is unchanged. The LSP surfaces the same diagnostic since it runs the analyzer. The syntax reference gains a paragraph stating the one-root rule, and the guide 06 note now says the compiler rejects the pattern. One pattern that used to generate is now an error: two sibling top-levelifblocks, where the generator reused the root variable so the last true branch won.if/elsecovers that case.Running the new check against the pre-#168 guide 09 snippet reports the modal's line:
Fixes #169