gitstack

grindlemire/go-tui code browser

8.3 KB Go 316 lines 2026-07-02 · 706f91a raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package tuigen

import (
	"strings"
	"testing"
)

// Component elements (textarea/input/modal/markdown) mount via app.Mount against
// the host component's receiver. A function templ has no receiver, so using one
// there generated invalid Go (issue #111). The analyzer now rejects it with a
// message that points at the struct-component pattern.
func TestAnalyzer_ComponentElementRequiresReceiver(t *testing.T) {
	type tc struct {
		input         string
		wantError     bool
		errorContains string
		hintContains  string
	}

	tests := map[string]tc{
		"textarea in function templ errors": {
			input: `package x
templ MyForm() {
	<textarea placeholder="Type here..." />
}`,
			wantError:     true,
			errorContains: "<textarea> can only be used inside a struct component",
			hintContains:  "give MyForm a receiver",
		},
		"input in function templ errors": {
			input: `package x
templ MyForm() {
	<input placeholder="Name" />
}`,
			wantError:     true,
			errorContains: "<input> can only be used inside a struct component",
		},
		"markdown in function templ errors": {
			input: `package x
templ Doc(src string) {
	<markdown source={src} />
}`,
			wantError:     true,
			errorContains: "<markdown> can only be used inside a struct component",
		},
		"modal in function templ errors": {
			input: `package x
templ Dialog(show bool) {
	<modal open={show}>
		<span>hi</span>
	</modal>
}`,
			wantError:     true,
			errorContains: "<modal> can only be used inside a struct component",
		},
		"nested in for loop in function templ errors": {
			input: `package x
templ List(items []string) {
	<div>
		for _, item := range items {
			<textarea placeholder={item} />
		}
	</div>
}`,
			wantError:     true,
			errorContains: "<textarea> can only be used inside a struct component",
		},
		"nested in if in function templ errors": {
			input: `package x
templ Maybe(show bool) {
	<div>
		if show {
			<input />
		}
	</div>
}`,
			wantError:     true,
			errorContains: "<input> can only be used inside a struct component",
		},
		"in let binding in function templ errors": {
			input: `package x
templ MyForm() {
	editor := <textarea />
	<div>{editor}</div>
}`,
			wantError:     true,
			errorContains: "<textarea> can only be used inside a struct component",
		},
		"in component-call children in function templ errors": {
			input: `package x
templ MyForm() {
	@Card() {
		<textarea />
	}
}`,
			wantError:     true,
			errorContains: "<textarea> can only be used inside a struct component",
		},
		"textarea in method templ is allowed": {
			input: `package x
type myForm struct{}
templ (c *myForm) Render() {
	<textarea placeholder="Type here..." />
}`,
			wantError: false,
		},
		"modal in method templ is allowed": {
			input: `package x
type myForm struct{}
templ (c *myForm) Render() {
	<modal open={c.show}>
		<span>hi</span>
	</modal>
}`,
			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 an error, got nil")
				}
				if !strings.Contains(err.Error(), tt.errorContains) {
					t.Errorf("error %q does not contain %q", err.Error(), tt.errorContains)
				}
				if tt.hintContains != "" && !strings.Contains(err.Error(), tt.hintContains) {
					t.Errorf("error %q does not contain hint %q", err.Error(), tt.hintContains)
				}
				return
			}
			if err != nil {
				t.Fatalf("expected no error, got %v", err)
			}
		})
	}
}

// Component expressions (@expr) render via expr.Render(app). A function templ
// body has no app in scope, so the analyzer rejects them there while allowing
// them in method templs, whose Render(app) receiver supplies app.
func TestAnalyzer_ComponentExprRequiresReceiver(t *testing.T) {
	type tc struct {
		input         string
		wantError     bool
		errorContains string
		hintContains  string
	}

	tests := map[string]tc{
		"component expr in function templ errors": {
			input: `package x
templ Foo(child tui.Component) {
	<div>@child</div>
}`,
			wantError:     true,
			errorContains: "component expression @child can only be used inside a struct component",
			hintContains:  "give Foo a receiver",
		},
		"component expr nested in for loop in function templ errors": {
			input: `package x
templ List(kids []tui.Component) {
	<div>
		for _, kid := range kids {
			@kid
		}
	</div>
}`,
			wantError:     true,
			errorContains: "component expression @kid can only be used inside a struct component",
		},
		"component expr in let binding in function templ errors": {
			input: `package x
templ Foo(child tui.Component) {
	thing := @child
	<div>{thing}</div>
}`,
			wantError:     true,
			errorContains: "component expression @child can only be used inside a struct component",
		},
		"component expr nested in if in function templ errors": {
			input: `package x
templ Maybe(show bool, child tui.Component) {
	<div>
		if show {
			@child
		}
	</div>
}`,
			wantError:     true,
			errorContains: "component expression @child can only be used inside a struct component",
		},
		"component expr in method templ is allowed": {
			input: `package x
type host struct{ child tui.Component }
templ (h *host) Render() {
	<div>@h.child</div>
}`,
			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 an error, got nil")
				}
				if !strings.Contains(err.Error(), tt.errorContains) {
					t.Errorf("error %q does not contain %q", err.Error(), tt.errorContains)
				}
				if tt.hintContains != "" && !strings.Contains(err.Error(), tt.hintContains) {
					t.Errorf("error %q does not contain hint %q", err.Error(), tt.hintContains)
				}
				return
			}
			if err != nil {
				t.Fatalf("expected no error, got %v", err)
			}
		})
	}
}

// A @Factory() call that returns a struct component mounts via app.Mount against
// the caller's receiver. In a function templ the generator instead emits a plain
// call and reads .Root on a type that has none. The analyzer rejects the call in
// a function templ, but leaves it (and plain function-component calls) alone in a
// method templ.
func TestAnalyzer_StructComponentCallRequiresReceiver(t *testing.T) {
	type tc struct {
		input         string
		wantError     bool
		errorContains string
		hintContains  string
	}

	// Shared preamble: a struct component `widget` with a factory `Widget()`.
	const structComp = `package x
type widget struct{}
func Widget() *widget { return &widget{} }
templ (w *widget) Render() {
	<span>hi</span>
}
`

	tests := map[string]tc{
		"struct factory call in function templ errors": {
			input: structComp + `templ Page() {
	<div>@Widget()</div>
}`,
			wantError:     true,
			errorContains: "@Widget() mounts a struct component and can only be used inside a struct component",
			hintContains:  "give Page a receiver",
		},
		"struct factory with params call in function templ errors": {
			input: `package x
type widget struct{}
func Widget(id string) *widget { return &widget{} }
templ (w *widget) Render() {
	<span>hi</span>
}
templ Page() {
	<div>@Widget("a")</div>
}`,
			wantError:     true,
			errorContains: "@Widget() mounts a struct component and can only be used inside a struct component",
		},
		"struct factory call in method templ is allowed": {
			input: structComp + `type host struct{}
templ (h *host) Render() {
	<div>@Widget()</div>
}`,
			wantError: false,
		},
		"struct factory used in a go expression is not flagged": {
			input: structComp + `templ Page() {
	<div>{Widget()}</div>
}`,
			wantError: false,
		},
		"function component call in function templ is allowed": {
			input: `package x
templ Badge() {
	<span>b</span>
}
templ Page() {
	<div>@Badge()</div>
}`,
			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 an error, got nil")
				}
				if !strings.Contains(err.Error(), tt.errorContains) {
					t.Errorf("error %q does not contain %q", err.Error(), tt.errorContains)
				}
				if tt.hintContains != "" && !strings.Contains(err.Error(), tt.hintContains) {
					t.Errorf("error %q does not contain hint %q", err.Error(), tt.hintContains)
				}
				return
			}
			if err != nil {
				t.Fatalf("expected no error, got %v", err)
			}
		})
	}
}