Skip to content

Commit

Permalink
fix: filter Go filenames (#5291)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jan 3, 2025
1 parent c58ddd3 commit c114969
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 33 deletions.
4 changes: 0 additions & 4 deletions pkg/goanalysis/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
"golang.org/x/tools/go/analysis"
)

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())

Expand Down
15 changes: 1 addition & 14 deletions pkg/golinters/dupl/dupl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dupl
import (
"fmt"
"go/token"
"strings"
"sync"

duplAPI "github.com/golangci/dupl"
Expand Down Expand Up @@ -55,19 +54,7 @@ func New(settings *config.DuplSettings) *goanalysis.Linter {
}

func runDupl(pass *analysis.Pass, settings *config.DuplSettings) ([]goanalysis.Issue, error) {
fileNames := internal.GetFileNames(pass)

var onlyGofiles []string
for _, name := range fileNames {
// Related to Windows
if !strings.HasSuffix(name, ".go") {
continue
}

onlyGofiles = append(onlyGofiles, name)
}

issues, err := duplAPI.Run(onlyGofiles, settings.Threshold)
issues, err := duplAPI.Run(internal.GetGoFileNames(pass), settings.Threshold)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gomodguard/gomodguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func New(settings *config.GoModGuardSettings) *goanalysis.Linter {
}

analyzer.Run = func(pass *analysis.Pass) (any, error) {
gomodguardIssues := processor.ProcessFiles(internal.GetFileNames(pass))
gomodguardIssues := processor.ProcessFiles(internal.GetGoFileNames(pass))

mu.Lock()
defer mu.Unlock()
Expand Down
11 changes: 9 additions & 2 deletions pkg/golinters/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ func FormatCode(code string, _ *config.Config) string {
return fmt.Sprintf("`%s`", code)
}

func GetFileNames(pass *analysis.Pass) []string {
func GetGoFileNames(pass *analysis.Pass) []string {
var filenames []string

for _, f := range pass.Files {
filenames = append(filenames, goanalysis.GetFilePosition(pass, f).Filename)
position, b := goanalysis.GetGoFilePosition(pass, f)
if !b {
continue
}

filenames = append(filenames, position.Filename)
}

return filenames
}
23 changes: 11 additions & 12 deletions pkg/golinters/revive/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func newWrapper(settings *config.ReviveSettings) (*wrapper, error) {
}

func (w *wrapper) run(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
packages := [][]string{internal.GetFileNames(pass)}
packages := [][]string{internal.GetGoFileNames(pass)}

failures, err := w.revive.Lint(packages, w.lintingRules, *w.conf)
if err != nil {
Expand Down Expand Up @@ -184,17 +184,16 @@ func toIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
if object.ReplacementLine != "" {
f := pass.Fset.File(token.Pos(object.Position.Start.Offset))

start := f.LineStart(object.Position.Start.Line)

end := goanalysis.EndOfLinePos(f, object.Position.End.Line)

issue.SuggestedFixes = []analysis.SuggestedFix{{
TextEdits: []analysis.TextEdit{{
Pos: start,
End: end,
NewText: []byte(object.ReplacementLine),
}},
}}
// Skip cgo files because the positions are wrong.
if object.GetFilename() == f.Name() {
issue.SuggestedFixes = []analysis.SuggestedFix{{
TextEdits: []analysis.TextEdit{{
Pos: f.LineStart(object.Position.Start.Line),
End: goanalysis.EndOfLinePos(f, object.Position.End.Line),
NewText: []byte(object.ReplacementLine),
}},
}}
}
}

return goanalysis.NewIssue(issue, pass)
Expand Down
8 changes: 8 additions & 0 deletions pkg/golinters/revive/revive_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ import (
func TestFromTestdata(t *testing.T) {
integration.RunTestdata(t)
}

func TestFix(t *testing.T) {
integration.RunFix(t)
}

func TestFixPathPrefix(t *testing.T) {
integration.RunFixPathPrefix(t)
}
14 changes: 14 additions & 0 deletions pkg/golinters/revive/testdata/fix/in/revive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//golangcitest:args -Erevive
//golangcitest:config_path testdata/revive-fix.yml
//golangcitest:expected_exitcode 0
package in

import (
"errors"
"fmt"
"math"
)

func _() error {
return errors.New(fmt.Sprintf("foo: %d", math.MaxInt))
}
14 changes: 14 additions & 0 deletions pkg/golinters/revive/testdata/fix/out/revive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//golangcitest:args -Erevive
//golangcitest:config_path testdata/revive-fix.yml
//golangcitest:expected_exitcode 0
package in

import (
"errors"
"fmt"
"math"
)

func _() error {
return fmt.Errorf("foo: %d", math.MaxInt)
}
7 changes: 7 additions & 0 deletions pkg/golinters/revive/testdata/revive-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters-settings:
revive:
ignore-generated-header: true
severity: warning
rules:
- name: errorf
- name: range

0 comments on commit c114969

Please sign in to comment.