/
diagnostics.go
61 lines (55 loc) · 1.21 KB
/
diagnostics.go
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
package parser
import "errors"
type diagnoser func(Node) ([]Diagnostic, error)
// Diagnostic for template file.
type Diagnostic struct {
Message string
Range Range
}
func walkTemplate(t TemplateFile, f func(Node) bool) {
for _, n := range t.Nodes {
hn, ok := n.(HTMLTemplate)
if !ok {
continue
}
walkNodes(hn.Children, f)
}
}
func walkNodes(t []Node, f func(Node) bool) {
for _, n := range t {
if !f(n) {
continue
}
if h, ok := n.(CompositeNode); ok {
walkNodes(h.ChildNodes(), f)
}
}
}
func Diagnose(t TemplateFile) ([]Diagnostic, error) {
diagnosers := []diagnoser{
legacyCallSyntaxDiagnoser,
}
var diags []Diagnostic
var errs error
walkTemplate(t, func(n Node) bool {
for _, d := range diagnosers {
diag, err := d(n)
if err != nil {
errs = errors.Join(errs, err)
return false
}
diags = append(diags, diag...)
}
return true
})
return diags, errs
}
func legacyCallSyntaxDiagnoser(n Node) ([]Diagnostic, error) {
if c, ok := n.(CallTemplateExpression); ok {
return []Diagnostic{{
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
Range: c.Expression.Range,
}}, nil
}
return nil, nil
}