Add whitespace linter (#673)
This commit is contained in:
parent
31afdf8b04
commit
c7dee2c0b2
@ -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:
|
||||
@ -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
|
||||
|
||||
|
1
go.mod
1
go.mod
@ -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
|
||||
|
2
go.sum
2
go.sum
@ -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=
|
||||
|
46
pkg/golinters/whitespace.go
Normal file
46
pkg/golinters/whitespace.go
Normal file
@ -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
|
||||
}
|
@ -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") == ""
|
||||
|
20
test/testdata/whitespace.go
vendored
Normal file
20
test/testdata/whitespace.go
vendored
Normal file
@ -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
vendor/github.com/ultraware/whitespace/LICENSE
generated
vendored
Normal file
7
vendor/github.com/ultraware/whitespace/LICENSE
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
Copyright 2018 Ultraware Consultancy and Development B.V.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
53
vendor/github.com/ultraware/whitespace/README.md
generated
vendored
Normal file
53
vendor/github.com/ultraware/whitespace/README.md
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
# Whitespace linter
|
||||
|
||||
Whitespace is a linter that checks for unnecessary newlines at the start and end of functions, if, for, etc.
|
||||
|
||||
Example code:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
fmt.Println("Hello world")
|
||||
}
|
||||
```
|
||||
|
||||
Reults in:
|
||||
|
||||
```
|
||||
$ whitespace .
|
||||
main.go:6:unnecessary newline
|
||||
```
|
||||
|
||||
## Installation guide
|
||||
|
||||
```bash
|
||||
go get git.ultraware.nl/NiseVoid/whitespace
|
||||
```
|
||||
|
||||
### Gometalinter
|
||||
|
||||
You can add whitespace to gometalinter and enable it.
|
||||
|
||||
`.gometalinter.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"Linters": {
|
||||
"whitespace": "whitespace:PATH:LINE:MESSAGE"
|
||||
},
|
||||
|
||||
"Enable": [
|
||||
"whitespace"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
commandline:
|
||||
|
||||
```bash
|
||||
gometalinter --linter "whitespace:whitespace:PATH:LINE:MESSAGE" --enable "whitespace"
|
||||
```
|
112
vendor/github.com/ultraware/whitespace/main.go
generated
vendored
Normal file
112
vendor/github.com/ultraware/whitespace/main.go
generated
vendored
Normal file
@ -0,0 +1,112 @@
|
||||
package whitespace
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/token"
|
||||
)
|
||||
|
||||
// Message contains a message
|
||||
type Message struct {
|
||||
Pos token.Position
|
||||
Message string
|
||||
}
|
||||
|
||||
// Run runs this linter on the provided code
|
||||
func Run(file *ast.File, fset *token.FileSet) []Message {
|
||||
var messages []Message
|
||||
|
||||
for _, f := range file.Decls {
|
||||
decl, ok := f.(*ast.FuncDecl)
|
||||
if !ok || decl.Body == nil { // decl.Body can be nil for e.g. cgo
|
||||
continue
|
||||
}
|
||||
|
||||
vis := visitor{file.Comments, fset, nil}
|
||||
ast.Walk(&vis, decl)
|
||||
|
||||
messages = append(messages, vis.messages...)
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
type visitor struct {
|
||||
comments []*ast.CommentGroup
|
||||
fset *token.FileSet
|
||||
messages []Message
|
||||
}
|
||||
|
||||
func (v *visitor) Visit(node ast.Node) ast.Visitor {
|
||||
if node == nil {
|
||||
return v
|
||||
}
|
||||
|
||||
if stmt, ok := node.(*ast.BlockStmt); ok {
|
||||
first, last := firstAndLast(v.comments, v.fset, stmt.Pos(), stmt.End(), stmt.List)
|
||||
|
||||
if msg := checkStart(v.fset, stmt.Lbrace, first); msg != nil {
|
||||
v.messages = append(v.messages, *msg)
|
||||
}
|
||||
if msg := checkEnd(v.fset, stmt.Rbrace, last); msg != nil {
|
||||
v.messages = append(v.messages, *msg)
|
||||
}
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func posLine(fset *token.FileSet, pos token.Pos) int {
|
||||
return fset.Position(pos).Line
|
||||
}
|
||||
|
||||
func firstAndLast(comments []*ast.CommentGroup, fset *token.FileSet, start, end token.Pos, stmts []ast.Stmt) (ast.Node, ast.Node) {
|
||||
if len(stmts) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
first, last := ast.Node(stmts[0]), ast.Node(stmts[len(stmts)-1])
|
||||
|
||||
for _, c := range comments {
|
||||
if posLine(fset, c.Pos()) == posLine(fset, start) || posLine(fset, c.End()) == posLine(fset, end) {
|
||||
continue
|
||||
}
|
||||
|
||||
if c.Pos() < start || c.End() > end {
|
||||
continue
|
||||
}
|
||||
if c.Pos() < first.Pos() {
|
||||
first = c
|
||||
}
|
||||
if c.End() > last.End() {
|
||||
last = c
|
||||
}
|
||||
}
|
||||
|
||||
return first, last
|
||||
}
|
||||
|
||||
func checkStart(fset *token.FileSet, start token.Pos, first ast.Node) *Message {
|
||||
if first == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if posLine(fset, start)+1 < posLine(fset, first.Pos()) {
|
||||
pos := fset.Position(start)
|
||||
return &Message{pos, `unnecessary leading newline`}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkEnd(fset *token.FileSet, end token.Pos, last ast.Node) *Message {
|
||||
if last == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if posLine(fset, end)-1 > posLine(fset, last.End()) {
|
||||
pos := fset.Position(end)
|
||||
return &Message{pos, `unnecessary trailing newline`}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user