Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/content/reference/gsx-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ templ UserList(users []string, maxVisible int) {
}
```

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.

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.

P2 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

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!


Pure components cannot host constructs that mount against a receiver: the component elements (`<input>`, `<textarea>`, `<modal>`, `<markdown>`), `@expr` component expressions, and `@Factory()` calls that return a component (a struct component, a `tui.Component`, or a type whose `Render` method is written in plain Go). Put those in a struct method component, which supplies the receiver they mount against. `tui generate` reports an error with a message pointing at the fix if you use one in a pure component.

### Children slot
Expand Down
25 changes: 25 additions & 0 deletions internal/tuigen/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,31 @@ func (a *Analyzer) analyzeComponent(comp *Component) {
for _, node := range comp.Body {
a.analyzeNode(node)
}

a.checkSingleRoot(comp)
}

// checkSingleRoot rejects a body with more than one root-producing node. The
// generator returns only the first, so anything after it would be built and
// dropped; a trailing <modal> would even draw as an overlay without ever
// joining the tree.
func (a *Analyzer) checkSingleRoot(comp *Component) {
seenRoot := false
for _, node := range comp.Body {
switch node.(type) {
case *Element, *ComponentCall, *ComponentExpr, *ForLoop, *IfStmt:
default:
continue
}
if !seenRoot {
seenRoot = true
continue
}
a.errors.Add(NewErrorWithHint(node.Pos(),
fmt.Sprintf("templ %s has more than one top-level element; only the first is rendered", comp.Name),
"wrap them in a single root element such as <div>"))
return
}
}

// analyzeNode validates an AST node.
Expand Down
170 changes: 170 additions & 0 deletions internal/tuigen/analyzer_single_root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package tuigen

import (
"strings"
"testing"
)

// A templ renders only its first top-level element, but the generator still
// emitted the rest, so a trailing <modal> mounted and drew as an overlay while
// never joining the element tree (issue #169). The analyzer now rejects a body
// with more than one top-level element.
func TestAnalyzer_SingleRootElement(t *testing.T) {
type tc struct {
input string
wantError bool
errorContains string
hintContains string
posContains string
}

tests := map[string]tc{
"element followed by modal errors": {
input: `package x
type app struct{}
templ (a *app) Render() {
<div>
<span>hi</span>
</div>
<modal open={a.show}>
<span>dialog</span>
</modal>
}`,
wantError: true,
errorContains: "templ Render has more than one top-level element",
hintContains: "wrap them in a single root element",
posContains: "test.gsx:7:",
},
"element followed by component call errors": {
input: `package x
templ Page() {
<div>hi</div>
@Footer()
}`,
wantError: true,
errorContains: "templ Page has more than one top-level element",
posContains: "test.gsx:4:",
},
"element followed by component expr errors": {
input: `package x
type app struct{}
templ (a *app) Render() {
<div>hi</div>
@a.footer
}`,
wantError: true,
errorContains: "templ Render has more than one top-level element",
},
"element followed by for loop errors": {
input: `package x
templ List(items []string) {
<div>hi</div>
for _, item := range items {
<span>{item}</span>
}
}`,
wantError: true,
errorContains: "templ List has more than one top-level element",
},
"if statement followed by element errors": {
input: `package x
templ Page(show bool) {
if show {
<span>on</span>
}
<div>hi</div>
}`,
wantError: true,
errorContains: "templ Page has more than one top-level element",
},
"three elements report once at the second": {
input: `package x
templ Page() {
<div>a</div>
<div>b</div>
<div>c</div>
}`,
wantError: true,
errorContains: "test.gsx:4:",
},
"let binding before the root is allowed": {
input: `package x
templ Page() {
label := <span>hi</span>
<div>{label}</div>
}`,
wantError: false,
},
"modal nested inside the root is allowed": {
input: `package x
type app struct{}
templ (a *app) Render() {
<div>
<span>hi</span>
<modal open={a.show}>
<span>dialog</span>
</modal>
</div>
}`,
wantError: false,
},
"modal as the only root is allowed": {
input: `package x
type app struct{}
templ (a *app) Render() {
<modal open={a.show}>
<span>dialog</span>
</modal>
}`,
wantError: false,
},
"if else as the only root is allowed": {
input: `package x
templ Page(show bool) {
if show {
<span>on</span>
} else {
<span>off</span>
}
}`,
wantError: false,
},
"for loop as the only root is allowed": {
input: `package x
templ List(items []string) {
for _, item := range items {
<span>{item}</span>
}
}`,
wantError: false,
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
_, err := AnalyzeFile("test.gsx", tt.input)
if !tt.wantError {
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
return
}
if err == nil {
t.Fatalf("expected an error, got nil")
}
msg := err.Error()
if !strings.Contains(msg, tt.errorContains) {
t.Errorf("error %q does not contain %q", msg, tt.errorContains)
}
if tt.hintContains != "" && !strings.Contains(msg, tt.hintContains) {
t.Errorf("error %q does not contain hint %q", msg, tt.hintContains)
}
if tt.posContains != "" && !strings.Contains(msg, tt.posContains) {
t.Errorf("error %q is not anchored at %q", msg, tt.posContains)
}
if n := strings.Count(msg, "more than one top-level element"); n != 1 {
t.Errorf("expected exactly one report, got %d in %q", n, msg)
}
})
}
}
Loading