Skip to content

Commit afa0e27

Browse files
authored
fix: filter files (#5272)
1 parent 62d7ebf commit afa0e27

File tree

13 files changed

+128
-12
lines changed

13 files changed

+128
-12
lines changed

pkg/goanalysis/position.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ func GetFilePosition(pass *analysis.Pass, f *ast.File) token.Position {
1212
return GetFilePositionFor(pass.Fset, f.Pos())
1313
}
1414

15+
func GetGoFilePosition(pass *analysis.Pass, f *ast.File) (token.Position, bool) {
16+
position := GetFilePositionFor(pass.Fset, f.Pos())
17+
18+
if filepath.Ext(position.Filename) == ".go" {
19+
return position, true
20+
}
21+
22+
return position, false
23+
}
24+
1525
func GetFilePositionFor(fset *token.FileSet, p token.Pos) token.Position {
1626
pos := fset.PositionFor(p, true)
1727

pkg/golinters/godox/godox.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ func New(settings *config.GodoxSettings) *goanalysis.Linter {
3434

3535
func runGodox(pass *analysis.Pass, settings *config.GodoxSettings) {
3636
for _, file := range pass.Files {
37-
position := goanalysis.GetFilePosition(pass, file)
37+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
38+
if !isGoFile {
39+
continue
40+
}
3841

3942
messages := godox.Run(file, pass.Fset, settings.Keywords...)
4043
if len(messages) == 0 {

pkg/golinters/gofmt/gofmt.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoF
4545
}
4646

4747
for _, file := range pass.Files {
48-
position := goanalysis.GetFilePosition(pass, file)
48+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
49+
if !isGoFile {
50+
continue
51+
}
4952

5053
diff, err := gofmtAPI.RunRewrite(position.Filename, settings.Simplify, rewriteRules)
5154
if err != nil { // TODO: skip

pkg/golinters/gofumpt/gofumpt.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ func New(settings *config.GofumptSettings) *goanalysis.Linter {
6161

6262
func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, options format.Options) error {
6363
for _, file := range pass.Files {
64-
position := goanalysis.GetFilePosition(pass, file)
64+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
65+
if !isGoFile {
66+
continue
67+
}
6568

6669
input, err := os.ReadFile(position.Filename)
6770
if err != nil {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//golangcitest:args -Egofumpt
2+
//golangcitest:config_path testdata/gofumpt-fix.yml
3+
//golangcitest:expected_exitcode 0
4+
package p
5+
6+
/*
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
void myprint(char* s) {
11+
printf("%d\n", s);
12+
}
13+
*/
14+
import "C"
15+
16+
import "fmt"
17+
18+
func GofmtNotExtra(bar string, baz string) {
19+
fmt.Print(bar, baz)
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//golangcitest:args -Egofumpt
2+
//golangcitest:config_path testdata/gofumpt-fix.yml
3+
//golangcitest:expected_exitcode 0
4+
package p
5+
6+
/*
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
void myprint(char* s) {
11+
printf("%d\n", s);
12+
}
13+
*/
14+
import "C"
15+
16+
import "fmt"
17+
18+
func GofmtNotExtra(bar, baz string) {
19+
fmt.Print(bar, baz)
20+
}

pkg/golinters/goheader/goheader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error {
6363
a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))
6464

6565
for _, file := range pass.Files {
66-
position := goanalysis.GetFilePosition(pass, file)
67-
68-
if !strings.HasSuffix(position.Filename, ".go") {
66+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
67+
if !isGoFile {
6968
continue
7069
}
7170

pkg/golinters/goimports/goimports.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ func New(settings *config.GoImportsSettings) *goanalysis.Linter {
4343

4444
func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) error {
4545
for _, file := range pass.Files {
46-
position := goanalysis.GetFilePosition(pass, file)
46+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
47+
if !isGoFile {
48+
continue
49+
}
4750

4851
diff, err := goimportsAPI.Run(position.Filename)
4952
if err != nil { // TODO: skip
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//golangcitest:args -Egoimports
2+
//golangcitest:expected_exitcode 0
3+
package p
4+
5+
/*
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
void myprint(char* s) {
10+
printf("%d\n", s);
11+
}
12+
*/
13+
import "C"
14+
15+
import (
16+
"os"
17+
"fmt"
18+
)
19+
20+
func goimports(a, b int) int {
21+
if a != b {
22+
return 1
23+
}
24+
return 2
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//golangcitest:args -Egoimports
2+
//golangcitest:expected_exitcode 0
3+
package p
4+
5+
/*
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
void myprint(char* s) {
10+
printf("%d\n", s);
11+
}
12+
*/
13+
import "C"
14+
15+
func goimports(a, b int) int {
16+
if a != b {
17+
return 1
18+
}
19+
return 2
20+
}

pkg/golinters/lll/lll.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ func runLll(pass *analysis.Pass, settings *config.LllSettings) error {
5555
}
5656

5757
func getLLLIssuesForFile(pass *analysis.Pass, file *ast.File, maxLineLen int, tabSpaces string) error {
58-
position := goanalysis.GetFilePosition(pass, file)
58+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
59+
if !isGoFile {
60+
return nil
61+
}
62+
5963
nonAdjPosition := pass.Fset.PositionFor(file.Pos(), false)
6064

6165
f, err := os.Open(position.Filename)

pkg/golinters/misspell/misspell.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
9191
}
9292

9393
func runMisspellOnFile(lintCtx *linter.Context, pass *analysis.Pass, file *ast.File, replacer *misspell.Replacer, mode string) error {
94-
filename := goanalysis.GetFilePosition(pass, file).Filename
94+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
95+
if !isGoFile {
96+
return nil
97+
}
9598

96-
fileContent, err := lintCtx.FileCache.GetFileBytes(filename)
99+
fileContent, err := lintCtx.FileCache.GetFileBytes(position.Filename)
97100
if err != nil {
98-
return fmt.Errorf("can't get file %s contents: %w", filename, err)
101+
return fmt.Errorf("can't get file %s contents: %w", position.Filename, err)
99102
}
100103

101104
// `r.ReplaceGo` doesn't find issues inside strings: it searches only inside comments.

pkg/golinters/nestif/nestif.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ func runNestIf(pass *analysis.Pass, settings *config.NestifSettings) {
3535
}
3636

3737
for _, file := range pass.Files {
38-
position := goanalysis.GetFilePosition(pass, file)
38+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
39+
if !isGoFile {
40+
continue
41+
}
3942

4043
issues := checker.Check(file, pass.Fset)
4144
if len(issues) == 0 {

0 commit comments

Comments
 (0)