-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.go
385 lines (332 loc) · 9.42 KB
/
lib.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
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
// SPDX-License-Identifier: GPL-3.0-or-later
package gosmopolitan
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"regexp"
"strings"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const analyzerName = "gosmopolitan"
const analyzerDoc = "Report certain i18n/l10n anti-patterns in your Go codebase"
type AnalyzerConfig struct {
// LookAtTests is flag controlling whether the lints are going to look at
// test files, despite other config knobs of the Go analysis tooling
// framework telling us otherwise.
//
// By default gosmopolitan does not look at test files, because i18n-aware
// apps most probably have many unmarked strings in test cases, and names
// and descriptions *of* test cases are probably in the program's original
// natural language too.
LookAtTests bool
// EscapeHatches is optionally a list of fully qualified names, in the
// `(full/pkg/path).name` form, to act as "i18n escape hatches". Inside
// call-like expressions to those names, the string literal script check
// is ignored.
//
// With this functionality in place, you can use type aliases like
// `type R = string` as markers, or have explicitly i18n-aware functions
// exempt from the checks.
EscapeHatches []string
// WatchForScripts is optionally a list of Unicode script names to watch
// for any usage in string literals. The range of supported scripts is
// determined by the [unicode.Scripts] map and values are case-sensitive.
WatchForScripts []string
// AllowTimeLocal is flag controlling whether usages of [time.Local] are
// allowed (i.e. not reported).
AllowTimeLocal bool
}
func NewAnalyzer() *analysis.Analyzer {
var lookAtTests bool
var escapeHatchesStr string
var watchForScriptsStr string
var allowTimeLocal bool
a := &analysis.Analyzer{
Name: analyzerName,
Doc: analyzerDoc,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: func(p *analysis.Pass) (any, error) {
cfg := AnalyzerConfig{
LookAtTests: lookAtTests,
EscapeHatches: strings.Split(escapeHatchesStr, ","),
WatchForScripts: strings.Split(watchForScriptsStr, ","),
AllowTimeLocal: allowTimeLocal,
}
pctx := processCtx{cfg: &cfg, p: p}
return pctx.run()
},
RunDespiteErrors: false,
}
a.Flags.BoolVar(&lookAtTests,
"lookattests",
false,
"also check the test files",
)
a.Flags.StringVar(
&escapeHatchesStr,
"escapehatches",
"",
"comma-separated list of fully qualified names to act as i18n escape hatches",
)
a.Flags.StringVar(
&watchForScriptsStr,
"watchforscripts",
"Han",
"comma-separated list of Unicode scripts to watch out for occurrence in string literals",
)
a.Flags.BoolVar(&allowTimeLocal,
"allowtimelocal",
false,
"allow time.Local usages",
)
return a
}
func NewAnalyzerWithConfig(cfg *AnalyzerConfig) *analysis.Analyzer {
return &analysis.Analyzer{
Name: analyzerName,
Doc: analyzerDoc,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: func(p *analysis.Pass) (any, error) {
pctx := processCtx{cfg: cfg, p: p}
return pctx.run()
},
RunDespiteErrors: false,
}
}
var DefaultAnalyzer = NewAnalyzer()
func validateUnicodeScriptName(name string) error {
if _, ok := unicode.Scripts[name]; !ok {
return fmt.Errorf("invalid Unicode script name: %s", name)
}
return nil
}
// example input: ["Han", "Arabic"]
// example output: `\p{Han}|\p{Arabic}`
// assumes len(scriptNames) > 0
func makeUnicodeScriptMatcherRegexpString(scriptNames []string) string {
var sb strings.Builder
for i, s := range scriptNames {
if i > 0 {
sb.WriteRune('|')
}
sb.WriteString(`\p{`)
sb.WriteString(s)
sb.WriteRune('}')
}
return sb.String()
}
func makeUnicodeScriptMatcherRegexp(scriptNames []string) (*regexp.Regexp, error) {
return regexp.Compile(makeUnicodeScriptMatcherRegexpString(scriptNames))
}
type processCtx struct {
cfg *AnalyzerConfig
p *analysis.Pass
}
func mapSlice[T any, U any](x []T, fn func(T) U) []U {
if x == nil {
return nil
}
y := make([]U, len(x))
for i, v := range x {
y[i] = fn(v)
}
return y
}
func sliceToSet[T comparable](x []T) map[T]struct{} {
// lo.SliceToMap(x, func(k T) (T, struct{}) { return k, struct{}{} })
y := make(map[T]struct{}, len(x))
for _, k := range x {
y[k] = struct{}{}
}
return y
}
func getFullyQualifiedName(x types.Object) string {
pkg := x.Pkg()
if pkg == nil {
return x.Name()
}
return fmt.Sprintf("%s.%s", pkg.Path(), x.Name())
}
// if input is in the "(%s).%s" form, remove the parens, else return the
// unchanged input
//
// this is for maintaining compatibility with the previous FQN notation that
// was born out of my confusion (the previous notation, while commonly seen,
// seems to be only for methods or pointer receiver types; the parens-less
// form is in fact unambiguous, because Go identifiers can't contain periods.)
func unquoteInputFQN(x string) string {
if len(x) == 0 || x[0] != '(' {
return x
}
before, after, found := strings.Cut(x[1:], ")")
if !found {
// malformed input: string in "(xxxxx" form with unclosed parens!
// in this case, only removing the opening parens might be better than
// doing nothing after all
return x[1:]
}
// at this point,
// input: "(foo).bar"
// before: "foo"
// after: ".bar"
return before + after
}
func (c *processCtx) run() (any, error) {
escapeHatchesSet := sliceToSet(mapSlice(c.cfg.EscapeHatches, unquoteInputFQN))
if len(c.cfg.WatchForScripts) == 0 {
c.cfg.WatchForScripts = []string{"Han"}
}
for _, s := range c.cfg.WatchForScripts {
if err := validateUnicodeScriptName(s); err != nil {
return nil, err
}
}
charRE, err := makeUnicodeScriptMatcherRegexp(c.cfg.WatchForScripts)
if err != nil {
return nil, err
}
usq := newUnicodeScriptQuerier(c.cfg.WatchForScripts)
insp := c.p.ResultOf[inspect.Analyzer].(*inspector.Inspector)
// support ignoring the test files, because test files could be full of
// i18n and l10n fixtures, and we want to focus on the actual run-time
// logic
//
// TODO: is there a way to both ignore test files earlier, and make use of
// inspect.Analyzer's cached results? currently Inspector doesn't provide
// a way to selectively travese some files' AST but not others.
isBelongingToTestFiles := func(n ast.Node) bool {
return strings.HasSuffix(c.p.Fset.File(n.Pos()).Name(), "_test.go")
}
shouldSkipTheContainingFile := func(n ast.Node) bool {
if c.cfg.LookAtTests {
return false
}
return isBelongingToTestFiles(n)
}
insp.Nodes(nil, func(n ast.Node, push bool) bool {
// we only need to look at each node once
if !push {
return false
}
if shouldSkipTheContainingFile(n) {
return false
}
// skip blocks that can contain string literals but are not otherwise
// interesting for us
switch n.(type) {
case *ast.ImportSpec, *ast.TypeSpec:
// import blocks, type declarations
return false
}
// and don't look inside escape hatches
referentFQN := c.getFullyQualifiedNameOfReferent(n)
if referentFQN != "" {
_, isEscapeHatch := escapeHatchesSet[referentFQN]
// if isEscapeHatch: don't recurse (false)
return !isEscapeHatch
}
// check only string literals
lit, ok := n.(*ast.BasicLit)
if !ok {
return true
}
if lit.Kind != token.STRING {
return true
}
// report string literals containing characters of given script (in
// the sense of "writing system")
if charRE.MatchString(lit.Value) {
match := charRE.FindIndex([]byte(lit.Value))
matchCh := []byte(lit.Value)[match[0]:match[1]]
scriptName := usq.queryScriptForRuneBytes(matchCh)
c.p.Report(analysis.Diagnostic{
Pos: lit.Pos() + token.Pos(match[0]),
End: lit.Pos() + token.Pos(match[1]),
Message: fmt.Sprintf("string literal contains rune in %s script", scriptName),
})
}
return true
})
if !c.cfg.AllowTimeLocal {
// check time.Local usages
insp.Nodes([]ast.Node{(*ast.Ident)(nil)}, func(n ast.Node, push bool) bool {
// we only need to look at each node once
if !push {
return false
}
if shouldSkipTheContainingFile(n) {
return false
}
ident := n.(*ast.Ident)
d := c.p.TypesInfo.ObjectOf(ident)
if d == nil || d.Pkg() == nil {
return true
}
if d.Pkg().Path() == "time" && d.Name() == "Local" {
c.p.Report(analysis.Diagnostic{
Pos: n.Pos(),
End: n.End(),
Message: "usage of time.Local",
})
}
return true
})
}
return nil, nil
}
func (c *processCtx) getFullyQualifiedNameOfReferent(n ast.Node) string {
var ident *ast.Ident
switch e := n.(type) {
case *ast.CallExpr:
ident = getIdentOfTypeOfExpr(e.Fun)
case *ast.CompositeLit:
ident = getIdentOfTypeOfExpr(e.Type)
default:
return ""
}
referent := c.p.TypesInfo.Uses[ident]
if referent == nil {
return ""
}
return getFullyQualifiedName(referent)
}
func getIdentOfTypeOfExpr(e ast.Expr) *ast.Ident {
switch x := e.(type) {
case *ast.Ident:
return x
case *ast.SelectorExpr:
return x.Sel
}
return nil
}
type unicodeScriptQuerier struct {
sets map[string]runes.Set
}
func newUnicodeScriptQuerier(scriptNames []string) *unicodeScriptQuerier {
sets := make(map[string]runes.Set, len(scriptNames))
for _, s := range scriptNames {
sets[s] = runes.In(unicode.Scripts[s])
}
return &unicodeScriptQuerier{
sets: sets,
}
}
func (x *unicodeScriptQuerier) queryScriptForRuneBytes(b []byte) string {
r := []rune(string(b))[0]
for s, set := range x.sets {
if set.Contains(r) {
return s
}
}
return ""
}