-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
17 changed files
with
1,685 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.