-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
276 lines (256 loc) · 7.99 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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.
// The gospel command finds and highlights misspelled words in Go source
// comments and strings. It uses hunspell to identify misspellings and only
// emits coloured output for visual inspection; don't use it in automated
// linting.
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"go/ast"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"github.com/kortschak/camel"
"github.com/kortschak/ct"
"github.com/kortschak/hunspell"
"golang.org/x/tools/go/packages"
)
func main() { os.Exit(gospel()) }
// Exit status codes.
const (
success = 0
internalError = 1 << iota
invocationError
directiveError // Currently unused. This will be for linting directives.
spellingError
)
// Suggestion behaviour.
const (
never = 0
once = 1
always = 2
)
func gospel() (status int) {
show := flag.Bool("show", true, "print comment or string with misspellings")
checkStrings := flag.Bool("check-strings", false, "check string literals")
ignoreUpper := flag.Bool("ignore-upper", true, "ignore all-uppercase words")
ignoreSingle := flag.Bool("ignore-single", true, "ignore single letter words")
ignoreIdents := flag.Bool("ignore-idents", true, "ignore words matching identifiers")
ignoreNumbers := flag.Bool("ignore-numbers", true, "ignore Go syntax number literals")
maskURLs := flag.Bool("mask-urls", true, "mask URLs in text")
camelSplit := flag.Bool("camel", true, "split words on camel case")
minNakedHex := flag.Int("min-naked-hex", 8, "length to recognize hex-digit words as number (0 is never ignore)")
maxWordLen := flag.Int("max-word-len", 40, "ignore words longer than this (0 is no limit)")
suggest := flag.Int("suggest", 0, "make suggestions for misspellings (0 - never, 1 - first instance, 2 - always)")
words := flag.String("misspellings", "", "file to write a dictionary of misspellings (.dic format)")
update := flag.Bool("update-dict", false, "update misspellings dictionary instead of creating a new one")
lang := flag.String("lang", "en_US", "language to use")
dicts := flag.String("dict-paths", path, "directory list containing hunspell dictionaries")
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.
`, os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *lang == "" {
fmt.Fprintln(os.Stderr, "missing lang flag")
return invocationError
}
if *suggest < never || always < *suggest {
fmt.Fprintln(os.Stderr, "invalid suggest flag value")
return invocationError
}
var (
spelling *hunspell.Spell
err error
)
for _, p := range filepath.SplitList(*dicts) {
if strings.HasPrefix(p, "~"+string(filepath.Separator)) {
dir, err := os.UserHomeDir()
if err != nil {
fmt.Fprintf(os.Stderr, "could not expand tilde: %v\n", err)
return 1
}
p = filepath.Join(dir, p[2:])
}
spelling, err = hunspell.NewSpell(p, *lang)
if err == nil {
break
}
}
if spelling == nil {
fmt.Fprintf(os.Stderr, "no %s dictionary found in: %v\n", *lang, *dicts)
return internalError
}
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
}
// Load known words as a dictionary. This requires a write to
// disk since hunspell does not allow dictionaries to be loaded
// from memory, and affix rules can't be provided directly.
kw, err := os.CreateTemp("", "gospel")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create known words dictionary: %v", err)
return internalError
} else {
defer os.Remove(kw.Name())
fmt.Fprintln(kw, len(knownWords))
for _, w := range knownWords {
fmt.Fprintln(kw, w)
}
err := spelling.AddDict(kw.Name())
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
}
// Load any dictionaries that exist in well known locations
// at module roots. We do not do this when we are outputting
// a misspelling list since the list will be incomplete unless
// it is appended to the existing list, unless we are making
// and updated dictionary when we will merge them.
if *words == "" || *update {
roots := make(map[string]bool)
for _, p := range pkgs {
if p.Module == nil {
continue
}
roots[p.Module.Dir] = true
}
for r := range roots {
err := spelling.AddDict(filepath.Join(r, ".words"))
if _, ok := err.(*os.PathError); !ok && err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
}
}
if *ignoreIdents {
err = addIdentifiers(spelling, pkgs, make(map[string]bool))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return internalError
}
}
// Add authors identifiers gleaned from NOTEs.
for _, p := range pkgs {
for _, f := range p.Syntax {
addNoteAuthors(spelling, f.Comments)
}
}
c := &checker{
spelling: spelling,
camel: camel.NewSplitter([]string{"\\"}),
show: *show,
ignoreUpper: *ignoreUpper,
ignoreSingle: *ignoreSingle,
ignoreNumbers: *ignoreNumbers,
maskURLs: *maskURLs,
camelSplit: *camelSplit,
maxWordLen: *maxWordLen,
minNakedHex: *minNakedHex,
makeSuggestions: *suggest,
warn: (ct.Italic | ct.Fg(ct.BoldRed)).Paint,
}
if c.show {
c.suggest = (ct.Italic | ct.Fg(ct.BoldGreen)).Paint
} else {
c.suggest = ct.Mode(0).Paint
}
if *words != "" {
c.misspelled = make(map[string]bool)
}
if *suggest != never {
c.suggested = make(map[string][]string)
}
for _, p := range pkgs {
c.fileset = p.Fset
for _, f := range p.Syntax {
if *checkStrings {
ast.Walk(c, f)
}
for _, g := range f.Comments {
// TODO(kortschak): Check each line of comment
// individually and provide ±line of context.
// This reduces output and takes the user to
// the error location more quickly. It also
// means that we can spell check reasons in
// linting and compiler directives.
c.check(g.Text(), g.Pos(), "comment")
}
}
}
if c.misspellings != 0 {
status |= spellingError
}
// Write out a dictionary of the misspelled words.
// The hunspell .dic format includes a count hint
// at the top of the file so add that as well.
if *words != "" {
if *update {
// Carry over words from the already existing dictionary.
old, err := os.Open(".words")
if err == nil {
sc := bufio.NewScanner(old)
for i := 0; sc.Scan(); i++ {
if i == 0 {
continue
}
c.misspelled[sc.Text()] = true
}
old.Close()
} else if !errors.Is(err, fs.ErrNotExist) {
fmt.Fprintf(os.Stderr, "failed to open .words file: %v", err)
return internalError
}
}
f, err := os.Create(*words)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to open misspellings file: %v", err)
return internalError
}
defer f.Close()
dict := make([]string, 0, len(c.misspelled))
for m := range c.misspelled {
dict = append(dict, m)
}
sort.Strings(dict)
fmt.Fprintln(f, len(dict))
for _, m := range dict {
fmt.Fprintln(f, m)
}
}
return status
}