Skip to content

Commit 8e5699b

Browse files
committed
Fix #100: misspell linter support
1 parent 88ebabc commit 8e5699b

36 files changed

+33834
-9
lines changed

Diff for: .golangci.example.yml

+5
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:

Diff for: .golangci.yml

+2
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

Diff for: Gopkg.lock

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Makefile

+3
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

Diff for: README.md

+10
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.

Diff for: pkg/config/config.go

+3
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 {

Diff for: pkg/golinters/misspell.go

+69
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+
}

Diff for: pkg/lint/lintersdb/lintersdb.go

+5
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()]

Diff for: pkg/lint/runner.go

+2-2
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++

Diff for: pkg/logutils/log.go

+1-1
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

Diff for: pkg/packages/package.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package packages
33
import (
44
"go/build"
55
"path/filepath"
6+
7+
"github.com/golangci/golangci-lint/pkg/result/processors"
68
)
79

810
type Package struct {
@@ -13,7 +15,13 @@ type Package struct {
1315
}
1416

1517
func (pkg *Package) Files(includeTest bool) []string {
16-
pkgFiles := append([]string{}, pkg.bp.GoFiles...)
18+
var pkgFiles []string
19+
for _, f := range pkg.bp.GoFiles {
20+
if !processors.IsCgoFilename(f) {
21+
// skip cgo at all levels to prevent failures on file reading
22+
pkgFiles = append(pkgFiles, f)
23+
}
24+
}
1725

1826
// TODO: add cgo files
1927
if includeTest {

Diff for: pkg/result/processors/cgo.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package processors
22

33
import (
4-
"strings"
5-
64
"github.com/golangci/golangci-lint/pkg/result"
75
)
86

@@ -23,7 +21,7 @@ func (p Cgo) Process(issues []result.Issue) ([]result.Issue, error) {
2321
return filterIssues(issues, func(i *result.Issue) bool {
2422
// some linters (.e.g gas, deadcode) return incorrect filepaths for cgo issues,
2523
// it breaks next processing, so skip them
26-
return i.FilePath() != "C" && !strings.HasSuffix(i.FilePath(), "/C")
24+
return !IsCgoFilename(i.FilePath())
2725
}), nil
2826
}
2927

Diff for: pkg/result/processors/utils.go

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package processors
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/golangci/golangci-lint/pkg/result"
78
)
@@ -44,3 +45,7 @@ func transformIssues(issues []result.Issue, transform func(i *result.Issue) *res
4445

4546
return retIssues
4647
}
48+
49+
func IsCgoFilename(f string) bool {
50+
return f == "C" || strings.HasSuffix(f, "/C")
51+
}

Diff for: test/linters_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func testSourcesFromDir(t *testing.T, dir string) {
3434

3535
for _, s := range sources {
3636
s := s
37-
t.Run(s, func(t *testing.T) {
37+
t.Run(filepath.Base(s), func(t *testing.T) {
3838
t.Parallel()
3939
testOneSource(t, s)
4040
})

Diff for: test/testdata/misspell.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// args: -Emisspell
2+
package testdata
3+
4+
func Misspell() {
5+
// comment with incorrect spelling: occured // ERROR "`occured` is a misspelling of `occurred`"
6+
}

Diff for: vendor/github.com/client9/misspell/.gitignore

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vendor/github.com/client9/misspell/.travis.yml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: vendor/github.com/client9/misspell/Dockerfile

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)