@@ -6,63 +6,72 @@ import (
6
6
"go/token"
7
7
8
8
"golang.org/x/tools/go/analysis"
9
+ "golang.org/x/tools/go/analysis/passes/inspect"
10
+ "golang.org/x/tools/go/ast/inspector"
9
11
)
10
12
11
13
func NewAnalyzer () * analysis.Analyzer {
12
14
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 ,
16
19
}
17
20
}
18
21
19
22
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 ),
27
36
}
28
- return nil , nil
29
- }
30
37
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
+ }
62
70
}
63
71
}
64
72
}
65
- }
73
+ })
74
+ return nil , nil
66
75
}
67
76
68
77
func checkIdent (pass * analysis.Pass , v * ast.Ident ) {
0 commit comments