gitstack

grindlemire/go-tui code browser

10.0 KB Go 470 lines 2026-06-03 ยท d15bb9f 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
package tuigen

import (
	"testing"
)

func TestParser_PackageAndImports(t *testing.T) {
	type tc struct {
		input       string
		wantPkg     string
		wantImports int
		wantError   bool
	}

	tests := map[string]tc{
		"simple package": {
			input:       "package myapp\n",
			wantPkg:     "myapp",
			wantImports: 0,
		},
		"package with single import": {
			input:       "package myapp\nimport \"fmt\"\n",
			wantPkg:     "myapp",
			wantImports: 1,
		},
		"package with grouped imports": {
			input: `package myapp
import (
	"fmt"
	"strings"
)
`,
			wantPkg:     "myapp",
			wantImports: 2,
		},
		"package with aliased import": {
			input:       "package myapp\nimport e \"github.com/example/element\"\n",
			wantPkg:     "myapp",
			wantImports: 1,
		},
		"missing package": {
			input:     "import \"fmt\"\n",
			wantError: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			l := NewLexer("test.gsx", tt.input)
			p := NewParser(l)
			file, err := p.ParseFile()

			if tt.wantError {
				if err == nil {
					t.Error("expected error, got nil")
				}
				return
			}

			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}

			if file.Package != tt.wantPkg {
				t.Errorf("Package = %q, want %q", file.Package, tt.wantPkg)
			}

			if len(file.Imports) != tt.wantImports {
				t.Errorf("len(Imports) = %d, want %d", len(file.Imports), tt.wantImports)
			}
		})
	}
}

func TestParser_ImportDetails(t *testing.T) {
	type tc struct {
		input     string
		wantAlias string
		wantPath  string
	}

	tests := map[string]tc{
		"simple import": {
			input:     "package x\nimport \"fmt\"\n",
			wantAlias: "",
			wantPath:  "fmt",
		},
		"aliased import": {
			input:     "package x\nimport f \"fmt\"\n",
			wantAlias: "f",
			wantPath:  "fmt",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			l := NewLexer("test.gsx", tt.input)
			p := NewParser(l)
			file, err := p.ParseFile()
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}

			if len(file.Imports) != 1 {
				t.Fatalf("expected 1 import, got %d", len(file.Imports))
			}

			imp := file.Imports[0]
			if imp.Alias != tt.wantAlias {
				t.Errorf("Alias = %q, want %q", imp.Alias, tt.wantAlias)
			}
			if imp.Path != tt.wantPath {
				t.Errorf("Path = %q, want %q", imp.Path, tt.wantPath)
			}
		})
	}
}

func TestParser_FunctionParamsMultilineWithTrailingComma(t *testing.T) {
	input := `package x
type SettingsApp struct{}

func NewSettingsApp(
	provider string,
	model string,
	onClose func(),
) *SettingsApp {
	return &SettingsApp{}
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	if len(file.Funcs) != 1 {
		t.Fatalf("expected 1 function, got %d", len(file.Funcs))
	}
}

func TestParser_GoExpression(t *testing.T) {
	input := `package x
templ Test() {
	<span>{fmt.Sprintf("Count: %d", count)}</span>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	textElem := file.Components[0].Body[0].(*Element)
	if len(textElem.Children) != 1 {
		t.Fatalf("expected 1 child, got %d", len(textElem.Children))
	}

	expr, ok := textElem.Children[0].(*GoExpr)
	if !ok {
		t.Fatalf("expected *GoExpr, got %T", textElem.Children[0])
	}

	expected := `fmt.Sprintf("Count: %d", count)`
	if expr.Code != expected {
		t.Errorf("Code = %q, want %q", expr.Code, expected)
	}
}

func TestParser_CompleteExample(t *testing.T) {
	input := `package components

import (
	"fmt"
	tui "github.com/grindlemire/go-tui"
)

templ Dashboard(items []string, selectedIndex int) {
	<div direction={tui.Column} padding=1>
		<span>Dashboard</span>
		for i, item := range items {
			if i == selectedIndex {
				<span textStyle={highlightStyle}>{item}</span>
			} else {
				<span>{item}</span>
			}
		}
	</div>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// Check package
	if file.Package != "components" {
		t.Errorf("Package = %q, want 'components'", file.Package)
	}

	// Check imports
	if len(file.Imports) != 2 {
		t.Errorf("len(Imports) = %d, want 2", len(file.Imports))
	}

	// Check component
	if len(file.Components) != 1 {
		t.Fatalf("len(Components) = %d, want 1", len(file.Components))
	}

	comp := file.Components[0]
	if comp.Name != "Dashboard" {
		t.Errorf("Name = %q, want 'Dashboard'", comp.Name)
	}

	if len(comp.Params) != 2 {
		t.Errorf("len(Params) = %d, want 2", len(comp.Params))
	}

	// Check body structure
	if len(comp.Body) != 1 {
		t.Fatalf("len(Body) = %d, want 1", len(comp.Body))
	}

	box := comp.Body[0].(*Element)
	if box.Tag != "div" {
		t.Errorf("box.Tag = %q, want 'div'", box.Tag)
	}

	// Should have text and for loop as children
	if len(box.Children) < 2 {
		t.Errorf("len(box.Children) = %d, want >= 2", len(box.Children))
	}
}

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

	tests := map[string]tc{
		"missing component name": {
			input: `package x
templ() {
	<span>Hello</span>
}`,
			errorContains: "expected component name",
		},
		"unclosed element": {
			input: `package x
templ Test() {
	<div>
		<span>Hello</span>
}`,
			errorContains: "expected closing tag",
		},
		"mismatched tags": {
			input: `package x
templ Test() {
	<div>Hello</span>
}`,
			errorContains: "mismatched closing tag",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			l := NewLexer("test.gsx", tt.input)
			p := NewParser(l)
			_, err := p.ParseFile()

			if err == nil {
				t.Error("expected error, got nil")
				return
			}

			// We just verify an error occurred, not the exact message
			// since error recovery may report different errors
		})
	}
}

func TestParser_Position(t *testing.T) {
	input := `package x

templ Test() {
	<span>Hello</span>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	// Component should be on line 3
	if comp.Position.Line != 3 {
		t.Errorf("Component line = %d, want 3", comp.Position.Line)
	}

	elem := comp.Body[0].(*Element)
	// Element should be on line 4
	if elem.Position.Line != 4 {
		t.Errorf("Element line = %d, want 4", elem.Position.Line)
	}
}

func TestParser_MultipleComponents(t *testing.T) {
	input := `package x

templ Header() {
	<span>Header</span>
}

templ Footer() {
	<span>Footer</span>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	if len(file.Components) != 2 {
		t.Fatalf("expected 2 components, got %d", len(file.Components))
	}

	if file.Components[0].Name != "Header" {
		t.Errorf("component 0 name = %q, want 'Header'", file.Components[0].Name)
	}

	if file.Components[1].Name != "Footer" {
		t.Errorf("component 1 name = %q, want 'Footer'", file.Components[1].Name)
	}
}

func TestParser_TextContent(t *testing.T) {
	input := `package x
templ Test() {
	<span>Hello World</span>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	textElem := file.Components[0].Body[0].(*Element)
	if textElem.Tag != "span" {
		t.Errorf("Tag = %q, want 'span'", textElem.Tag)
	}

	// Text content is parsed as identifier tokens (Hello, World)
	if len(textElem.Children) < 1 {
		t.Errorf("expected children, got %d", len(textElem.Children))
	}
}

func TestParser_TextContentCoalescing(t *testing.T) {
	input := `package x
templ Test() {
	<span>Hello World from component</span>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	textElem := file.Components[0].Body[0].(*Element)
	if textElem.Tag != "span" {
		t.Fatalf("expected span element, got %s", textElem.Tag)
	}

	// Text should be coalesced into a single TextContent node
	if len(textElem.Children) != 1 {
		t.Fatalf("expected 1 child (coalesced text), got %d", len(textElem.Children))
	}

	textContent, ok := textElem.Children[0].(*TextContent)
	if !ok {
		t.Fatalf("expected *TextContent, got %T", textElem.Children[0])
	}

	expected := "Hello World from component"
	if textContent.Text != expected {
		t.Errorf("Text = %q, want %q", textContent.Text, expected)
	}
}

func TestParser_ErrorRecoveryMultipleComponents(t *testing.T) {
	// This test verifies that the parser can recover from errors
	// and continue parsing subsequent components
	input := `package x

templ Broken(
	<span>Hello</span>
}

templ Working() {
	<span>World</span>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()

	// Should have errors
	if err == nil {
		t.Log("Note: No error returned, parser may have recovered")
	}

	// But should still have parsed what it could
	// The first component is broken, but we should try to recover
	if file == nil {
		t.Fatal("expected file to be non-nil even with errors")
	}

	// Due to error recovery, we might get the second component
	t.Logf("Parsed %d components", len(file.Components))
}

func TestParser_RawSourcePreservation(t *testing.T) {
	// Test that raw source is preserved correctly in conditions/iterables
	input := `package x
templ Test() {
	if user.Name != "" && user.Age >= 18 {
		<span>Adult</span>
	}
	for i, v := range items[0:10] {
		<span>{v}</span>
	}
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	body := file.Components[0].Body

	// Check if condition preserves original formatting
	ifStmt := body[0].(*IfStmt)
	expectedCond := `user.Name != "" && user.Age >= 18`
	if ifStmt.Condition != expectedCond {
		t.Errorf("Condition = %q, want %q", ifStmt.Condition, expectedCond)
	}

	// Check for iterable preserves original formatting
	forLoop := body[1].(*ForLoop)
	expectedIter := "items[0:10]"
	if forLoop.Iterable != expectedIter {
		t.Errorf("Iterable = %q, want %q", forLoop.Iterable, expectedIter)
	}
}