/
find_go.go
140 lines (121 loc) · 3.26 KB
/
find_go.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
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
package msgfile
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"path/filepath"
"strings"
"golang.org/x/tools/go/packages"
"zgo.at/errors"
"zgo.at/zstd/zfilepath"
"zgo.at/zstd/zstring"
)
// Go finds all translatable strings in dir.
func FindGo(dir string, funs ...string) (Entries, error) {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedTypes | packages.NeedSyntax,
Dir: dir,
}, filepath.Join(dir, "/..."))
if err != nil {
return nil, errors.Wrap(err, "msgfile.FindGo")
}
var (
found = make(Entries)
errs = errors.NewGroup(20)
)
for _, p := range pkgs {
if len(p.Errors) > 0 {
for _, e := range p.Errors {
errs.Append(e)
}
return nil, errors.Wrap(errs.ErrorOrNil(), "msgfile.FindGo")
}
var comment string
for _, f := range p.Syntax {
ast.Inspect(f, func(n ast.Node) bool {
if n == nil {
return true
}
{
// text := new(bytes.Buffer)
// printer.Fprint(text, p.Fset, n)
// fmt.Printf(" %-20T → %q\n", n, text.String())
}
cmt, ok := n.(*ast.CommentGroup)
if ok {
t := cmt.Text()
if !strings.HasPrefix(t, "z18n: ") {
comment = ""
return true
}
comment = strings.TrimPrefix(strings.TrimRight(t, "\n"), "z18n: ")
return true
}
c, ok := n.(*ast.CallExpr)
if !ok {
switch n.(type) {
case *ast.ValueSpec, *ast.Ident, *ast.Comment:
default:
comment = ""
}
return true
}
var name string
switch f := c.Fun.(type) {
case *ast.FuncLit, *ast.ArrayType, *ast.ParenExpr, *ast.CallExpr:
comment = ""
return true // No work.
case *ast.Ident: // T(..)
name = f.Name
case *ast.SelectorExpr: // pkg.T(..)
name = f.Sel.Name
case *ast.IndexExpr: // someMap[T(..)]
// TODO
return true
default:
pos := p.Fset.Position(c.Pos())
text := new(bytes.Buffer)
printer.Fprint(text, p.Fset, c.Fun)
panic(errors.Errorf("msgfile.FindGo: unhandled c.Fun type %[1]T at %[2]s:%[3]d\n type → %#[1]v\n text → %[4]s\n\n",
c.Fun, pos.Filename, pos.Line, text.String()))
}
if !zstring.Contains(funs, name) {
comment = ""
return true
}
if len(c.Args) < 1 {
pos := p.Fset.Position(c.Fun.Pos())
errs.Append(errors.Errorf("%s:%d: not enough arguments to %q",
pos.Filename, pos.Line, name))
}
// Get the first string argument.
idlit, ok := c.Args[0].(*ast.BasicLit)
if !ok || idlit.Kind != token.STRING {
if len(c.Args) > 1 {
idlit, ok = c.Args[1].(*ast.BasicLit)
}
if !ok || idlit.Kind != token.STRING {
comment = ""
return true
}
}
id, def := zstring.Split2(strings.Trim(idlit.Value, quotes), "|")
// if def != "" && errors.Append(typeCheck(id, def)) {
// return false
// }
e := Entry{ID: id, Default: normalizeMessage(def), Context: comment}
if f, ok := found[id]; ok {
e.Loc = f.Loc
}
pos := p.Fset.Position(c.Pos())
e.Loc = append(e.Loc, fmt.Sprintf("%s:%d", zfilepath.TrimPrefix(pos.Filename, dir), pos.Line))
found[id] = e
comment = ""
return true
})
}
}
return found, errors.Wrap(errs.ErrorOrNil(), "msgfile.FindGo")
}