gitstack

grindlemire/go-tui code browser

10.5 KB Go 508 lines 2026-05-29 ยท ba5d647 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package tuigen

import (
	"strings"
	"testing"
)

func TestAnalyzer_NestedElements(t *testing.T) {
	// Test that nested elements are all validated
	input := `package x
templ Test() {
	<div>
		<div>
			<unknownTag />
		</div>
	</div>
}`

	_, err := AnalyzeFile("test.gsx", input)
	if err == nil {
		t.Error("expected error for nested unknown tag")
		return
	}

	if !strings.Contains(err.Error(), "unknown element tag <unknownTag>") {
		t.Errorf("error %q does not contain expected message", err.Error())
	}
}

func TestAnalyzer_ControlFlowValidation(t *testing.T) {
	type tc struct {
		input         string
		wantError     bool
		errorContains string
	}

	tests := map[string]tc{
		"valid for loop": {
			input: `package x
templ Test(items []string) {
	<div>
		for _, item := range items {
			<span>{item}</span>
		}
	</div>
}`,
			wantError: false,
		},
		"invalid element in for loop": {
			input: `package x
templ Test(items []string) {
	<div>
		for _, item := range items {
			<badTag />
		}
	</div>
}`,
			wantError:     true,
			errorContains: "unknown element tag <badTag>",
		},
		"valid if statement": {
			input: `package x
templ Test(show bool) {
	<div>
		if show {
			<span>visible</span>
		}
	</div>
}`,
			wantError: false,
		},
		"invalid element in if then": {
			input: `package x
templ Test(show bool) {
	<div>
		if show {
			<badTag />
		}
	</div>
}`,
			wantError:     true,
			errorContains: "unknown element tag <badTag>",
		},
		"invalid element in if else": {
			input: `package x
templ Test(show bool) {
	<div>
		if show {
			<span>yes</span>
		} else {
			<badTag />
		}
	</div>
}`,
			wantError:     true,
			errorContains: "unknown element tag <badTag>",
		},
	}

	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.Error("expected error, got nil")
					return
				}
				if tt.errorContains != "" && !strings.Contains(err.Error(), tt.errorContains) {
					t.Errorf("error %q does not contain %q", err.Error(), tt.errorContains)
				}
			} else {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
			}
		})
	}
}

func TestAnalyzer_AllKnownAttributes(t *testing.T) {
	// Test all known attributes are accepted
	attributes := []string{
		"width", "widthPercent", "height", "heightPercent",
		"minWidth", "minHeight", "maxWidth", "maxHeight",
		"direction", "justify", "align", "gap",
		"flexGrow", "flexShrink", "alignSelf",
		"padding", "margin",
		"border", "borderStyle", "background",
		"text", "textStyle", "textAlign",
		"onFocus", "onBlur",
		"scrollable", "scrollbarStyle", "scrollbarThumbStyle", "hideScrollbar",
		"disabled", "id",
	}

	for _, attr := range attributes {
		t.Run(attr, func(t *testing.T) {
			input := `package x
templ Test() {
	<div ` + attr + `=1></div>
}`
			_, err := AnalyzeFile("test.gsx", input)
			if err != nil {
				t.Errorf("attribute %q should be valid, got error: %v", attr, err)
			}
		})
	}
}

func TestAnalyzer_AllKnownTags(t *testing.T) {
	// Test all known tags are accepted
	tags := []string{
		"div", "span", "p", "ul", "li",
		"button", "input", "textarea", "table", "tr", "td", "th",
		"progress", "hr", "br",
	}

	for _, tag := range tags {
		t.Run(tag, func(t *testing.T) {
			input := `package x
templ Test() {
	<` + tag + ` />
}`
			_, err := AnalyzeFile("test.gsx", input)
			if err != nil {
				t.Errorf("tag %q should be valid, got error: %v", tag, err)
			}
		})
	}
}

func TestAnalyzer_HRValid(t *testing.T) {
	type tc struct {
		input     string
		wantError bool
	}

	tests := map[string]tc{
		"hr self-closing": {
			input: `package x
templ Test() {
	<div>
		<hr/>
	</div>
}`,
			wantError: false,
		},
		"hr with class": {
			input: `package x
templ Test() {
	<div>
		<hr class="border-double"/>
	</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.Error("expected error, got nil")
				}
			} else {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
			}
		})
	}
}

func TestAnalyzer_BRValid(t *testing.T) {
	type tc struct {
		input     string
		wantError bool
	}

	tests := map[string]tc{
		"br self-closing": {
			input: `package x
templ Test() {
	<div>
		<br/>
	</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.Error("expected error, got nil")
				}
			} else {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
			}
		})
	}
}

func TestAnalyzer_MarkdownValid(t *testing.T) {
	type tc struct {
		input         string
		wantError     bool
		errorContains string
	}

	tests := map[string]tc{
		"markdown self-closing with source/width": {
			input: `package x
templ (c *view) Render() {
	<markdown source={c.readme} width={80} />
}`,
			wantError: false,
		},
		"markdown with state attr": {
			input: `package x
templ (c *view) Render() {
	<markdown state={c.md} />
}`,
			wantError: false,
		},
		"markdown is void": {
			input: `package x
templ (c *view) Render() {
	<markdown source={c.readme}>oops</markdown>
}`,
			wantError:     true,
			errorContains: "<markdown> is a void element and cannot have children",
		},
	}

	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.Fatal("expected error, got nil")
				}
				if !strings.Contains(err.Error(), tt.errorContains) {
					t.Errorf("error %q should contain %q", err.Error(), tt.errorContains)
				}
			} else if err != nil {
				t.Errorf("unexpected error: %v", err)
			}
		})
	}
}

func TestAnalyzer_VoidWithChildren(t *testing.T) {
	type tc struct {
		input         string
		wantError     bool
		errorContains string
	}

	tests := map[string]tc{
		"hr with text child": {
			input: `package x
templ Test() {
	<div>
		<hr>text</hr>
	</div>
}`,
			wantError:     true,
			errorContains: "<hr> is a void element and cannot have children",
		},
		"hr with element child": {
			input: `package x
templ Test() {
	<div>
		<hr><span>nested</span></hr>
	</div>
}`,
			wantError:     true,
			errorContains: "<hr> is a void element and cannot have children",
		},
		"br with text child": {
			input: `package x
templ Test() {
	<div>
		<br>text</br>
	</div>
}`,
			wantError:     true,
			errorContains: "<br> is a void element and cannot have children",
		},
		"input with child": {
			input: `package x
templ Test() {
	<div>
		<input>text</input>
	</div>
}`,
			wantError:     true,
			errorContains: "<input> is a void element and cannot have children",
		},
	}

	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.Error("expected error, got nil")
					return
				}
				if tt.errorContains != "" && !strings.Contains(err.Error(), tt.errorContains) {
					t.Errorf("error %q does not contain %q", err.Error(), tt.errorContains)
				}
			} else {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
			}
		})
	}
}

func TestAnalyzer_TailwindClassValidation(t *testing.T) {
	type tc struct {
		input         string
		wantError     bool
		errorContains string
		hintContains  string
	}

	tests := map[string]tc{
		"valid tailwind classes": {
			input: `package x
templ Test() {
	<div class="flex-col gap-2 p-4"></div>
}`,
			wantError: false,
		},
		"valid width and height classes": {
			input: `package x
templ Test() {
	<div class="w-full h-1/2"></div>
}`,
			wantError: false,
		},
		"valid individual padding classes": {
			input: `package x
templ Test() {
	<div class="pt-2 pb-4 pl-1"></div>
}`,
			wantError: false,
		},
		"valid border color classes": {
			input: `package x
templ Test() {
	<div class="border border-red"></div>
}`,
			wantError: false,
		},
		"valid text alignment classes": {
			input: `package x
templ Test() {
	<div class="text-center"></div>
}`,
			wantError: false,
		},
		"unknown tailwind class": {
			input: `package x
templ Test() {
	<div class="flex-columns"></div>
}`,
			wantError:     true,
			errorContains: `unknown Tailwind class "flex-columns"`,
			hintContains:  `flex-col`,
		},
		"unknown tailwind class without suggestion": {
			input: `package x
templ Test() {
	<div class="xyz-completely-invalid"></div>
}`,
			wantError:     true,
			errorContains: `unknown Tailwind class "xyz-completely-invalid"`,
		},
		"multiple unknown classes": {
			input: `package x
templ Test() {
	<div class="flex-columns badclass"></div>
}`,
			wantError:     true,
			errorContains: `unknown Tailwind class`,
		},
		"mix of valid and invalid classes": {
			input: `package x
templ Test() {
	<div class="flex-col gap-2 badclass p-4"></div>
}`,
			wantError:     true,
			errorContains: `unknown Tailwind class "badclass"`,
		},
	}

	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.Error("expected error, got nil")
					return
				}
				if tt.errorContains != "" && !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)
				}
			} else {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
			}
		})
	}
}

func TestAnalyzer_TailwindClassErrorPosition(t *testing.T) {
	// Test that the error position correctly points to the invalid class
	input := `package x
templ Test() {
	<div class="flex-col badclass p-2"></div>
}`

	_, err := AnalyzeFile("test.gsx", input)
	if err == nil {
		t.Fatal("expected error, got nil")
	}

	errList, ok := err.(*ErrorList)
	if !ok {
		t.Fatalf("expected *ErrorList, got %T", err)
	}

	errors := errList.Errors()
	if len(errors) != 1 {
		t.Fatalf("expected 1 error, got %d", len(errors))
	}

	tuiErr := errors[0]
	// The error should point to "badclass" which starts at column 22
	// (class=" is at column 7, content starts at column 14, "flex-col " is 9 chars, so badclass is at column 23)
	// Actually, let's verify the error message contains the class name
	if !strings.Contains(tuiErr.Message, "badclass") {
		t.Errorf("error message %q should contain 'badclass'", tuiErr.Message)
	}

	// Verify EndPos is set for range highlighting
	if tuiErr.EndPos.Line == 0 && tuiErr.EndPos.Column == 0 {
		t.Error("EndPos should be set for range-based highlighting")
	}
}