Skip to content

Commit c7dee2c

Browse files
NiseVoidjirfag
authored andcommitted
Add whitespace linter (#673)
1 parent 31afdf8 commit c7dee2c

File tree

9 files changed

+247
-0
lines changed

9 files changed

+247
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ scopelint: Scopelint checks for unpinned variables in go programs [fast: true, a
216216
stylecheck: Stylecheck is a replacement for golint [fast: false, auto-fix: false]
217217
unconvert: Remove unnecessary type conversions [fast: true, auto-fix: false]
218218
unparam: Reports unused function parameters [fast: false, auto-fix: false]
219+
whitespace: Tool for detection of leading and trailing whitespace [fast: true, auto-fix: false]
219220
```
220221
221222
Pass `-E/--enable` to enable linter and `-D/--disable` to disable:
@@ -459,6 +460,7 @@ golangci-lint help linters
459460
- [gochecknoinits](https://github.com/leighmcculloch/gochecknoinits) - Checks that no init functions are present in Go code
460461
- [gochecknoglobals](https://github.com/leighmcculloch/gochecknoglobals) - Checks that no globals are present in Go code
461462
- [funlen](https://github.com/ultraware/funlen) - Tool for detection of long functions
463+
- [whitespace](https://github.com/ultraware/whitespace) - Tool for detection of leading and trailing whitespace
462464
463465
## Configuration
464466

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ require (
5353
github.com/stretchr/testify v1.2.2
5454
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec
5555
github.com/ultraware/funlen v0.0.1
56+
github.com/ultraware/whitespace v0.0.2
5657
github.com/valyala/quicktemplate v1.1.1
5758
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect
5859
golang.org/x/sys v0.0.0-20190312061237-fead79001313 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
164164
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
165165
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec h1:AmoEvWAO3nDx1MEcMzPh+GzOOIA5Znpv6++c7bePPY0=
166166
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
167+
github.com/ultraware/whitespace v0.0.2 h1:iL4Un0C3VaMIBGfDogtcdBeSotjfSHYW8OdI1U9Vqas=
168+
github.com/ultraware/whitespace v0.0.2/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
167169
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
168170
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
169171
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=

pkg/golinters/whitespace.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package golinters
2+
3+
import (
4+
"context"
5+
"go/token"
6+
7+
"github.com/golangci/golangci-lint/pkg/lint/linter"
8+
"github.com/golangci/golangci-lint/pkg/result"
9+
10+
"github.com/ultraware/whitespace"
11+
)
12+
13+
type Whitespace struct{}
14+
15+
func (Whitespace) Name() string {
16+
return "whitespace"
17+
}
18+
19+
func (Whitespace) Desc() string {
20+
return "Tool for detection of leading and trailing whitespace"
21+
}
22+
23+
func (w Whitespace) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
24+
var issues []whitespace.Message
25+
for _, file := range lintCtx.ASTCache.GetAllValidFiles() {
26+
issues = append(issues, whitespace.Run(file.F, file.Fset)...)
27+
}
28+
29+
if len(issues) == 0 {
30+
return nil, nil
31+
}
32+
33+
res := make([]result.Issue, len(issues))
34+
for k, i := range issues {
35+
res[k] = result.Issue{
36+
Pos: token.Position{
37+
Filename: i.Pos.Filename,
38+
Line: i.Pos.Line,
39+
},
40+
Text: i.Message,
41+
FromLinter: w.Name(),
42+
}
43+
}
44+
45+
return res, nil
46+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
242242
WithPresets(linter.PresetStyle).
243243
WithSpeed(10).
244244
WithURL("https://github.com/ultraware/funlen"),
245+
linter.NewConfig(golinters.Whitespace{}).
246+
WithPresets(linter.PresetStyle).
247+
WithSpeed(10).
248+
WithURL("https://github.com/ultraware/whitespace"),
245249
}
246250

247251
isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""

test/testdata/whitespace.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//args: -Ewhitespace
2+
package testdata
3+
4+
func UselessStart() { // ERROR "unnecessary leading newline"
5+
6+
a := 1
7+
_ = a
8+
}
9+
10+
func UselessEnd() {
11+
a := 1
12+
_ = a
13+
14+
} // ERROR "unnecessary trailing newline"
15+
16+
func CommentsShouldBeIgnored() {
17+
// test
18+
a := 1
19+
_ = a
20+
}

vendor/github.com/ultraware/whitespace/LICENSE

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

vendor/github.com/ultraware/whitespace/README.md

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/ultraware/whitespace/main.go

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)