Repository navigation
fix(gsx): reject templ bodies with more than one top-level element #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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!