Skip to content

Fix lint errors on files with //line directive #1065

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pkg/golinters/dupl.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewDupl() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewGofmt() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewGoimports() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gomodguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewGomodguard() *goanalysis.Linter {
}

for _, file := range pass.Files {
files = append(files, pass.Fset.Position(file.Pos()).Filename)
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}

processor, err := gomodguard.NewProcessor(processorCfg, log.New(os.Stderr, "", 0))
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/ineffassign.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewIneffassign() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/lll.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewLLL() *goanalysis.Linter {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewMisspell() *goanalysis.Linter {

var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.Position(f.Pos())
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewWSL() *goanalysis.Linter {
)

for _, file := range pass.Files {
files = append(files, pass.Fset.Position(file.Pos()).Filename)
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}

wslErrors, _ := wsl.NewProcessorWithConfig(processorCfg).
Expand Down
21 changes: 21 additions & 0 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,27 @@ func TestLineDirectiveProcessedFilesFullLoading(t *testing.T) {
r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputEq(output + "\n")
}

func TestLintFilesWithLineDirective(t *testing.T) {
testshared.NewLintRunner(t).Run("-Edupl", "--disable-all", "--config=testdata/linedirective/dupl.yml", getTestDataDir("linedirective")).
ExpectHasIssue("21-23 lines are duplicate of `testdata/linedirective/hello.go:25-27` (dupl)")
testshared.NewLintRunner(t).Run("-Egofmt", "--disable-all", "--no-config", getTestDataDir("linedirective")).
ExpectHasIssue("File is not `gofmt`-ed with `-s` (gofmt)")
testshared.NewLintRunner(t).Run("-Egoimports", "--disable-all", "--no-config", getTestDataDir("linedirective")).
ExpectHasIssue("File is not `goimports`-ed (goimports)")
testshared.NewLintRunner(t).
Run("-Egomodguard", "--disable-all", "--config=testdata/linedirective/gomodguard.yml", getTestDataDir("linedirective")).
ExpectHasIssue("import of package `github.com/ryancurrah/gomodguard` is blocked because the module is not " +
"in the allowed modules list. (gomodguard)")
testshared.NewLintRunner(t).Run("-Eineffassign", "--disable-all", "--no-config", getTestDataDir("linedirective")).
ExpectHasIssue("ineffectual assignment to `x` (ineffassign)")
testshared.NewLintRunner(t).Run("-Elll", "--disable-all", "--config=testdata/linedirective/lll.yml", getTestDataDir("linedirective")).
ExpectHasIssue("line is 57 characters (lll)")
testshared.NewLintRunner(t).Run("-Emisspell", "--disable-all", "--no-config", getTestDataDir("linedirective")).
ExpectHasIssue("is a misspelling of `language` (misspell)")
testshared.NewLintRunner(t).Run("-Ewsl", "--disable-all", "--no-config", getTestDataDir("linedirective")).
ExpectHasIssue("block should not start with a whitespace (wsl)")
}

func TestSkippedDirsNoMatchArg(t *testing.T) {
dir := getTestDataDir("skipdirs", "skip_me", "nested")
r := testshared.NewLintRunner(t).Run("--print-issued-lines=false", "--no-config", "--skip-dirs", dir, "-Egolint", dir)
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/linedirective/dupl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
dupl:
threshold: 10
5 changes: 5 additions & 0 deletions test/testdata/linedirective/gomodguard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters-settings:
gomodguard:
allowed:
domains:
- golang.org
37 changes: 37 additions & 0 deletions test/testdata/linedirective/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Refers a existent, but non-go file with line directive
//line hello.tmpl:1
package main

import (
"github.com/ryancurrah/gomodguard"
)

func _() {
var x int
_ = x
x = 0 //x
}

func main() {
a()
b()
wsl()
}

func a() {
fmt.Println("foo")
}

func b() {
fmt.Println("foo")
}

func wsl() bool {

return true
}

func notFormatted() {
}

// langauge
1 change: 1 addition & 0 deletions test/testdata/linedirective/hello.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a template file for some code generator, and is not a valid go source file.
3 changes: 3 additions & 0 deletions test/testdata/linedirective/lll.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
lll:
line-length: 50