Skip to content

Commit e99d580

Browse files
committed
feat: use inspect.Analyzer for optimized traversal
1 parent eab065b commit e99d580

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

asciicheck.go

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,72 @@ import (
66
"go/token"
77

88
"golang.org/x/tools/go/analysis"
9+
"golang.org/x/tools/go/analysis/passes/inspect"
10+
"golang.org/x/tools/go/ast/inspector"
911
)
1012

1113
func NewAnalyzer() *analysis.Analyzer {
1214
return &analysis.Analyzer{
13-
Name: "asciicheck",
14-
Doc: "checks that all code identifiers does not have non-ASCII symbols in the name",
15-
Run: run,
15+
Name: "asciicheck",
16+
Doc: "checks that all code identifiers does not have non-ASCII symbols in the name",
17+
Requires: []*analysis.Analyzer{inspect.Analyzer},
18+
Run: run,
1619
}
1720
}
1821

1922
func run(pass *analysis.Pass) (any, error) {
20-
for _, file := range pass.Files {
21-
ast.Inspect(
22-
file, func(node ast.Node) bool {
23-
cb(pass, node)
24-
return true
25-
},
26-
)
23+
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
24+
25+
nodeFilter := []ast.Node{
26+
(*ast.File)(nil),
27+
(*ast.ImportSpec)(nil),
28+
(*ast.TypeSpec)(nil),
29+
(*ast.ValueSpec)(nil),
30+
(*ast.FuncDecl)(nil),
31+
(*ast.StructType)(nil),
32+
(*ast.FuncType)(nil),
33+
(*ast.InterfaceType)(nil),
34+
(*ast.LabeledStmt)(nil),
35+
(*ast.AssignStmt)(nil),
2736
}
28-
return nil, nil
29-
}
3037

31-
func cb(pass *analysis.Pass, n ast.Node) {
32-
switch n := n.(type) {
33-
case *ast.File:
34-
checkIdent(pass, n.Name)
35-
case *ast.ImportSpec:
36-
checkIdent(pass, n.Name)
37-
case *ast.TypeSpec:
38-
checkIdent(pass, n.Name)
39-
checkFieldList(pass, n.TypeParams)
40-
case *ast.ValueSpec:
41-
for _, name := range n.Names {
42-
checkIdent(pass, name)
43-
}
44-
case *ast.FuncDecl:
45-
checkIdent(pass, n.Name)
46-
checkFieldList(pass, n.Recv)
47-
case *ast.StructType:
48-
checkFieldList(pass, n.Fields)
49-
case *ast.FuncType:
50-
checkFieldList(pass, n.TypeParams)
51-
checkFieldList(pass, n.Params)
52-
checkFieldList(pass, n.Results)
53-
case *ast.InterfaceType:
54-
checkFieldList(pass, n.Methods)
55-
case *ast.LabeledStmt:
56-
checkIdent(pass, n.Label)
57-
case *ast.AssignStmt:
58-
if n.Tok == token.DEFINE {
59-
for _, expr := range n.Lhs {
60-
if ident, ok := expr.(*ast.Ident); ok {
61-
checkIdent(pass, ident)
38+
inspect.Preorder(nodeFilter, func(n ast.Node) {
39+
switch n := n.(type) {
40+
case *ast.File:
41+
checkIdent(pass, n.Name)
42+
case *ast.ImportSpec:
43+
checkIdent(pass, n.Name)
44+
case *ast.TypeSpec:
45+
checkIdent(pass, n.Name)
46+
checkFieldList(pass, n.TypeParams)
47+
case *ast.ValueSpec:
48+
for _, name := range n.Names {
49+
checkIdent(pass, name)
50+
}
51+
case *ast.FuncDecl:
52+
checkIdent(pass, n.Name)
53+
checkFieldList(pass, n.Recv)
54+
case *ast.StructType:
55+
checkFieldList(pass, n.Fields)
56+
case *ast.FuncType:
57+
checkFieldList(pass, n.TypeParams)
58+
checkFieldList(pass, n.Params)
59+
checkFieldList(pass, n.Results)
60+
case *ast.InterfaceType:
61+
checkFieldList(pass, n.Methods)
62+
case *ast.LabeledStmt:
63+
checkIdent(pass, n.Label)
64+
case *ast.AssignStmt:
65+
if n.Tok == token.DEFINE {
66+
for _, expr := range n.Lhs {
67+
if ident, ok := expr.(*ast.Ident); ok {
68+
checkIdent(pass, ident)
69+
}
6270
}
6371
}
6472
}
65-
}
73+
})
74+
return nil, nil
6675
}
6776

6877
func checkIdent(pass *analysis.Pass, v *ast.Ident) {

0 commit comments

Comments
 (0)