Skip to content

Commit

Permalink
Add WSL linter (#771)
Browse files Browse the repository at this point in the history
* Add WSL linter

* Use v1.0.0 tag for wsl

* Don't add specific test file skip, use mutex to add errors

* Fix goimports error

* Add more tests for WSL, bump WSL version

* Fix bad go.sum (go mod tidy)
  • Loading branch information
bombsimon authored and tpounds committed Oct 4, 2019
1 parent 30864f8 commit 3e09174
Show file tree
Hide file tree
Showing 17 changed files with 1,685 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ stylecheck: Stylecheck is a replacement for golint [fast: true, auto-fix: false]
unconvert: Remove unnecessary type conversions [fast: true, auto-fix: false]
unparam: Reports unused function parameters [fast: true, auto-fix: false]
whitespace: Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true]
wsl: Whitespace Linter - Forces you to use empty lines! [fast: true, auto-fix: false]
```
Pass `-E/--enable` to enable linter and `-D/--disable` to disable:
Expand Down Expand Up @@ -469,6 +470,7 @@ golangci-lint help linters
- [godox](https://github.com/matoous/godox) - Tool for detection of FIXME, TODO and other comment keywords
- [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
- [wsl](https://github.com/bombsimon/wsl) - Whitespace Linter - Forces you to use empty lines!
## Configuration
Expand Down Expand Up @@ -1123,6 +1125,7 @@ Thanks to developers and authors of used linters:
- [leighmcculloch](https://github.com/leighmcculloch)
- [matoous](https://github.com/matoous)
- [ultraware](https://github.com/ultraware)
- [bombsimon](https://github.com/bombsimon)
## Changelog
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/OpenPeeDeeP/depguard v1.0.1
github.com/bombsimon/wsl v1.0.1
github.com/fatih/color v1.7.0
github.com/go-critic/go-critic v0.3.5-0.20190904082202-d79a9f0c64db
github.com/go-lintpack/lintpack v0.5.2
Expand Down Expand Up @@ -40,7 +41,7 @@ require (
github.com/uudashr/gocognit v0.0.0-20190926065955-1655d0de0517
github.com/valyala/quicktemplate v1.2.0
golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678
gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v2 v2.2.4
honnef.co/go/tools v0.0.1-2019.2.3
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bombsimon/wsl v1.0.1 h1:GkrpOj7ajds8re6EJXN+k6UtatYSktupigQ2ZfOUIOU=
github.com/bombsimon/wsl v1.0.1/go.mod h1:DSRwCD8c7NecM11/LnZcTS8nS8OND5ybj04DWM4l8mc=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down Expand Up @@ -308,6 +310,8 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
Expand Down
63 changes: 63 additions & 0 deletions pkg/golinters/wsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package golinters

import (
"sync"

"github.com/bombsimon/wsl"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

const (
name = "wsl"
)

// NewWSL returns a new WSL linter.
func NewWSL() *goanalysis.Linter {
var (
issues []result.Issue
mu = sync.Mutex{}
analyzer = &analysis.Analyzer{
Name: goanalysis.TheOnlyAnalyzerName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
)

return goanalysis.NewLinter(
name,
"Whitespace Linter - Forces you to use empty lines!",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
files := []string{}

for _, file := range pass.Files {
files = append(files, pass.Fset.Position(file.Pos()).Filename)
}

wslErrors, _ := wsl.ProcessFiles(files)
if len(wslErrors) == 0 {
return nil, nil
}

mu.Lock()
defer mu.Unlock()

for _, err := range wslErrors {
issues = append(issues, result.Issue{
FromLinter: name,
Pos: err.Position,
Text: err.Reason,
})
}

return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []result.Issue {
return issues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithAutoFix().
WithURL("https://github.com/ultraware/whitespace"),
linter.NewConfig(golinters.NewWSL()).
WithPresets(linter.PresetStyle).
WithURL("https://github.com/bombsimon/wsl"),
}

isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""
Expand Down
80 changes: 80 additions & 0 deletions test/testdata/wsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//args: -Ewsl
//config: linters-settings.wsl.tests=1
package testdata

import (
"context"
"fmt"
)

func main() {
var (
y = 0
)
if y < 1 { // ERROR "if statements should only be cuddled with assignments"
fmt.Println("tight")
}

thisIsNotUsedInIf := true
if 2 > 1 { // ERROR "if statements should only be cuddled with assignments used in the if statement itself"
return
}

one := 1
two := 2
three := 3
if three == 3 { // ERROR "only one cuddle assignment allowed before if statement"
fmt.Println("too many cuddled assignments", one, two, thisIsNotUsedInIf)
}

var a = "a"
var b = "b" // ERROR "declarations should never be cuddled"

if true {
return
}
if false { // ERROR "if statements should only be cuddled with assignments"
return
}

for i := range make([]int, 10) {
fmt.Println(i)
fmt.Println(i + i)
continue // ERROR "branch statements should not be cuddled if block has more than two lines"
}

assignOne := a
fmt.Println(assignOne)
assignTwo := b // ERROR "assignments should only be cuddled with other assignments"
fmt.Println(assignTwo)

_, cf1 := context.WithCancel(context.Background())
_, cf2 := context.WithCancel(context.Background())
defer cf1() // ERROR "only one cuddle assignment allowed before defer statement"
defer cf2()
}

func f1() int {
x := 1
return x
}

func f2() int {
x := 1
y := 3
return x + y // ERROR "return statements should not be cuddled if block has more than two lines"
}

func f3() int {
sum := 0
for _, v := range []int{2, 4, 8} {
sum += v
}

notSum := 0
for _, v := range []int{1, 2, 4} { // ERROR "ranges should only be cuddled with assignments used in the iteration"
sum += v
}

return sum + notSum
}
70 changes: 70 additions & 0 deletions vendor/github.com/bombsimon/wsl/.gitignore

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

25 changes: 25 additions & 0 deletions vendor/github.com/bombsimon/wsl/.travis.yml

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

21 changes: 21 additions & 0 deletions vendor/github.com/bombsimon/wsl/LICENSE

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

Loading

0 comments on commit 3e09174

Please sign in to comment.