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
|
package tuigen
import (
"strings"
"testing"
)
func TestAnalyzer_NameCollisions(t *testing.T) {
type tc struct {
input string
wantError bool
errorContains string
}
tests := map[string]tc{
"templ conflicts with Go function of the same name": {
input: `package x
func Foo() string { return "x" }
templ Foo() {
<span>hi</span>
}`,
wantError: true,
errorContains: "conflicts with a Go function",
},
"duplicate function templ names": {
input: `package x
templ Foo() {
<span>one</span>
}
templ Foo() {
<span>two</span>
}`,
wantError: true,
errorContains: "duplicate templ",
},
"handwritten Render method alongside a method templ": {
input: `package x
import tui "github.com/grindlemire/go-tui"
type row struct{ v string }
func (r *row) Render(app *tui.App) *tui.Element { return nil }
templ (r *row) Render() {
<span>{r.v}</span>
}`,
wantError: true,
errorContains: "already declares a Render method",
},
"user type collides with generated view struct": {
input: `package x
type FooView struct{ n int }
templ Foo() {
<span>hi</span>
}`,
wantError: true,
errorContains: "conflicts with the view struct",
},
"duplicate Render templs on the same receiver": {
input: `package x
type row struct{ v string }
templ (r *row) Render() {
<span>{r.v}</span>
}
templ (r *row) Render() {
<span>again</span>
}`,
wantError: true,
errorContains: "duplicate Render templ",
},
"unrelated Go function name is fine": {
input: `package x
func helper() string { return "x" }
templ Foo() {
<span>hi</span>
}`,
wantError: false,
},
"Render method on a different type is fine": {
input: `package x
import tui "github.com/grindlemire/go-tui"
type other struct{}
func (o *other) Render(app *tui.App) *tui.Element { return nil }
type row struct{ v string }
templ (r *row) Render() {
<span>{r.v}</span>
}`,
wantError: false,
},
"user type without the View suffix is fine": {
input: `package x
type FooModel struct{ n int }
templ Foo() {
<span>hi</span>
}`,
wantError: false,
},
"reserved helper name updatePropsFields is rejected": {
input: `package x
import tui "github.com/grindlemire/go-tui"
type row struct{ v string }
func (r *row) updatePropsFields(fresh tui.Component) {}
templ (r *row) Render() {
<span>{r.v}</span>
}`,
wantError: true,
errorContains: "reserved generated helper name",
},
"reserved helper name bindAppFields is rejected": {
input: `package x
import tui "github.com/grindlemire/go-tui"
type row struct{ v string }
func (r *row) bindAppFields(app *tui.App) {}
templ (r *row) Render() {
<span>{r.v}</span>
}`,
wantError: true,
errorContains: "reserved generated helper name",
},
"reserved helper name on a type without a templ is fine": {
input: `package x
import tui "github.com/grindlemire/go-tui"
type other struct{}
func (o *other) updatePropsFields(fresh tui.Component) {}
type row struct{ v string }
templ (r *row) Render() {
<span>{r.v}</span>
}`,
wantError: false,
},
"calling the helper from an override is fine": {
input: `package x
import tui "github.com/grindlemire/go-tui"
type row struct{ v string }
func (r *row) UpdateProps(fresh tui.Component) {
r.updatePropsFields(fresh)
}
templ (r *row) Render() {
<span>{r.v}</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.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_CrossFileNameCollisions(t *testing.T) {
type tc struct {
input string
siblingGo string
siblingGSX string
wantError bool
errorContains string
}
tests := map[string]tc{
"templ conflicts with function in sibling file": {
input: `package x
templ Foo() {
<span>hi</span>
}`,
siblingGo: "package x\n\nfunc Foo() string { return \"x\" }",
wantError: true,
errorContains: "conflicts with a Go function",
},
"templ duplicated in sibling gsx file": {
input: `package x
templ Foo() {
<span>hi</span>
}`,
siblingGSX: `package x
templ Foo() {
<span>other</span>
}`,
wantError: true,
errorContains: "another file",
},
"view struct conflicts with type in sibling file": {
input: `package x
templ Foo() {
<span>hi</span>
}`,
siblingGo: "package x\n\ntype FooView struct{ n int }",
wantError: true,
errorContains: "conflicts with the view struct",
},
"handwritten Render in sibling file": {
input: `package x
type row struct{ v string }
templ (r *row) Render() {
<span>{r.v}</span>
}`,
siblingGo: "package x\n\nimport tui \"github.com/grindlemire/go-tui\"\n\nfunc (r *row) Render(app *tui.App) *tui.Element { return nil }",
wantError: true,
errorContains: "already declares a Render method",
},
"Render templ duplicated in sibling gsx file": {
input: `package x
type row struct{ v string }
templ (r *row) Render() {
<span>{r.v}</span>
}`,
siblingGSX: `package x
templ (r *row) Render() {
<span>other</span>
}`,
wantError: true,
errorContains: "another file",
},
"unrelated sibling declarations are fine": {
input: `package x
templ Foo() {
<span>hi</span>
}`,
siblingGo: "package x\n\nfunc Bar() {}\n\ntype BarView struct{}",
wantError: false,
},
"reserved helper name in sibling file is rejected": {
input: `package x
type row struct{ v string }
templ (r *row) Render() {
<span>{r.v}</span>
}`,
siblingGo: "package x\n\nimport tui \"github.com/grindlemire/go-tui\"\n\nfunc (r *row) unbindAppFields() {}",
wantError: true,
errorContains: "reserved generated helper name",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
ctx := NewPackageContext()
if tt.siblingGo != "" {
if err := ctx.AddGoSource("sibling.go", tt.siblingGo); err != nil {
t.Fatalf("AddGoSource failed: %v", err)
}
}
if tt.siblingGSX != "" {
lexer := NewLexer("sibling.gsx", tt.siblingGSX)
parser := NewParser(lexer)
sib, err := parser.ParseFile()
if err != nil {
t.Fatalf("sibling parse failed: %v", err)
}
ctx.AddGSXFile(sib)
}
lexer := NewLexer("test.gsx", tt.input)
parser := NewParser(lexer)
file, err := parser.ParseFile()
if err != nil {
t.Fatalf("parse failed: %v", err)
}
analyzer := NewAnalyzer()
analyzer.SetPackageContext(ctx)
err = analyzer.Analyze(file)
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)
}
}
})
}
}
|