-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
108 lines (97 loc) · 3.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"flag"
"fmt"
"go/build"
"go/scanner"
"go/token"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/kisielk/gotool"
)
func main() {
log.SetFlags(0)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage of %s:
glyphcheck [flags] # runs on package in current directory
glyphcheck [flags] [packages] # specified as Go import paths
glyphcheck [flags] [directories] # where a '/...' suffix includes all sub-directories
glyphcheck [flags] [files] # all must belong to a single package
Flags:
`, os.Args[0])
flag.PrintDefaults()
}
scanComments := flag.Bool("comments", false, "also scan comments")
flag.Parse()
var lines []string
for _, file := range listFiles(flag.Args()) {
lines = append(lines, scanFile(file, *scanComments)...)
}
if len(lines) > 0 {
log.Fatalln(strings.Join(lines, "\n"))
}
}
// listFiles parses args as a set of Go files or import paths and returns the
// set of all Go files it references.
func listFiles(args []string) (files []string) {
if len(args) > 0 && strings.HasSuffix(args[0], ".go") {
// assume ad-hoc package
return args
}
for _, path := range gotool.ImportPaths(args) {
buildPkg, err := build.Import(path, ".", 0)
if err != nil {
log.Fatal(err)
}
for _, file := range buildPkg.GoFiles {
files = append(files, filepath.Join(buildPkg.Dir, file))
}
}
return
}
// scanFile scans file and returns formatted error strings for each line in
// file that contains a homoglyph.
func scanFile(file string, scanComments bool) (lines []string) {
src, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
var s scanner.Scanner
fset := token.NewFileSet()
tokFile := fset.AddFile("", fset.Base(), len(src))
var scanMode scanner.Mode
if scanComments {
scanMode = scanner.ScanComments
}
s.Init(tokFile, src, func(pos token.Position, msg string) {
// exit if file cannot be scanned
log.Fatalf("%v:%v: %v", pos.Filename, pos.Line, msg)
}, scanMode)
for {
pos, tok, lit := s.Scan()
if tok == token.EOF {
return
}
for _, c := range lit {
if c >= utf8.RuneSelf && isHomoglyph(c) {
lines = append(lines, fmt.Sprintf("%s:%v: %s (%#U)", file, fset.Position(pos), lit, c))
break
}
}
}
}
var homoglyphs map[rune]struct{}
func init() {
homoglyphs = make(map[rune]struct{})
for _, r := range `ᅟᅠ ㅤǃ!״″"$%&'﹝(﹞)⁎*+‚,‐𐆑-٠۔܁܂․‧。.。⁄∕╱⫻⫽/ノΟοОоՕ𐒆OoΟοОоՕ𐒆Ooا123456𐒇7Ց89։܃܄∶꞉:;;‹<𐆐=›>?@[\]^_`ÀÁÂÃÄÅàáâãäåɑΑαаᎪAaßʙΒβВЬᏴᛒBbϲϹСсᏟⅭⅽ𐒨CcĎďĐđԁժᎠⅮⅾDdÈÉÊËéêëĒēĔĕĖėĘĚěΕЕеᎬEeϜFfɡɢԌնᏀGgʜΗНһᎻHhɩΙІіاᎥᛁⅠⅰ𐒃IiϳЈјյᎫJjΚκКᏦᛕKKkʟιاᏞⅬⅼLlΜϺМᎷᛖⅯⅿMmɴΝNnΟοОоՕ𐒆OoΟοОоՕ𐒆OoΡρРрᏢPpႭႳQqʀԻᏒᚱRrЅѕՏႽᏚ𐒖SsΤτТᎢTtμυԱՍ⋃UuνѴѵᏙⅤⅴVvѡᎳWwΧχХхⅩⅹXxʏΥγуҮYyΖᏃZz{ǀا|}⁓~ӧӒӦ` {
homoglyphs[r] = struct{}{}
}
}
func isHomoglyph(r rune) bool {
_, ok := homoglyphs[r]
return ok
}