Skip to content

Commit

Permalink
Fix some false positives, add configuration, fix linter errors
Browse files Browse the repository at this point in the history
* Functions on one line should not be considered
* Fix better token.Pos() for whitespaces
* Fix variable names
* Add stringer
* Support configuration (not yet exposed properly)
* Support to whitelist any RHS
* Support to whitelist any calls one line above
* Support to cuddle any assignment and call if any of the RHS/LHS is
used
  • Loading branch information
bombsimon committed Oct 6, 2019
1 parent 32d6ac1 commit 9dc3d97
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 142 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ good, making it harder for other people to read and understand. The linter will
warn about newlines in and around blocks, in the beginning of files and other
places in the code.

**I know this linter is aggressive** and a lot of projects I've tested it on
have failed miserably. For this linter to be useful at all I want to be open to
new ideas, configurations and discussions! Also note that some of the warnings
might be bugs or unintentional false positives so I would love an
[issue](https://github.com/bombsimon/wsl/issues/new) to fix, discuss, change or
make something configurable!

## Usage

Install by using `go get -u github.com/bombsimon/wsl/cmd/...`.
Expand All @@ -26,6 +33,9 @@ By default, the linter will run on `./...` which means all go files in the
current path and all subsequent paths, including test files. To disable linting
test files, use `-n` or `--no-test`.

This linter can also be used as a part of
[golangci-lint](https://github.com/golangci/golangci-lint)

## Rules

Note that this linter doesn't take in consideration the issues that will be
Expand Down Expand Up @@ -335,6 +345,11 @@ fmt.Println(a)

foo := true
someFunc(false)

if someBool() {
fmt.Println("doing things")
}
x.Calling()
```

**Do**
Expand All @@ -350,6 +365,12 @@ someFunc(foo)
bar := false

someFunc(true)

if someBool() {
fmt.Println("doing things")
}

x.Calling()
```

#### Ranges
Expand Down
42 changes: 20 additions & 22 deletions cmd/wsl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@ import (

func main() {
var (
args []string
help bool
notest bool
warnings bool
cwd, _ = os.Getwd()
files = []string{}
finalFiles = []string{}
args []string
help bool
noTest bool
showWarnings bool
cwd, _ = os.Getwd()
files = []string{}
finalFiles = []string{}
)

flag.BoolVar(&help, "h", false, "Show this help text")
flag.BoolVar(&help, "help", false, "")
flag.BoolVar(&notest, "n", false, "Don't lint test files")
flag.BoolVar(&notest, "no-test", false, "")
flag.BoolVar(&warnings, "w", false, "Show warnings (ignored rules)")
flag.BoolVar(&warnings, "warnings", false, "")
flag.BoolVar(&noTest, "n", false, "Don't lint test files")
flag.BoolVar(&noTest, "no-test", false, "")
flag.BoolVar(&showWarnings, "w", false, "Show warnings (ignored rules)")
flag.BoolVar(&showWarnings, "warnings", false, "")

flag.Parse()

if help {
showHelp()

return
}

Expand All @@ -57,7 +56,7 @@ func main() {

// Use relative path to print shorter names, sort out test files if chosen.
for _, f := range files {
if notest {
if noTest {
if strings.HasSuffix(f, "_test.go") {
continue
}
Expand All @@ -72,26 +71,25 @@ func main() {
finalFiles = append(finalFiles, f)
}

r, w := wsl.ProcessFiles(finalFiles)
processor := wsl.NewProcessor()
result, warnings := processor.ProcessFiles(finalFiles)

for _, x := range r {
fmt.Printf("%s:%d: %s\n", x.FileName, x.LineNumber, x.Reason)
for _, r := range result {
fmt.Println(r.String())
}

if warnings && len(w) > 0 {
if showWarnings && len(warnings) > 0 {
fmt.Println()
fmt.Println("⚠️ Warnings found")

for _, x := range w {
fmt.Println(x)
for _, w := range warnings {
fmt.Println(w)
}
}

if len(r) > 0 {
if len(result) > 0 {
os.Exit(2)
}

return
}

func expandGoWildcard(root string) []string {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/bombsimon/wsl

require (
github.com/davecgh/go-spew v1.1.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.4.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit 9dc3d97

Please sign in to comment.