Skip to content
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

Add WSL linter #771

Merged
merged 6 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ linters-settings:
whitespace:
multi-if: false

wsl:
# Skip test files
no-test: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. please, don't use negative naming
  2. please, don't add such duplicating option: any linter can be disabled for tests by exclude-rules. It's not performance effective, I agree, but it should be done in a general way for all linters, not per-linter

Copy link
Member Author

@bombsimon bombsimon Oct 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, I'll remove this since the configuration isn't needed!


linters:
enable:
- megacheck
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,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 @@ -466,6 +467,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 @@ -795,6 +797,10 @@ linters-settings:
whitespace:
multi-if: false

wsl:
# Skip test files
no-test: false

linters:
enable:
- megacheck
Expand Down Expand Up @@ -1117,6 +1123,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.0
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 @@ -39,7 +40,7 @@ require (
github.com/ultraware/whitespace v0.0.3
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.3
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.0 h1:6+XZ2EoYhP3Hwja8u4fgDYA1UdszkbUXJ+TBEG8Z53E=
github.com/bombsimon/wsl v1.0.0/go.mod h1:sOSQ78XMiq4wUAAhso7VhDmnSQu9nPHIntSxgAzTy30=
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 @@ -306,6 +308,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.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
gopkg.in/yaml.v2 v2.2.3/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
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ type LintersSettings struct {
Whitespace struct {
MultiIf bool `mapstructure:"multi-if"`
}
WSL struct {
NoTest bool `mapstructure:"no-test"`
}

Lll LllSettings
Unparam UnparamSettings
Expand Down
67 changes: 67 additions & 0 deletions pkg/golinters/wsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package golinters

import (
"strings"

"github.com/bombsimon/wsl"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
"golang.org/x/tools/go/analysis"
)

const (
name = "wsl"
)

// NewWSL returns a new WSL linter.
func NewWSL() *goanalysis.Linter {
var (
issues []result.Issue
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) {
var (
errCfg = lintCtx.Settings().WSL
fileNames []string
)

for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())

if errCfg.NoTest && strings.HasSuffix(pos.Filename, "_test.go") {
continue
}

fileNames = append(fileNames, pos.Filename)
}

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

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 @@ -201,6 +201,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
12 changes: 12 additions & 0 deletions test/testdata/wsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//args: -Ewsl
//config: linters-settings.wsl.tests=1
package testdata

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

}
}
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.

24 changes: 24 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