-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
233 lines (207 loc) · 8.12 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright ©2022 Dan Kortschak. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:generate go run -tags docs gendoc.go path_linux.go suggest_string.go config.go
// The gospel command finds and highlights misspelled words in Go source
// comments, strings and embedded files. It uses hunspell to identify
// misspellings and emits coloured output for visual inspection or error
// lists for use in automated linting.
package main
import (
"flag"
"fmt"
"go/ast"
"os"
"runtime/debug"
"strings"
"github.com/BurntSushi/toml"
"golang.org/x/tools/go/packages"
)
func main() { os.Exit(gospel()) }
func gospel() (status int) {
config, status, err := loadConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return status
}
// Persisted options.
flag.BoolVar(&config.IgnoreIdents, "ignore-idents", config.IgnoreIdents, "ignore words matching identifiers")
flag.StringVar(&config.Lang, "lang", config.Lang, "language to use")
flag.BoolVar(&config.Show, "show", config.Show, "print comment or string with misspellings")
flag.BoolVar(&config.CheckStrings, "check-strings", config.CheckStrings, "check string literals")
flag.BoolVar(&config.CheckEmbedded, "check-embedded", config.CheckEmbedded, "check embedded data files")
flag.BoolVar(&config.IgnoreUpper, "ignore-upper", config.IgnoreUpper, "ignore all-uppercase words")
flag.BoolVar(&config.IgnoreSingle, "ignore-single", config.IgnoreSingle, "ignore single letter words")
flag.BoolVar(&config.IgnoreNumbers, "ignore-numbers", config.IgnoreNumbers, "ignore Go syntax number literals")
flag.BoolVar(&config.ReadLicenses, "read-licenses", config.ReadLicenses, "ignore words found in license files")
flag.BoolVar(&config.GitLog, "read-git-log", config.GitLog, "ignore author names and emails found in `git log` output")
flag.BoolVar(&config.MaskFlags, "mask-flags", config.MaskFlags, "ignore words with a leading dash")
flag.BoolVar(&config.MaskURLs, "mask-urls", config.MaskURLs, "mask URLs in text")
flag.BoolVar(&config.CheckURLs, "check-urls", config.CheckURLs, "check URLs in text with HEAD request")
flag.BoolVar(&config.CamelSplit, "camel", config.CamelSplit, "split words on camel case")
flag.BoolVar(&config.EntropyFiler.Filter, "entropy-filter", config.EntropyFiler.Filter, "filter strings and embedded files by entropy")
flag.IntVar(&config.MinNakedHex, "min-naked-hex", config.MinNakedHex, "length to recognize hex-digit words as number (0 is never ignore)")
flag.IntVar(&config.MaxWordLen, "max-word-len", config.MaxWordLen, "ignore words longer than this (0 is no limit)")
flag.Var(&config.MakeSuggestions, "suggest", "make suggestions for misspellings (never, once, each, always)")
flag.IntVar(&config.DiffContext, "diff-context", config.DiffContext, "specify number of lines of change context to include")
// Non-persisted config options.
flag.StringVar(&config.paths, "dict-paths", config.paths, "directory list containing hunspell dictionaries")
flag.StringVar(&config.words, "misspellings", "", "file to write a dictionary of misspellings (.dic format)")
flag.BoolVar(&config.update, "update-dict", false, "update misspellings dictionary instead of creating a new one")
flag.StringVar(&config.since, "since", config.since, "only consider changes since this ref (requires git)")
version := flag.Bool("version", false, "update misspellings dictionary instead of creating a new one")
writeConf := flag.Bool("write-config", false, "write config file based on flags and existing config to stdout and exit")
flag.Bool("config", true, "use config file") // Included for documentation.
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), `usage: %s [options] [packages]
The gospel program will report misspellings in Go source comments and strings.
The position of each comment block or string with misspelled a word will be
output. If the -show flag is true, the complete comment block or string will
be printed with misspelled words highlighted.
If files with the name ".words" exist at module roots, they are loaded as
dictionaries unless the misspellings flag is set without update-dict.
The ".words" file is read as a hunspell .dic format file and so requires
a non-zero numeric value on the first line. This value is a hint to hunspell
for the number of words in the dictionary and is populated correctly by the
misspellings option. The file may be edited to remove incorrect words without
requiring the hint to be adjusted.
If a .gospel.conf file exists in the root of the current module and the config
flag is true (default) it will be used to populate selected flag defaults:
show, check-strings, ignore-upper, ignore-single, ignore-numbers, mask-urls,
camel, min-naked-hex, max-word-len and suggest.
String literals can be filtered on the basis of entropy to exclude unexpectedly
high or low complexity text from spell checking. This is experimental, and may
change in behaviour in future versions.
See https://github.com/kortschak/gospel for more complete documentation.
`, os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *version {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("unknown")
return 0
}
mod := &info.Main
if mod.Replace != nil {
mod = mod.Replace
}
fmt.Printf("%s %s %s\n\n", mod.Path, mod.Version, mod.Sum)
buildSettings(info)
return 0
}
if config.Lang == "" {
fmt.Fprintln(os.Stderr, "missing lang flag")
return invocationError
}
if config.MakeSuggestions < never || always < config.MakeSuggestions {
fmt.Fprintln(os.Stderr, "invalid suggest flag value")
return invocationError
}
if strings.Contains(config.since, "..") {
fmt.Fprintln(os.Stderr, "cannot use commit range for since argument")
return invocationError
}
if *writeConf {
toml.NewEncoder(os.Stdout).Encode(config)
return success
}
cfg := &packages.Config{
Mode: packages.NeedFiles |
packages.NeedImports |
packages.NeedDeps |
packages.NeedSyntax |
packages.NeedTypes |
packages.NeedTypesInfo |
packages.NeedModule,
}
pkgs, err := packages.Load(cfg, flag.Args()...)
if err != nil {
fmt.Fprintf(os.Stderr, "load: %v\n", err)
return internalError
}
if packages.PrintErrors(pkgs) != 0 {
return internalError
}
d, err := newDictionary(pkgs, config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
c, err := newChecker(d, config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return invocationError
}
for _, p := range pkgs {
c.fileset = p.Fset
for _, f := range p.Syntax {
if !c.changeFilter.fileIsInChange(f.Pos(), c.fileset) {
continue
}
c.noteGenerated(f)
if c.CheckStrings {
ast.Walk(c, f)
}
for _, g := range f.Comments {
lastOK := true
for i, l := range g.List {
ok := c.check(l.Text, l)
// Provide context for spelling in comments.
if !ok {
if i != 0 && lastOK {
prev := g.List[i-1]
c.misspellings = append(c.misspellings, misspelling{
text: prev.Text,
pos: c.fileset.Position(prev.Pos()),
end: c.fileset.Position(prev.End()),
})
}
} else {
if !lastOK {
c.misspellings = append(c.misspellings, misspelling{
text: l.Text,
pos: c.fileset.Position(l.Pos()),
end: c.fileset.Position(l.End()),
})
}
}
lastOK = ok
}
}
}
}
if c.CheckEmbedded {
// TODO(kortschak): Remove this and use packages.Load
// when https://go.dev/issue/50720 is resolved.
embedded, err := embedFiles(flag.Args())
if err != nil {
fmt.Fprintf(os.Stdout, "could not get embedded files list: %v", err)
return internalError
}
const maxLineLen = 120 // TODO(kortschak): Consider making this configurable.
for _, path := range embedded {
e, err := c.loadEmbedded(path, maxLineLen)
if err != nil {
fmt.Fprintf(os.Stdout, "could not read embedded file: %v", err)
return internalError
}
if !c.changeFilter.fileIsInChange(e.Pos(), e) {
continue
}
c.fileset = e
c.check(e.Text(), e)
}
}
if d.misspellings != 0 {
status |= spellingError
}
c.report()
err = d.writeMisspellings()
if err != nil {
fmt.Fprintln(os.Stderr, err)
status |= internalError
}
return status
}