1package asciicheck 2 3import ( 4 "fmt" 5 "go/ast" 6 "golang.org/x/tools/go/analysis" 7) 8 9func NewAnalyzer() *analysis.Analyzer { 10 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, 14 } 15} 16 17func 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 ) 26 } 27 28 return nil, nil 29} 30 31func 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 } 38 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 ) 47 } 48 } 49} 50