Soichiro Kashima 7f48cc88b8
Fix lint errors on files with //line directive (#1065)
If the target files contains `//line` directive and it indicates
a non-go file, the linter is going to handle it as a go file,
which results in failure.
The cause of this issue is that the linters (`Analyzer`s) are using
`pass.Fset.Position()`. This func returns the adjusted position using
`//line` directive.
The example project reported in #998 has `//line` directive that
indicates other non-go file.
According to the description of "Compiler Directives”
(https://golang.org/cmd/compile/#hdr-Compiler_Directives),
line directives is mainly used for reporting original positions to
the generators or something.
On linters of golangci-lint, `pass.Fset.Position()` is used just to
aggregate file names; we don't have to adjust positions.
This changes `Analyzer`s that use `pass.Fset.Position()` to aggregate
file names to use `pass.Fset.PositionFor()` with `adjusted == false`.

Relates: #998
2020-05-05 18:49:34 +03:00

82 lines
2.3 KiB
Go

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 = []string{}
linterCfg = lintCtx.Cfg.LintersSettings.WSL
processorCfg = wsl.Configuration{
StrictAppend: linterCfg.StrictAppend,
AllowAssignAndCallCuddle: linterCfg.AllowAssignAndCallCuddle,
AllowMultiLineAssignCuddle: linterCfg.AllowMultiLineAssignCuddle,
AllowCuddleDeclaration: linterCfg.AllowCuddleDeclaration,
AllowTrailingComment: linterCfg.AllowTrailingComment,
AllowSeparatedLeadingComment: linterCfg.AllowSeparatedLeadingComment,
ForceCuddleErrCheckAndAssign: linterCfg.ForceCuddleErrCheckAndAssign,
ForceCaseTrailingWhitespaceLimit: linterCfg.ForceCaseTrailingWhitespaceLimit,
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)
}