Skip to content
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

fix: filter files #5272

Merged
merged 3 commits into from
Jan 1, 2025
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
10 changes: 10 additions & 0 deletions pkg/goanalysis/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ func GetFilePosition(pass *analysis.Pass, f *ast.File) token.Position {
return GetFilePositionFor(pass.Fset, f.Pos())
}

func GetGoFilePosition(pass *analysis.Pass, f *ast.File) (token.Position, bool) {
position := GetFilePositionFor(pass.Fset, f.Pos())

if filepath.Ext(position.Filename) == ".go" {
return position, true
}

return position, false
}

func GetFilePositionFor(fset *token.FileSet, p token.Pos) token.Position {
pos := fset.PositionFor(p, true)

Expand Down
5 changes: 4 additions & 1 deletion pkg/golinters/godox/godox.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func New(settings *config.GodoxSettings) *goanalysis.Linter {

func runGodox(pass *analysis.Pass, settings *config.GodoxSettings) {
for _, file := range pass.Files {
position := goanalysis.GetFilePosition(pass, file)
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
continue
}

messages := godox.Run(file, pass.Fset, settings.Keywords...)
if len(messages) == 0 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/golinters/gofmt/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoF
}

for _, file := range pass.Files {
position := goanalysis.GetFilePosition(pass, file)
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
continue
}

diff, err := gofmtAPI.RunRewrite(position.Filename, settings.Simplify, rewriteRules)
if err != nil { // TODO: skip
Expand Down
5 changes: 4 additions & 1 deletion pkg/golinters/gofumpt/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ func New(settings *config.GofumptSettings) *goanalysis.Linter {

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

input, err := os.ReadFile(position.Filename)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/golinters/gofumpt/testdata/fix/in/gofumpt_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt-fix.yml
//golangcitest:expected_exitcode 0
package p

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import "fmt"

func GofmtNotExtra(bar string, baz string) {
fmt.Print(bar, baz)
}
20 changes: 20 additions & 0 deletions pkg/golinters/gofumpt/testdata/fix/out/gofumpt_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//golangcitest:args -Egofumpt
//golangcitest:config_path testdata/gofumpt-fix.yml
//golangcitest:expected_exitcode 0
package p

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import "fmt"

func GofmtNotExtra(bar, baz string) {
fmt.Print(bar, baz)
}
5 changes: 2 additions & 3 deletions pkg/golinters/goheader/goheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error {
a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values))

for _, file := range pass.Files {
position := goanalysis.GetFilePosition(pass, file)

if !strings.HasSuffix(position.Filename, ".go") {
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
continue
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/golinters/goimports/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func New(settings *config.GoImportsSettings) *goanalysis.Linter {

func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) error {
for _, file := range pass.Files {
position := goanalysis.GetFilePosition(pass, file)
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
continue
}

diff, err := goimportsAPI.Run(position.Filename)
if err != nil { // TODO: skip
Expand Down
25 changes: 25 additions & 0 deletions pkg/golinters/goimports/testdata/fix/in/goimports_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//golangcitest:args -Egoimports
//golangcitest:expected_exitcode 0
package p

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

import (
"os"
"fmt"
)

func goimports(a, b int) int {
if a != b {
return 1
}
return 2
}
20 changes: 20 additions & 0 deletions pkg/golinters/goimports/testdata/fix/out/goimports_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//golangcitest:args -Egoimports
//golangcitest:expected_exitcode 0
package p

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
printf("%d\n", s);
}
*/
import "C"

func goimports(a, b int) int {
if a != b {
return 1
}
return 2
}
6 changes: 5 additions & 1 deletion pkg/golinters/lll/lll.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ func runLll(pass *analysis.Pass, settings *config.LllSettings) error {
}

func getLLLIssuesForFile(pass *analysis.Pass, file *ast.File, maxLineLen int, tabSpaces string) error {
position := goanalysis.GetFilePosition(pass, file)
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
return nil
}

nonAdjPosition := pass.Fset.PositionFor(file.Pos(), false)

f, err := os.Open(position.Filename)
Expand Down
9 changes: 6 additions & 3 deletions pkg/golinters/misspell/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
}

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

fileContent, err := lintCtx.FileCache.GetFileBytes(filename)
fileContent, err := lintCtx.FileCache.GetFileBytes(position.Filename)
if err != nil {
return fmt.Errorf("can't get file %s contents: %w", filename, err)
return fmt.Errorf("can't get file %s contents: %w", position.Filename, err)
}

// `r.ReplaceGo` doesn't find issues inside strings: it searches only inside comments.
Expand Down
5 changes: 4 additions & 1 deletion pkg/golinters/nestif/nestif.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func runNestIf(pass *analysis.Pass, settings *config.NestifSettings) {
}

for _, file := range pass.Files {
position := goanalysis.GetFilePosition(pass, file)
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
continue
}

issues := checker.Check(file, pass.Fset)
if len(issues) == 0 {
Expand Down
Loading