Skip to content

Commit

Permalink
Merge pull request #30 from tdakkota/refactor/do-not-use-deprecated-a…
Browse files Browse the repository at this point in the history
…st-object

refactor: get rid of deprecated `ast.Object`
  • Loading branch information
tdakkota authored Feb 15, 2025
2 parents a3a3252 + e99d580 commit b2a65a2
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 46 deletions.
107 changes: 80 additions & 27 deletions asciicheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,100 @@ package asciicheck
import (
"fmt"
"go/ast"
"go/token"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

func NewAnalyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "asciicheck",
Doc: "checks that all code identifiers does not have non-ASCII symbols in the name",
Run: run,
Name: "asciicheck",
Doc: "checks that all code identifiers does not have non-ASCII symbols in the name",
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
}

func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
alreadyViewed := map[*ast.Object]struct{}{}
ast.Inspect(
file, func(node ast.Node) bool {
cb(pass, node, alreadyViewed)
return true
},
)
func run(pass *analysis.Pass) (any, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
(*ast.File)(nil),
(*ast.ImportSpec)(nil),
(*ast.TypeSpec)(nil),
(*ast.ValueSpec)(nil),
(*ast.FuncDecl)(nil),
(*ast.StructType)(nil),
(*ast.FuncType)(nil),
(*ast.InterfaceType)(nil),
(*ast.LabeledStmt)(nil),
(*ast.AssignStmt)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.File:
checkIdent(pass, n.Name)
case *ast.ImportSpec:
checkIdent(pass, n.Name)
case *ast.TypeSpec:
checkIdent(pass, n.Name)
checkFieldList(pass, n.TypeParams)
case *ast.ValueSpec:
for _, name := range n.Names {
checkIdent(pass, name)
}
case *ast.FuncDecl:
checkIdent(pass, n.Name)
checkFieldList(pass, n.Recv)
case *ast.StructType:
checkFieldList(pass, n.Fields)
case *ast.FuncType:
checkFieldList(pass, n.TypeParams)
checkFieldList(pass, n.Params)
checkFieldList(pass, n.Results)
case *ast.InterfaceType:
checkFieldList(pass, n.Methods)
case *ast.LabeledStmt:
checkIdent(pass, n.Label)
case *ast.AssignStmt:
if n.Tok == token.DEFINE {
for _, expr := range n.Lhs {
if ident, ok := expr.(*ast.Ident); ok {
checkIdent(pass, ident)
}
}
}
}
})
return nil, nil
}

func cb(pass *analysis.Pass, n ast.Node, m map[*ast.Object]struct{}) {
if v, ok := n.(*ast.Ident); ok {
if _, ok := m[v.Obj]; ok {
return
} else {
m[v.Obj] = struct{}{}
}
func checkIdent(pass *analysis.Pass, v *ast.Ident) {
if v == nil {
return
}

ch, ascii := isASCII(v.Name)
if !ascii {
pass.Report(
analysis.Diagnostic{
Pos: v.Pos(),
Message: fmt.Sprintf("identifier \"%s\" contain non-ASCII character: %#U", v.Name, ch),
},
)
ch, ascii := isASCII(v.Name)
if !ascii {
pass.Report(
analysis.Diagnostic{
Pos: v.Pos(),
Message: fmt.Sprintf("identifier \"%s\" contain non-ASCII character: %#U", v.Name, ch),
},
)
}
}

func checkFieldList(pass *analysis.Pass, f *ast.FieldList) {
if f == nil {
return
}
for _, f := range f.List {
for _, name := range f.Names {
checkIdent(pass, name)
}
}
}
7 changes: 3 additions & 4 deletions testdata/field_name.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package testdata

type Field struct{}

type JustStruct struct {
Tеst Field // want `identifier "Tеst" contain non-ASCII character: U\+0435 'е'`
type _ struct {
Tést int // want `identifier "Tést" contain non-ASCII character: U\+00E9 'é'`
_, Tést2 int // want `identifier "Tést2" contain non-ASCII character: U\+00E9 'é'`
}
4 changes: 3 additions & 1 deletion testdata/func_name.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package testdata

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

type _ interface {
TéstFunc() // want `identifier "TéstFunc" contain non-ASCII character: U\+00E9 'é'`
}
7 changes: 0 additions & 7 deletions testdata/generic_name.go

This file was deleted.

5 changes: 5 additions & 0 deletions testdata/import_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package testdata

import téstImport "fmt" // want `identifier "téstImport" contain non-ASCII character: U\+00E9 'é'`

var _ téstImport.Stringer
7 changes: 7 additions & 0 deletions testdata/label_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testdata

func _() {
téstLabel: // want `identifier "téstLabel" contain non-ASCII character: U\+00E9 'é'`
return
goto téstLabel
}
1 change: 1 addition & 0 deletions testdata/pkg/package_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package téstdata // want `identifier "téstdata" contain non-ASCII character: U\+00E9 'é'`
3 changes: 0 additions & 3 deletions testdata/struct_name.go

This file was deleted.

12 changes: 12 additions & 0 deletions testdata/type_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package testdata

type (
_ struct{}
TéstStruct struct{} // want `identifier "TéstStruct" contain non-ASCII character: U\+00E9 'é'`
TéstAlias = struct{} // want `identifier "TéstAlias" contain non-ASCII character: U\+00E9 'é'`

_[_ any] struct{}
_[TéstGeneric any] struct{} // want `identifier "TéstGeneric" contain non-ASCII character: U\+00E9 'é'`
)

func _[TéstGeneric any]() {} // want `identifier "TéstGeneric" contain non-ASCII character: U\+00E9 'é'`
30 changes: 26 additions & 4 deletions testdata/variable_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@ package testdata

import "fmt"

func f() {
var tеstVar int // want `identifier "tеstVar" contain non-ASCII character: U\+0435 'е'`
tеstVar = 0
fmt.Println(tеstVar)
const téstGlobalConst = 0 // want `identifier "téstGlobalConst" contain non-ASCII character: U\+00E9 'é'`

var téstGlobalVar int // want `identifier "téstGlobalVar" contain non-ASCII character: U\+00E9 'é'`

func _() {
const téstConst = 0 // want `identifier "téstConst" contain non-ASCII character: U\+00E9 'é'`

var téstVar int // want `identifier "téstVar" contain non-ASCII character: U\+00E9 'é'`
téstVar = 0
fmt.Println(téstVar)

var ch chan int
téstVar2, _ := <-ch // want `identifier "téstVar2" contain non-ASCII character: U\+00E9 'é'`
fmt.Println(téstVar2)
}

func _(téstParam int) {} // want `identifier "téstParam" contain non-ASCII character: U\+00E9 'é'`
func _() (téstResult int) { return } // want `identifier "téstResult" contain non-ASCII character: U\+00E9 'é'`

type _ interface {
m1(téstParam int) // want `identifier "téstParam" contain non-ASCII character: U\+00E9 'é'`
m2() (téstResult int) // want `identifier "téstResult" contain non-ASCII character: U\+00E9 'é'`
}

type Recv struct{}

func (téstRecv Recv) _() {} // want `identifier "téstRecv" contain non-ASCII character: U\+00E9 'é'`

0 comments on commit b2a65a2

Please sign in to comment.