-
Notifications
You must be signed in to change notification settings - Fork 4
/
checker.go
294 lines (265 loc) · 6.95 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
// 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"
"os"
"path/filepath"
"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 *token.FileSet
dictionary *dictionary
camel camel.Splitter
heuristics []heuristic
config
misspellings []misspelling
suggested map[string][]string
// warn is the decoration for incorrectly spelled words.
warn func(...interface{}) fmt.Formatter
// suggest is the decoration for suggested words.
suggest func(...interface{}) fmt.Formatter
}
// 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{},
},
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 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)
}
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, where string) (ok bool) {
sc := bufio.NewScanner(c.textReader(text))
w := words{}
sc.Split(w.ScanWords)
var words []misspelled
for sc.Scan() {
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
}
words = append(words, misspelled{word: word, span: w.current})
}
if len(words) != 0 {
c.misspellings = append(c.misspellings, misspelling{
words: words,
where: where,
text: text,
pos: c.fileset.Position(node.Pos()),
end: c.fileset.Position(node.End()),
})
}
return len(words) == 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
}
// urls is used for masking URLs in check.
var urls = xurls.Strict()
// 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 {
masked := urls.ReplaceAllStringFunc(text, func(s string) string {
return strings.Repeat(" ", len(s))
})
return strings.NewReader(masked)
}
return strings.NewReader(text)
}
// 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, "string")
}
return c
}
// unexpectedEntropy returns whether the text falls outside the expected
// ranges for text.
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 == '_' })
}