-
Notifications
You must be signed in to change notification settings - Fork 4
/
dictionary.go
471 lines (438 loc) · 11.3 KB
/
dictionary.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// 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.
package main
import (
"bufio"
"errors"
"fmt"
"go/ast"
"go/token"
"go/types"
"io"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"unicode"
"unicode/utf8"
"github.com/kortschak/hunspell"
"golang.org/x/tools/go/packages"
)
// dictionary is a spelling dictionary that can record misspelled words.
type dictionary struct {
*hunspell.Spell
config
// misspellings is the number of misspellings found.
misspellings int
// misspelled is the complete list of misspelled words
// found during the check. The words must have had any
// leading and trailing underscores removed.
misspelled map[string]bool
// roots is the set of module roots.
roots map[string]bool
// ignoredURLs is the set of URLs to omit from checking
// target validity.
ignoredURLs map[string]bool
}
// newDictionary returns a new dictionary based on the provided packages
// and configuration.
func newDictionary(pkgs []*packages.Package, cfg config) (*dictionary, error) {
d := dictionary{config: cfg}
if d.words != "" {
d.misspelled = make(map[string]bool)
}
if d.CheckURLs {
d.ignoredURLs = make(map[string]bool)
}
var (
ook librarian
aff, dic string
err error
)
for _, p := range filepath.SplitList(d.paths) {
if strings.HasPrefix(p, "~"+string(filepath.Separator)) {
dir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not expand tilde: %v", err)
}
p = filepath.Join(dir, p[2:])
}
aff, dic, err = hunspell.Paths(p, cfg.Lang)
if err != nil {
return nil, fmt.Errorf("could not find dictionary: %v", err)
}
ook, err = newLibrarian(aff, dic)
if err == nil {
for _, w := range knownWords {
err = ook.addWord(w)
if err != nil {
return nil, fmt.Errorf("%w in internal dictionary", err)
}
}
break
}
}
if ook.rules == nil {
return nil, fmt.Errorf("no %s dictionary found in: %v", d.Lang, d.paths)
}
// 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 d.words == "" || d.update {
d.roots = make(map[string]bool)
for _, p := range pkgs {
if p.Module == nil {
continue
}
d.roots[p.Module.Dir] = true
}
for r := range d.roots {
err := ook.addDictionary(filepath.Join(r, ".words"))
if _, ok := err.(*os.PathError); !ok && err != nil {
return nil, err
}
}
}
// 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 {
return nil, fmt.Errorf("failed to create known words dictionary: %v", err)
}
defer func() {
// In case we fail the write, close the file to allow
// intransigent operating systems to delete it.
kw.Close()
os.Remove(kw.Name())
}()
err = ook.writeTo(kw)
if err != nil {
return nil, fmt.Errorf("failed to write known words dictionary: %v", err)
}
dic = kw.Name()
err = kw.Close()
if err != nil {
return nil, fmt.Errorf("failed to write known words dictionary: %v", err)
}
d.Spell, err = hunspell.NewSpellPaths(aff, dic)
if err != nil {
return nil, fmt.Errorf("could not open dictionary: %v", err)
}
// Get URLs if we are ignoring them.
if d.CheckURLs {
d.ignoredURLs = ook.urls
}
ook.urls = nil
if cfg.ReadLicenses {
const licenseThreshold = 75 // Threshold for matching a license.
for r := range d.roots {
readLicenses(d.Spell, r, licenseThreshold)
}
}
if cfg.GitLog {
readGitLog(d.Spell)
}
if cfg.IgnoreIdents {
err = addIdentifiers(d.Spell, pkgs, make(map[string]bool))
if err != nil {
return nil, err
}
}
// Add authors identifiers gleaned from NOTEs.
for _, p := range pkgs {
for _, f := range p.Syntax {
addNoteAuthors(d.Spell, f.Comments)
}
}
return &d, nil
}
// noteMisspelling records the word as a misspelling if a words file was
// requested.
func (d *dictionary) noteMisspelling(word string) {
d.misspellings++
if d.misspelled != nil {
d.misspelled[word] = true
}
}
// writeMisspellings writes the recorded misspellings to the words file.
func (d *dictionary) writeMisspellings() error {
// 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 d.words != "" {
if d.update {
// Carry over words from the already existing dictionaries.
for r := range d.roots {
old, err := os.Open(filepath.Join(r, ".words"))
if err == nil {
sc := bufio.NewScanner(old)
for i := 0; sc.Scan(); i++ {
if i == 0 {
continue
}
d.misspelled[sc.Text()] = true
}
old.Close()
} else if !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to open .words file: %v", err)
}
}
}
f, err := os.Create(d.words)
if err != nil {
return fmt.Errorf("failed to open misspellings file: %v", err)
}
defer f.Close()
dict := make([]string, 0, len(d.misspelled))
for m := range d.misspelled {
dict = append(dict, m)
}
sort.Strings(dict)
_, err = fmt.Fprintln(f, len(dict))
if err != nil {
return fmt.Errorf("failed to write new dictionary: %v", err)
}
for _, m := range dict {
_, err = fmt.Fprintln(f, m)
if err != nil {
return fmt.Errorf("failed to write new dictionary: %v", err)
}
}
}
return nil
}
// addIdentifiers adds identifier labels to the spelling dictionary.
func addIdentifiers(spelling *hunspell.Spell, pkgs []*packages.Package, seen map[string]bool) error {
v := &adder{spelling: spelling}
for _, p := range pkgs {
v.pkg = p
for _, e := range strings.Split(p.String(), "/") {
if !spelling.IsCorrect(e) {
spelling.Add(e)
}
}
for _, w := range directiveWords(p.Syntax, p.Fset) {
if !spelling.IsCorrect(w) {
spelling.Add(w)
}
}
for _, f := range p.Syntax {
ast.Walk(v, f)
}
for _, dep := range p.Imports {
if seen[dep.String()] {
continue
}
seen[dep.String()] = true
addIdentifiers(spelling, []*packages.Package{dep}, seen)
}
}
if v.failed != 0 {
return errors.New("missed adding %d identifiers")
}
return nil
}
// directiveWords returns words used in directive comments.
func directiveWords(files []*ast.File, fset *token.FileSet) []string {
var words []string
for _, f := range files {
m := ast.NewCommentMap(fset, f, f.Comments)
for _, g := range m {
for _, cg := range g {
for _, c := range cg.List {
if !strings.HasPrefix(c.Text, "//") {
continue
}
text := strings.TrimPrefix(c.Text, "//")
if strings.HasPrefix(text, " ") {
continue
}
idx := strings.Index(text, ":")
if idx < 1 {
continue
}
if strings.HasPrefix(text[idx+1:], " ") {
continue
}
line := strings.SplitN(text, "\n", 2)[0]
directive := strings.SplitN(line, " ", 2)[0]
words = append(words, strings.FieldsFunc(directive, func(r rune) bool {
return unicode.IsSpace(r) || unicode.IsSymbol(r) || unicode.IsPunct(r)
})...)
}
}
}
}
return words
}
// adder is an ast.Visitor that adds tokens to a spelling dictionary.
type adder struct {
spelling *hunspell.Spell
failed int
pkg *packages.Package
}
// Visit adds the names of all identifiers to the dictionary.
func (a *adder) Visit(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.Ident:
// Check whether this is a type and only make it
// countable in that case.
ok := n.Obj != nil && n.Obj.Kind == ast.Typ
a.addWordUnknownWord(stripUnderscores(n.Name), ok)
case *ast.StructType:
typ, ok := a.pkg.TypesInfo.Types[n].Type.(*types.Struct)
if !ok {
break
}
for i := 0; i < typ.NumFields(); i++ {
f := typ.Field(i)
if !f.Exported() {
continue
}
for _, w := range extractStructTagWords(typ.Tag(i)) {
a.addWordUnknownWord(w, false)
}
}
}
return a
}
func (a *adder) addWordUnknownWord(w string, countable bool) {
if a.spelling.IsCorrect(w) {
// Assume we have the correct plurality rules.
// This should work most of the time. If it turns
// out to be a problem, we can make this conditional
// on countable and always add those terms.
return
}
var ok bool
if countable {
ok = a.spelling.AddWithAffix(w, "item")
} else {
ok = a.spelling.Add(w)
}
if !ok {
a.failed++
}
}
// a librarian collates dictionaries.
type librarian struct {
rules map[string]string
urls map[string]bool
}
// newLibrarian returns a new librarian populated with words and affix rules
// obtained from the hunspell .dic file paths provided, checking that the
// affix file aff also exists.
func newLibrarian(aff, dic string) (librarian, error) {
_, err := os.Stat(aff)
if err != nil {
return librarian{}, err
}
l := librarian{
rules: make(map[string]string),
urls: make(map[string]bool),
}
err = l.addDictionary(dic)
if err != nil {
return librarian{}, err
}
return l, nil
}
// addDictionary adds word rules from the hunspell dictionary at the given
// path.
func (l librarian) addDictionary(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
sc := bufio.NewScanner(f)
for i := 0; sc.Scan(); i++ {
if i == 0 {
// Skip word count line.
continue
}
err := l.addWord(sc.Text())
if err != nil {
return fmt.Errorf("%w at %s:%d", err, path, i+1)
}
}
return sc.Err()
}
// addWord adds the provided word to the librarian's dictionary merging any
// affix rules into those already existing for the word.
func (l librarian) addWord(w string) error {
r := strings.Split(w, "/")
word := r[0]
if word == "" {
// This should never happen, but we can ignore it.
return nil
}
var affix string
switch len(r) {
case 1:
case 2:
affix = r[1]
default:
if urls.MatchString(w) {
l.urls[w] = true
return nil
}
return fmt.Errorf("invalid dictionary entry %q", w)
}
l.rules[word] = mergeRules(l.rules[word], affix)
return nil
}
// mergeRules merges affix rules.
func mergeRules(a, b string) string {
switch {
case a == "":
return b
case b == "":
return a
default:
r := make([]rune, 0, utf8.RuneCountInString(a)+utf8.RuneCountInString(b))
r = append(r, []rune(a)...)
r = append(r, []rune(b)...)
sort.Slice(r, func(i, j int) bool { return r[i] < r[j] })
curr := 0
for i, e := range r {
if e == r[curr] {
continue
}
curr++
if curr < i {
r[curr], r[i] = r[i], 0
}
}
return string(r[:curr+1])
}
}
// writeTo writes the word rules in the librarian to the provided io.Writer
// in hunspell .dic format.
func (l librarian) writeTo(w io.Writer) error {
dict := make([]string, 0, len(l.rules))
for w, r := range l.rules {
if r != "" {
dict = append(dict, w+"/"+r)
} else {
dict = append(dict, w)
}
}
_, err := fmt.Fprintln(w, len(dict))
if err != nil {
return fmt.Errorf("failed to write new dictionary: %v", err)
}
// We don't sort here since it's for immediate consumption by hunspell.
for _, r := range dict {
_, err = fmt.Fprintln(w, r)
if err != nil {
return fmt.Errorf("failed to write new dictionary: %v", err)
}
}
return nil
}