Skip to content

Commit 8abbb1b

Browse files
committed
feat: Formatter to Analyzer converter
1 parent dffdb06 commit 8abbb1b

14 files changed

+58
-4
lines changed

pkg/goformatters/analyzer.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package goformatters
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/rogpeppe/go-internal/diff"
10+
"golang.org/x/tools/go/analysis"
11+
12+
"github.com/golangci/golangci-lint/pkg/goanalysis"
13+
"github.com/golangci/golangci-lint/pkg/goformatters/internal"
14+
"github.com/golangci/golangci-lint/pkg/logutils"
15+
)
16+
17+
// NewAnalyzer converts a [Formatter] to an [analysis.Analyzer].
18+
func NewAnalyzer(logger logutils.Log, doc string, formatter Formatter) *analysis.Analyzer {
19+
return &analysis.Analyzer{
20+
Name: formatter.Name(),
21+
Doc: doc,
22+
Run: func(pass *analysis.Pass) (any, error) {
23+
for _, file := range pass.Files {
24+
position, isGoFiles := goanalysis.GetGoFilePosition(pass, file)
25+
if !isGoFiles {
26+
continue
27+
}
28+
29+
input, err := os.ReadFile(position.Filename)
30+
if err != nil {
31+
return nil, fmt.Errorf("unable to open file %s: %w", position.Filename, err)
32+
}
33+
34+
output, err := formatter.Format(position.Filename, input)
35+
if err != nil {
36+
return nil, fmt.Errorf("error while running %s: %w", formatter.Name(), err)
37+
}
38+
39+
if !bytes.Equal(input, output) {
40+
newName := filepath.ToSlash(position.Filename)
41+
oldName := newName + ".orig"
42+
43+
theDiff := diff.Diff(oldName, input, newName, output)
44+
45+
err = internal.ExtractDiagnosticFromPatch(pass, file, string(theDiff), logger)
46+
if err != nil {
47+
return nil, fmt.Errorf("can't extract issues from %s diff output %q: %w", formatter.Name(), string(theDiff), err)
48+
}
49+
}
50+
}
51+
52+
return nil, nil
53+
},
54+
}
55+
}

pkg/golinters/internal/diff.go renamed to pkg/goformatters/internal/diff.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"golang.org/x/tools/go/analysis"
1313

1414
"github.com/golangci/golangci-lint/pkg/goanalysis"
15-
"github.com/golangci/golangci-lint/pkg/lint/linter"
1615
"github.com/golangci/golangci-lint/pkg/logutils"
1716
)
1817

@@ -215,7 +214,7 @@ func ExtractDiagnosticFromPatch(
215214
pass *analysis.Pass,
216215
file *ast.File,
217216
patch string,
218-
lintCtx *linter.Context,
217+
logger logutils.Log,
219218
) error {
220219
diffs, err := diffpkg.ParseMultiFileDiff([]byte(patch))
221220
if err != nil {
@@ -232,12 +231,12 @@ func ExtractDiagnosticFromPatch(
232231

233232
for _, d := range diffs {
234233
if len(d.Hunks) == 0 {
235-
lintCtx.Log.Warnf("Got no hunks in diff %+v", d)
234+
logger.Warnf("Got no hunks in diff %+v", d)
236235
continue
237236
}
238237

239238
for _, hunk := range d.Hunks {
240-
p := hunkChangesParser{log: lintCtx.Log}
239+
p := hunkChangesParser{log: logger}
241240

242241
changes := p.parse(hunk)
243242

0 commit comments

Comments
 (0)