Skip to content

Commit b2a65a2

Browse files
authored
Merge pull request #30 from tdakkota/refactor/do-not-use-deprecated-ast-object
refactor: get rid of deprecated `ast.Object`
2 parents a3a3252 + e99d580 commit b2a65a2

File tree

10 files changed

+137
-46
lines changed

10 files changed

+137
-46
lines changed

asciicheck.go

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,100 @@ package asciicheck
33
import (
44
"fmt"
55
"go/ast"
6+
"go/token"
7+
68
"golang.org/x/tools/go/analysis"
9+
"golang.org/x/tools/go/analysis/passes/inspect"
10+
"golang.org/x/tools/go/ast/inspector"
711
)
812

913
func NewAnalyzer() *analysis.Analyzer {
1014
return &analysis.Analyzer{
11-
Name: "asciicheck",
12-
Doc: "checks that all code identifiers does not have non-ASCII symbols in the name",
13-
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,
1419
}
1520
}
1621

17-
func run(pass *analysis.Pass) (interface{}, error) {
18-
for _, file := range pass.Files {
19-
alreadyViewed := map[*ast.Object]struct{}{}
20-
ast.Inspect(
21-
file, func(node ast.Node) bool {
22-
cb(pass, node, alreadyViewed)
23-
return true
24-
},
25-
)
22+
func run(pass *analysis.Pass) (any, error) {
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),
2636
}
2737

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+
}
70+
}
71+
}
72+
}
73+
})
2874
return nil, nil
2975
}
3076

31-
func cb(pass *analysis.Pass, n ast.Node, m map[*ast.Object]struct{}) {
32-
if v, ok := n.(*ast.Ident); ok {
33-
if _, ok := m[v.Obj]; ok {
34-
return
35-
} else {
36-
m[v.Obj] = struct{}{}
37-
}
77+
func checkIdent(pass *analysis.Pass, v *ast.Ident) {
78+
if v == nil {
79+
return
80+
}
3881

39-
ch, ascii := isASCII(v.Name)
40-
if !ascii {
41-
pass.Report(
42-
analysis.Diagnostic{
43-
Pos: v.Pos(),
44-
Message: fmt.Sprintf("identifier \"%s\" contain non-ASCII character: %#U", v.Name, ch),
45-
},
46-
)
82+
ch, ascii := isASCII(v.Name)
83+
if !ascii {
84+
pass.Report(
85+
analysis.Diagnostic{
86+
Pos: v.Pos(),
87+
Message: fmt.Sprintf("identifier \"%s\" contain non-ASCII character: %#U", v.Name, ch),
88+
},
89+
)
90+
}
91+
}
92+
93+
func checkFieldList(pass *analysis.Pass, f *ast.FieldList) {
94+
if f == nil {
95+
return
96+
}
97+
for _, f := range f.List {
98+
for _, name := range f.Names {
99+
checkIdent(pass, name)
47100
}
48101
}
49102
}

testdata/field_name.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package testdata
22

3-
type Field struct{}
4-
5-
type JustStruct struct {
6-
Tеst Field // want `identifier "Tеst" contain non-ASCII character: U\+0435 'е'`
3+
type _ struct {
4+
Tést int // want `identifier "Tést" contain non-ASCII character: U\+00E9 'é'`
5+
_, Tést2 int // want `identifier "Tést2" contain non-ASCII character: U\+00E9 'é'`
76
}

testdata/func_name.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package testdata
22

3-
func TеstFunc() { // want `identifier "TеstFunc" contain non-ASCII character: U\+0435 'е'`
3+
func TéstFunc() {} // want `identifier "TéstFunc" contain non-ASCII character: U\+00E9 'é'`
44

5+
type _ interface {
6+
TéstFunc() // want `identifier "TéstFunc" contain non-ASCII character: U\+00E9 'é'`
57
}

testdata/generic_name.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

testdata/import_name.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package testdata
2+
3+
import téstImport "fmt" // want `identifier "téstImport" contain non-ASCII character: U\+00E9 'é'`
4+
5+
var _ téstImport.Stringer

testdata/label_name.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package testdata
2+
3+
func _() {
4+
téstLabel: // want `identifier "téstLabel" contain non-ASCII character: U\+00E9 'é'`
5+
return
6+
goto téstLabel
7+
}

testdata/pkg/package_name.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package téstdata // want `identifier "téstdata" contain non-ASCII character: U\+00E9 'é'`

testdata/struct_name.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

testdata/type_name.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package testdata
2+
3+
type (
4+
_ struct{}
5+
TéstStruct struct{} // want `identifier "TéstStruct" contain non-ASCII character: U\+00E9 'é'`
6+
TéstAlias = struct{} // want `identifier "TéstAlias" contain non-ASCII character: U\+00E9 'é'`
7+
8+
_[_ any] struct{}
9+
_[TéstGeneric any] struct{} // want `identifier "TéstGeneric" contain non-ASCII character: U\+00E9 'é'`
10+
)
11+
12+
func _[TéstGeneric any]() {} // want `identifier "TéstGeneric" contain non-ASCII character: U\+00E9 'é'`

testdata/variable_name.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,30 @@ package testdata
22

33
import "fmt"
44

5-
func f() {
6-
var tеstVar int // want `identifier "tеstVar" contain non-ASCII character: U\+0435 'е'`
7-
tеstVar = 0
8-
fmt.Println(tеstVar)
5+
const téstGlobalConst = 0 // want `identifier "téstGlobalConst" contain non-ASCII character: U\+00E9 'é'`
6+
7+
var téstGlobalVar int // want `identifier "téstGlobalVar" contain non-ASCII character: U\+00E9 'é'`
8+
9+
func _() {
10+
const téstConst = 0 // want `identifier "téstConst" contain non-ASCII character: U\+00E9 'é'`
11+
12+
var téstVar int // want `identifier "téstVar" contain non-ASCII character: U\+00E9 'é'`
13+
téstVar = 0
14+
fmt.Println(téstVar)
15+
16+
var ch chan int
17+
téstVar2, _ := <-ch // want `identifier "téstVar2" contain non-ASCII character: U\+00E9 'é'`
18+
fmt.Println(téstVar2)
919
}
20+
21+
func _(téstParam int) {} // want `identifier "téstParam" contain non-ASCII character: U\+00E9 'é'`
22+
func _() (téstResult int) { return } // want `identifier "téstResult" contain non-ASCII character: U\+00E9 'é'`
23+
24+
type _ interface {
25+
m1(téstParam int) // want `identifier "téstParam" contain non-ASCII character: U\+00E9 'é'`
26+
m2() (téstResult int) // want `identifier "téstResult" contain non-ASCII character: U\+00E9 'é'`
27+
}
28+
29+
type Recv struct{}
30+
31+
func (téstRecv Recv) _() {} // want `identifier "téstRecv" contain non-ASCII character: U\+00E9 'é'`

0 commit comments

Comments
 (0)