Skip to content

Commit eab065b

Browse files
committed
refactor: get rid of deprecated ast.Object
1 parent a3a3252 commit eab065b

10 files changed

+119
-37
lines changed

asciicheck.go

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package asciicheck
33
import (
44
"fmt"
55
"go/ast"
6+
"go/token"
7+
68
"golang.org/x/tools/go/analysis"
79
)
810

@@ -14,36 +16,78 @@ func NewAnalyzer() *analysis.Analyzer {
1416
}
1517
}
1618

17-
func run(pass *analysis.Pass) (interface{}, error) {
19+
func run(pass *analysis.Pass) (any, error) {
1820
for _, file := range pass.Files {
19-
alreadyViewed := map[*ast.Object]struct{}{}
2021
ast.Inspect(
2122
file, func(node ast.Node) bool {
22-
cb(pass, node, alreadyViewed)
23+
cb(pass, node)
2324
return true
2425
},
2526
)
2627
}
27-
2828
return nil, nil
2929
}
3030

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{}{}
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)
3743
}
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)
62+
}
63+
}
64+
}
65+
}
66+
}
67+
68+
func checkIdent(pass *analysis.Pass, v *ast.Ident) {
69+
if v == nil {
70+
return
71+
}
3872

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-
)
73+
ch, ascii := isASCII(v.Name)
74+
if !ascii {
75+
pass.Report(
76+
analysis.Diagnostic{
77+
Pos: v.Pos(),
78+
Message: fmt.Sprintf("identifier \"%s\" contain non-ASCII character: %#U", v.Name, ch),
79+
},
80+
)
81+
}
82+
}
83+
84+
func checkFieldList(pass *analysis.Pass, f *ast.FieldList) {
85+
if f == nil {
86+
return
87+
}
88+
for _, f := range f.List {
89+
for _, name := range f.Names {
90+
checkIdent(pass, name)
4791
}
4892
}
4993
}

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)