-
Notifications
You must be signed in to change notification settings - Fork 4
/
checker.go
443 lines (403 loc) · 11 KB
/
checker.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
// 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"
"fmt"
"go/ast"
"go/token"
"io"
"math"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"unicode"
"github.com/kortschak/camel"
"github.com/kortschak/ct"
"mvdan.cc/xurls/v2"
)
// checker implements an AST-walking spell checker.
type checker struct {
fileset positioner
dictionary *dictionary
camel camel.Splitter
heuristics []heuristic
changeFilter changeFilter
config
misspellings []misspelling
suggested map[string][]string
// generated is the set of files that have code generation
// comments.
generated map[string]bool
// warn is the decoration for incorrectly spelled words.
// Warnings are colour-differentiated based on whether the
// source is generated code.
warn map[bool]func(...interface{}) fmt.Formatter
// suggest is the decoration for suggested words.
suggest func(...interface{}) fmt.Formatter
}
// positioner is an abstraction of the token.FileSet Position method.
type positioner interface {
Position(token.Pos) token.Position
}
// newChecker returns a new spelling checker using the provided spelling
// and configuration.
func newChecker(d *dictionary, cfg config) (*checker, error) {
c := &checker{
dictionary: d,
config: cfg,
camel: camel.NewSplitter([]string{"\\"}),
heuristics: []heuristic{
wordLen{cfg.MaxWordLen},
isNakedHex{cfg.MinNakedHex},
isHexRune{},
isUnit{},
},
generated: make(map[string]bool),
warn: map[bool]func(...interface{}) fmt.Formatter{
false: (ct.Italic | ct.Fg(ct.BoldRed)).Paint, // Not generated code.
true: (ct.Italic | ct.Fg(ct.BoldYellow)).Paint, // Generated code.
},
}
if c.Show {
c.suggest = (ct.Italic | ct.Fg(ct.BoldGreen)).Paint
} else {
c.suggest = ct.Mode(0).Paint
}
if c.MakeSuggestions != never {
c.suggested = make(map[string][]string)
}
// Add optional heuristics.
if c.IgnoreUpper {
c.heuristics = append(c.heuristics, allUpper{})
}
if c.IgnoreSingle {
c.heuristics = append(c.heuristics, isSingle{})
}
if c.IgnoreNumbers {
c.heuristics = append(c.heuristics, &isNumber{})
}
if len(c.Patterns) != 0 {
p, err := newPatterns(c.Patterns)
if err != nil {
return nil, err
}
c.heuristics = append(c.heuristics, p)
}
if c.since != "" {
new, err := gitAdditionsSince(c.since, c.DiffContext)
if err != nil {
return nil, err
}
c.changeFilter = new
}
return c, nil
}
// check checks the provided text and outputs information about any misspellings
// in the text.
func (c *checker) check(text string, node ast.Node) (ok bool) {
var misspellings []misspelled
if c.CheckURLs {
misspellings = c.confirmURLtargets(misspellings, text, node)
}
sc := bufio.NewScanner(c.textReader(text))
w := words{}
sc.Split(w.ScanWords)
for sc.Scan() {
if !c.changeFilter.isInChange(node.Pos()+token.Pos(w.current.pos), c.fileset) {
continue
}
word := sc.Text()
// Remove common suffixes from words.
// Note that prefix removal cannot be
// done without adjusting the word's
// start position.
switch {
case strings.HasSuffix(word, "'s"):
word = strings.TrimSuffix(word, "'s")
case strings.HasSuffix(word, "'d"):
word = strings.TrimSuffix(word, "'d")
case strings.HasSuffix(word, "'ed"):
word = strings.TrimSuffix(word, "'ed")
case strings.HasSuffix(word, "'th"):
word = strings.TrimSuffix(word, "'th")
}
if c.isCorrect(stripUnderscores(word), false) {
continue
}
misspellings = append(misspellings, misspelled{
word: word,
span: w.current,
note: "misspelled",
suggest: true,
})
}
if len(misspellings) != 0 {
c.misspellings = append(c.misspellings, misspelling{
words: misspellings,
where: where(node),
text: text,
pos: c.fileset.Position(node.Pos()),
end: c.fileset.Position(node.End()),
})
}
return len(misspellings) == 0
}
// rel returns the wd-relative path for the input if possible.
func rel(path string) string {
wd, err := os.Getwd()
if err != nil {
return path
}
rel, err := filepath.Rel(wd, path)
if err != nil {
return path
}
return rel
}
// where returns a string representation of the class of syntax
// component where the misspelling was identified.
func where(n ast.Node) string {
switch n.(type) {
case *ast.Comment:
return "comment"
case *ast.BasicLit:
return "string"
case *embedded:
return "embedded file"
default:
return fmt.Sprintf("unexpected node type: %T", n)
}
}
// genNote is the specified pattern for generated code notes. See output
// of go help generate.
var genNote = regexp.MustCompile("^// Code generated .* DO NOT EDIT.$")
// noteGenerated collects the set of files that have been marked as generated.
func (c *checker) noteGenerated(f *ast.File) {
for _, g := range f.Comments {
if g.Pos() > f.Package {
// From go help generate:
//
// This line must appear before the first non-comment, non-blank
// text in the file.
//
// Though not all packages adhere to this and place it after
// the package decl. Leave those files as non-generated to send
// a signal to repair that.
return
}
for _, cm := range g.List {
if genNote.MatchString(cm.Text) {
c.generated[c.fileset.Position(f.Pos()).Filename] = true
return
}
}
}
}
var (
// urls is used for masking URLs in check.
urls = xurls.Strict()
// flags is used for masking flags in check.
flags = regexp.MustCompile(`(?:^|\s)(?:-{1,2}\w+)+\b`)
)
// textReader returns an io.Reader containing the provided text conditioned
// according to the configuration.
func (c *checker) textReader(text string) io.Reader {
if c.MaskURLs {
text = urls.ReplaceAllStringFunc(text, func(s string) string {
return strings.Repeat(" ", len(s))
})
}
if c.MaskFlags {
text = flags.ReplaceAllStringFunc(text, func(s string) string {
// We don't have a \b for boundaries with dash
// so we may be replacing a space-class rune
// here with a space. This doesn't matter since
// we are scanning this text.
return strings.Repeat(" ", len(s))
})
}
return strings.NewReader(text)
}
// confirmURLtargets fills and returns dst with a list of unreachable URL
// targets with the HTTP status or error reasons included.
func (c *checker) confirmURLtargets(dst []misspelled, text string, node ast.Node) []misspelled {
for _, idx := range urls.FindAllStringIndex(text, -1) {
if !c.changeFilter.isInChange(node.Pos()+token.Pos(idx[0]), c.fileset) {
continue
}
u := text[idx[0]:idx[1]]
if c.dictionary.ignoredURLs[u] {
continue
}
parsed, err := url.Parse(u)
if err != nil {
continue
}
switch parsed.Scheme {
case "http", "https":
default:
continue
}
// While servers may treat GET and HEAD differently, resulting
// in false positives and negatives, use of HEAD is justified by
// https://datatracker.ietf.org/doc/html/rfc2616/#section-9.4.
//
// This method is often used for testing hypertext links for
// validity, accessibility, and recent modification.
//
resp, err := http.Head(u)
if err != nil {
dst = append(dst, misspelled{
word: u,
span: span{pos: idx[0], end: idx[1]},
note: fmt.Sprintf("unreachable (%v)", err),
})
c.dictionary.noteMisspelling(u)
continue
}
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
switch statusClass := resp.StatusCode / 100; statusClass {
case 4, 5:
dst = append(dst, misspelled{
word: u,
span: span{pos: idx[0], end: idx[1]},
note: fmt.Sprintf("unreachable (%v)", resp.Status),
})
c.dictionary.noteMisspelling(u)
}
}
return dst
}
// empty is a word suggestion sentinel indicating that previous suggestion
// has been made.
var empty = []string{}
// isCorrect performs the word correctness checks for checker.
func (c *checker) isCorrect(word string, partial bool) bool {
for _, h := range c.heuristics {
if h.isAcceptable(word, partial) {
return true
}
}
if c.dictionary.IsCorrect(word) {
return true
}
if partial || c.caseFoldMatch(word) {
// TODO(kortschak): Consider not adding case-fold
// matches to the misspelled map.
c.dictionary.noteMisspelling(word)
return false
}
var fragments []string
if c.CamelSplit {
// TODO(kortschak): Allow user-configurable
// known words for camel case splitting.
fragments = c.camel.Split(word)
} else {
fragments = strings.Split(word, "_")
}
for _, frag := range fragments {
if !c.isCorrect(frag, true) {
return false
}
}
return true
}
// caseFoldMatch returns whether there is a suggestion for the word that
// is an exact match under case folding. This checks for the common error
// of failing to adjust export visibility of labels in comments.
func (c *checker) caseFoldMatch(word string) bool {
for _, suggest := range c.dictionary.Suggest(word) {
if strings.EqualFold(suggest, word) {
return true
}
}
return false
}
// Visit walks the AST performing spell checking on any string literals.
func (c *checker) Visit(n ast.Node) ast.Visitor {
if n, ok := n.(*ast.BasicLit); ok && n.Kind == token.STRING {
isDoubleQuoted := n.Value[0] == '"'
text := n.Value
if isDoubleQuoted {
var err error
text, err = strconv.Unquote(text)
if err != nil {
// This should never happen.
isDoubleQuoted = false
text = n.Value
}
}
if c.unexpectedEntropy(text, isDoubleQuoted) {
return c
}
c.check(n.Value, n)
}
return c
}
// unexpectedEntropy returns whether the text falls outside the expected
// ranges for text. If print is true only printable bytes are considered
// when calculating entropy.
func (c *checker) unexpectedEntropy(text string, print bool) bool {
if !c.EntropyFiler.Filter || len(text) < c.EntropyFiler.MinLenFiltered {
return false
}
e := entropy(text, print)
low := expectedEntropy(len(text), c.EntropyFiler.Accept.Low)
high := expectedEntropy(len(text), c.EntropyFiler.Accept.High)
return e < low || high < e
}
// entropy returns the entropy of the provided text in bits. If
// print is true, non-printable characters are grouped into a single
// class.
func entropy(text string, print bool) float64 {
if text == "" {
return 0
}
var counts [256]float64
for _, b := range []byte(text) {
if print && !unicode.IsPrint(rune(b)) {
continue
}
counts[b]++
}
n := len(text)
// e = -∑i=1..k((p_i)*log(p_i))
var e float64
for _, cnt := range counts {
if cnt == 0 {
// Ignore zero counts.
continue
}
p := cnt / float64(n)
e += p * math.Log2(p)
}
if e == 0 {
// Don't negate zero.
return 0
}
return -e
}
// expectedEntropy returns the expected entropy for a sequence of n letters
// uniformly chosen from an alphabet of s letters.
func expectedEntropy(n, s int) float64 {
if n > s {
n = s
}
if n < 2 {
return 0
}
return -math.Log2(1 / float64(n))
}
// stripUnderscores removes leading and trailing underscores from
// words to prevent emph marking used in comments from preventing
// spell check matching.
func stripUnderscores(s string) string {
return strings.TrimFunc(s, func(r rune) bool { return r == '_' })
}