Skip to content

Commit 7b2a63d

Browse files
committed
Fix #100: misspell linter support
1 parent 88ebabc commit 7b2a63d

36 files changed

+33834
-9
lines changed

.golangci.example.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ linters-settings:
9999
include-go-root: false
100100
packages:
101101
- github.com/davecgh/go-spew/spew
102+
misspell:
103+
# Correct spellings using locale preferences for US or UK.
104+
# Default is to use a neutral variety of English.
105+
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
106+
locale: US
102107

103108
linters:
104109
enable:

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ linters-settings:
1818
# logging is allowed only by logutils.Log, logrus
1919
# is allowed to use only in logutils package
2020
- github.com/sirupsen/logrus
21+
misspell:
22+
locale: US
2123

2224
linters:
2325
enable-all: true

Gopkg.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ test:
44
GL_TEST_RUN=1 golangci-lint run --no-config -v
55
GL_TEST_RUN=1 go test -v ./...
66

7+
test_linters:
8+
GL_TEST_RUN=1 go test -v ./test -count 1 -run TestSourcesFromTestdataWithIssuesDir/$T
9+
710
assets:
811
svg-term --cast=183662 --out docs/demo.svg --window --width 110 --height 30 --from 2000 --to 20000 --profile Dracula --term iterm2
912

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ goimports: Goimports does everything that gofmt does. Additionally it checks unu
113113
maligned: Tool to detect Go structs that would take less memory if their fields were sorted [fast: false]
114114
megacheck: 3 sub-linters in one: unused, gosimple and staticcheck [fast: false]
115115
depguard: Go linter that checks if package imports are in a list of acceptable packages [fast: false]
116+
misspell: Finds commonly misspelled English words in comments [fast: true]
116117
```
117118

118119
Pass `-E/--enable` to enable linter and `-D/--disable` to disable:
@@ -218,6 +219,7 @@ golangci-lint linters
218219
- [maligned](https://github.com/mdempsky/maligned) - Tool to detect Go structs that would take less memory if their fields were sorted
219220
- [megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck) - 3 sub-linters in one: unused, gosimple and staticcheck
220221
- [depguard](https://github.com/OpenPeeDeeP/depguard) - Go linter that checks if package imports are in a list of acceptable packages
222+
- [misspell](https://github.com/client9/misspell) - Finds commonly misspelled English words in comments
221223

222224
# Configuration
223225
The config file has lower priority than command-line options. If the same bool/string/int option is provided on the command-line
@@ -415,6 +417,11 @@ linters-settings:
415417
include-go-root: false
416418
packages:
417419
- github.com/davecgh/go-spew/spew
420+
misspell:
421+
# Correct spellings using locale preferences for US or UK.
422+
# Default is to use a neutral variety of English.
423+
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
424+
locale: US
418425

419426
linters:
420427
enable:
@@ -487,6 +494,8 @@ linters-settings:
487494
# logging is allowed only by logutils.Log, logrus
488495
# is allowed to use only in logutils package
489496
- github.com/sirupsen/logrus
497+
misspell:
498+
locale: US
490499

491500
linters:
492501
enable-all: true
@@ -591,6 +600,7 @@ Thanks to developers and authors of used linters:
591600
- [remyoudompheng](https://github.com/remyoudompheng)
592601
- [alecthomas](https://github.com/alecthomas)
593602
- [OpenPeeDeeP](https://github.com/OpenPeeDeeP)
603+
- [client9](https://github.com/client9)
594604

595605
# Future Plans
596606
1. Upstream all changes of forked linters.

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ type LintersSettings struct {
153153
Packages []string
154154
IncludeGoRoot bool `mapstructure:"include-go-root"`
155155
}
156+
Misspell struct {
157+
Locale string
158+
}
156159
}
157160

158161
type Linters struct {

pkg/golinters/misspell.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package golinters
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"go/token"
7+
"io/ioutil"
8+
"strings"
9+
10+
"github.com/client9/misspell"
11+
"github.com/golangci/golangci-lint/pkg/lint/linter"
12+
"github.com/golangci/golangci-lint/pkg/result"
13+
)
14+
15+
type Misspell struct{}
16+
17+
func (Misspell) Name() string {
18+
return "misspell"
19+
}
20+
21+
func (Misspell) Desc() string {
22+
return "Finds commonly misspelled English words in comments"
23+
}
24+
25+
func (lint Misspell) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
26+
r := misspell.Replacer{
27+
Replacements: misspell.DictMain,
28+
}
29+
30+
// Figure out regional variations
31+
locale := lintCtx.Settings().Misspell.Locale
32+
switch strings.ToUpper(locale) {
33+
case "":
34+
// nothing
35+
case "US":
36+
r.AddRuleList(misspell.DictAmerican)
37+
case "UK", "GB":
38+
r.AddRuleList(misspell.DictBritish)
39+
case "NZ", "AU", "CA":
40+
return nil, fmt.Errorf("unknown locale: %q", locale)
41+
}
42+
43+
r.Compile()
44+
45+
var res []result.Issue
46+
for _, f := range lintCtx.PkgProgram.Files(lintCtx.Cfg.Run.AnalyzeTests) {
47+
fileContent, err := ioutil.ReadFile(f)
48+
if err != nil {
49+
return nil, fmt.Errorf("can't read file %s: %s", f, err)
50+
}
51+
52+
_, diffs := r.ReplaceGo(string(fileContent))
53+
for _, diff := range diffs {
54+
text := fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, diff.Corrected)
55+
pos := token.Position{
56+
Filename: f,
57+
Line: diff.Line,
58+
Column: diff.Column + 1,
59+
}
60+
res = append(res, result.Issue{
61+
Pos: pos,
62+
Text: text,
63+
FromLinter: lint.Name(),
64+
})
65+
}
66+
}
67+
68+
return res, nil
69+
}

pkg/lint/lintersdb/lintersdb.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ func GetAllSupportedLinterConfigs() []linter.Config {
162162
WithPresets(linter.PresetStyle).
163163
WithSpeed(6).
164164
WithURL("https://github.com/OpenPeeDeeP/depguard"),
165+
linter.NewConfig(golinters.Misspell{}).
166+
WithPresets(linter.PresetStyle).
167+
WithSpeed(7).
168+
WithURL("https://github.com/client9/misspell"),
165169
}
166170

167171
if os.Getenv("GOLANGCI_COM_RUN") == "1" {
@@ -170,6 +174,7 @@ func GetAllSupportedLinterConfigs() []linter.Config {
170174
golinters.Dupl{}.Name(): true, // annoying
171175
golinters.Maligned{}.Name(): true, // rarely usable
172176
golinters.TypeCheck{}.Name(): true, // annoying because of different building envs
177+
golinters.Misspell{}.Name(): true, // unsure about false-positives number
173178
}
174179
return enableLinterConfigs(lcs, func(lc *linter.Config) bool {
175180
return !disabled[lc.Linter.Name()]

pkg/lint/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type lintRes struct {
6969
func (r Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, lc linter.Config) (ret []result.Issue, err error) {
7070
defer func() {
7171
if panicData := recover(); panicData != nil {
72-
err = fmt.Errorf("panic occured: %s", panicData)
72+
err = fmt.Errorf("panic occurred: %s", panicData)
7373
r.Log.Warnf("Panic stack trace: %s", debug.Stack())
7474
}
7575
}()
@@ -240,7 +240,7 @@ func (r Runner) Run(ctx context.Context, linters []linter.Config, lintCtx *linte
240240
lintResultsCh := r.runWorkers(ctx, lintCtx, linters)
241241
processedLintResultsCh := r.processLintResults(ctx, lintResultsCh)
242242
if ctx.Err() != nil {
243-
// XXX: always process issues, even if timeout occured
243+
// XXX: always process issues, even if timeout occurred
244244
finishedLintersN := 0
245245
for range processedLintResultsCh {
246246
finishedLintersN++

pkg/logutils/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
LogLevelInfo LogLevel = 1
2222

2323
// hidden errors: non critical errors: work can be continued, no need to fail whole program;
24-
// tests will crash if any warning occured.
24+
// tests will crash if any warning occurred.
2525
LogLevelWarn LogLevel = 2
2626

2727
// only not hidden from user errors: whole program failing, usually

0 commit comments

Comments
 (0)