-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathwsl.go
83 lines (71 loc) · 2.51 KB
/
wsl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package golinters
import (
"sync"
"github.com/bombsimon/wsl/v3"
"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 []goanalysis.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) {
var (
files = make([]string, 0, len(pass.Files))
linterCfg = lintCtx.Cfg.LintersSettings.WSL
processorCfg = wsl.Configuration{
StrictAppend: linterCfg.StrictAppend,
AllowAssignAndCallCuddle: linterCfg.AllowAssignAndCallCuddle,
AllowAssignAndAnythingCuddle: linterCfg.AllowAssignAndAnythingCuddle,
AllowMultiLineAssignCuddle: linterCfg.AllowMultiLineAssignCuddle,
AllowCuddleDeclaration: linterCfg.AllowCuddleDeclaration,
AllowTrailingComment: linterCfg.AllowTrailingComment,
AllowSeparatedLeadingComment: linterCfg.AllowSeparatedLeadingComment,
ForceCuddleErrCheckAndAssign: linterCfg.ForceCuddleErrCheckAndAssign,
ForceCaseTrailingWhitespaceLimit: linterCfg.ForceCaseTrailingWhitespaceLimit,
ForceExclusiveShortDeclarations: linterCfg.ForceExclusiveShortDeclarations,
AllowCuddleWithCalls: []string{"Lock", "RLock"},
AllowCuddleWithRHS: []string{"Unlock", "RUnlock"},
ErrorVariableNames: []string{"err"},
}
)
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}
wslErrors, _ := wsl.NewProcessorWithConfig(processorCfg).
ProcessFiles(files)
if len(wslErrors) == 0 {
return nil, nil
}
mu.Lock()
defer mu.Unlock()
for _, err := range wslErrors {
issues = append(issues, goanalysis.NewIssue(&result.Issue{
FromLinter: name,
Pos: err.Position,
Text: err.Reason,
}, pass))
}
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return issues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}