Skip to content

Add whitespace linter #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ scopelint: Scopelint checks for unpinned variables in go programs [fast: true, a
stylecheck: Stylecheck is a replacement for golint [fast: false, auto-fix: false]
unconvert: Remove unnecessary type conversions [fast: true, auto-fix: false]
unparam: Reports unused function parameters [fast: false, auto-fix: false]
whitespace: Tool for detection of leading and trailing whitespace [fast: true, auto-fix: false]
```

Pass `-E/--enable` to enable linter and `-D/--disable` to disable:
Expand Down Expand Up @@ -459,6 +460,7 @@ golangci-lint help linters
- [gochecknoinits](https://github.com/leighmcculloch/gochecknoinits) - Checks that no init functions are present in Go code
- [gochecknoglobals](https://github.com/leighmcculloch/gochecknoglobals) - Checks that no globals are present in Go code
- [funlen](https://github.com/ultraware/funlen) - Tool for detection of long functions
- [whitespace](https://github.com/ultraware/whitespace) - Tool for detection of leading and trailing whitespace

## Configuration

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
github.com/stretchr/testify v1.2.2
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec
github.com/ultraware/funlen v0.0.1
github.com/ultraware/whitespace v0.0.2
github.com/valyala/quicktemplate v1.1.1
golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a // indirect
golang.org/x/sys v0.0.0-20190312061237-fead79001313 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec h1:AmoEvWAO3nDx1MEcMzPh+GzOOIA5Znpv6++c7bePPY0=
github.com/timakin/bodyclose v0.0.0-20190721030226-87058b9bfcec/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
github.com/ultraware/whitespace v0.0.2 h1:iL4Un0C3VaMIBGfDogtcdBeSotjfSHYW8OdI1U9Vqas=
github.com/ultraware/whitespace v0.0.2/go.mod h1:aVMh/gQve5Maj9hQ/hg+F75lr/X5A89uZnzAmWSineA=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
Expand Down
46 changes: 46 additions & 0 deletions pkg/golinters/whitespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package golinters

import (
"context"
"go/token"

"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"

"github.com/ultraware/whitespace"
)

type Whitespace struct{}

func (Whitespace) Name() string {
return "whitespace"
}

func (Whitespace) Desc() string {
return "Tool for detection of leading and trailing whitespace"
}

func (w Whitespace) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
var issues []whitespace.Message
for _, file := range lintCtx.ASTCache.GetAllValidFiles() {
issues = append(issues, whitespace.Run(file.F, file.Fset)...)
}

if len(issues) == 0 {
return nil, nil
}

res := make([]result.Issue, len(issues))
for k, i := range issues {
res[k] = result.Issue{
Pos: token.Position{
Filename: i.Pos.Filename,
Line: i.Pos.Line,
},
Text: i.Message,
FromLinter: w.Name(),
}
}

return res, nil
}
4 changes: 4 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithSpeed(10).
WithURL("https://github.com/ultraware/funlen"),
linter.NewConfig(golinters.Whitespace{}).
WithPresets(linter.PresetStyle).
WithSpeed(10).
WithURL("https://github.com/ultraware/whitespace"),
}

isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""
Expand Down
20 changes: 20 additions & 0 deletions test/testdata/whitespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//args: -Ewhitespace
package testdata

func UselessStart() { // ERROR "unnecessary leading newline"

a := 1
_ = a
}

func UselessEnd() {
a := 1
_ = a

} // ERROR "unnecessary trailing newline"

func CommentsShouldBeIgnored() {
// test
a := 1
_ = a
}
7 changes: 7 additions & 0 deletions vendor/github.com/ultraware/whitespace/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions vendor/github.com/ultraware/whitespace/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions vendor/github.com/ultraware/whitespace/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.