Skip to content

Commit b8f3f33

Browse files
committed
feat: extract meta formatter from formatter processor
1 parent 8eb7bd8 commit b8f3f33

File tree

3 files changed

+89
-52
lines changed

3 files changed

+89
-52
lines changed

pkg/goformatters/meta_formatter.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package goformatters
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"go/format"
7+
8+
"github.com/golangci/golangci-lint/pkg/config"
9+
"github.com/golangci/golangci-lint/pkg/goformatters/gci"
10+
"github.com/golangci/golangci-lint/pkg/goformatters/gofmt"
11+
"github.com/golangci/golangci-lint/pkg/goformatters/gofumpt"
12+
"github.com/golangci/golangci-lint/pkg/goformatters/goimports"
13+
"github.com/golangci/golangci-lint/pkg/lint/linter"
14+
"github.com/golangci/golangci-lint/pkg/logutils"
15+
)
16+
17+
type MetaFormatter struct {
18+
log logutils.Log
19+
formatters []Formatter
20+
}
21+
22+
func NewMetaFormatter(log logutils.Log, cfg *config.Config, enabledLinters map[string]*linter.Config) (*MetaFormatter, error) {
23+
m := &MetaFormatter{log: log}
24+
25+
if _, ok := enabledLinters[gofmt.Name]; ok {
26+
m.formatters = append(m.formatters, gofmt.New(cfg.LintersSettings.Gofmt))
27+
}
28+
29+
if _, ok := enabledLinters[gofumpt.Name]; ok {
30+
m.formatters = append(m.formatters, gofumpt.New(cfg.LintersSettings.Gofumpt, cfg.Run.Go))
31+
}
32+
33+
if _, ok := enabledLinters[goimports.Name]; ok {
34+
m.formatters = append(m.formatters, goimports.New())
35+
}
36+
37+
// gci is a last because the only goal of gci is to handle imports.
38+
if _, ok := enabledLinters[gci.Name]; ok {
39+
formatter, err := gci.New(cfg.LintersSettings.Gci)
40+
if err != nil {
41+
return nil, fmt.Errorf("gci: creating formatter: %w", err)
42+
}
43+
44+
m.formatters = append(m.formatters, formatter)
45+
}
46+
47+
return m, nil
48+
}
49+
50+
func (m *MetaFormatter) Format(filename string, src []byte) []byte {
51+
if len(m.formatters) == 0 {
52+
data, err := format.Source(src)
53+
if err != nil {
54+
m.log.Warnf("(fmt) formatting file %s: %v", filename, err)
55+
return src
56+
}
57+
58+
return data
59+
}
60+
61+
data := bytes.Clone(src)
62+
63+
for _, formatter := range m.formatters {
64+
formatted, err := formatter.Format(filename, data)
65+
if err != nil {
66+
m.log.Warnf("(%s) formatting file %s: %v", formatter.Name(), filename, err)
67+
continue
68+
}
69+
70+
data = formatted
71+
}
72+
73+
return data
74+
}

pkg/lint/runner.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/golangci/golangci-lint/internal/errorutil"
1111
"github.com/golangci/golangci-lint/pkg/config"
1212
"github.com/golangci/golangci-lint/pkg/fsutils"
13+
"github.com/golangci/golangci-lint/pkg/goformatters"
1314
"github.com/golangci/golangci-lint/pkg/goutil"
1415
"github.com/golangci/golangci-lint/pkg/lint/linter"
1516
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
@@ -60,7 +61,12 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
6061
return nil, fmt.Errorf("failed to get enabled linters: %w", err)
6162
}
6263

63-
formatter, err := processors.NewFormatter(log, cfg, enabledLinters)
64+
metaFormatter, err := goformatters.NewMetaFormatter(log, cfg, enabledLinters)
65+
if err != nil {
66+
return nil, fmt.Errorf("failed to create meta-formatter: %w", err)
67+
}
68+
69+
formatter, err := processors.NewFormatter(log, cfg, metaFormatter)
6470
if err != nil {
6571
return nil, fmt.Errorf("failed to create formatter: %w", err)
6672
}

pkg/result/processors/formatter.go

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package processors
22

33
import (
44
"bytes"
5-
"fmt"
65
"os"
76
"slices"
87

@@ -12,7 +11,6 @@ import (
1211
"github.com/golangci/golangci-lint/pkg/goformatters/gofmt"
1312
"github.com/golangci/golangci-lint/pkg/goformatters/gofumpt"
1413
"github.com/golangci/golangci-lint/pkg/goformatters/goimports"
15-
"github.com/golangci/golangci-lint/pkg/lint/linter"
1614
"github.com/golangci/golangci-lint/pkg/logutils"
1715
"github.com/golangci/golangci-lint/pkg/result"
1816
)
@@ -22,38 +20,17 @@ import (
2220
// - The code format is applied after the fixes to avoid changing positions.
2321
// - The [Fixer] writes the files on the disk (so the file cache cannot be used as it contains the files before fixes).
2422
type Formatter struct {
25-
log logutils.Log
26-
cfg *config.Config
27-
formatters []goformatters.Formatter
23+
log logutils.Log
24+
cfg *config.Config
25+
formatter *goformatters.MetaFormatter
2826
}
2927

3028
// NewFormatter creates a new [Formatter].
31-
func NewFormatter(log logutils.Log, cfg *config.Config, enabledLinters map[string]*linter.Config) (*Formatter, error) {
29+
func NewFormatter(log logutils.Log, cfg *config.Config, formatter *goformatters.MetaFormatter) (*Formatter, error) {
3230
p := &Formatter{
33-
log: log,
34-
cfg: cfg,
35-
}
36-
37-
if _, ok := enabledLinters[gofmt.Name]; ok {
38-
p.formatters = append(p.formatters, gofmt.New(cfg.LintersSettings.Gofmt))
39-
}
40-
41-
if _, ok := enabledLinters[gofumpt.Name]; ok {
42-
p.formatters = append(p.formatters, gofumpt.New(cfg.LintersSettings.Gofumpt, cfg.Run.Go))
43-
}
44-
45-
if _, ok := enabledLinters[goimports.Name]; ok {
46-
p.formatters = append(p.formatters, goimports.New())
47-
}
48-
49-
// gci is a last because the only goal of gci is to handle imports.
50-
if _, ok := enabledLinters[gci.Name]; ok {
51-
formatter, err := gci.New(cfg.LintersSettings.Gci)
52-
if err != nil {
53-
return nil, fmt.Errorf("gci: creating formatter: %w", err)
54-
}
55-
56-
p.formatters = append(p.formatters, formatter)
31+
log: log,
32+
cfg: cfg,
33+
formatter: formatter,
5734
}
5835

5936
return p, nil
@@ -68,10 +45,6 @@ func (p *Formatter) Process(issues []result.Issue) ([]result.Issue, error) {
6845
return issues, nil
6946
}
7047

71-
if len(p.formatters) == 0 {
72-
return issues, nil
73-
}
74-
7548
all := []string{gofumpt.Name, goimports.Name, gofmt.Name, gci.Name}
7649

7750
var notFixableIssues []result.Issue
@@ -95,7 +68,7 @@ func (p *Formatter) Process(issues []result.Issue) ([]result.Issue, error) {
9568
continue
9669
}
9770

98-
formatted := p.format(target, content)
71+
formatted := p.formatter.Format(target, content)
9972
if bytes.Equal(content, formatted) {
10073
continue
10174
}
@@ -109,20 +82,4 @@ func (p *Formatter) Process(issues []result.Issue) ([]result.Issue, error) {
10982
return notFixableIssues, nil
11083
}
11184

112-
func (p *Formatter) format(filename string, src []byte) []byte {
113-
data := bytes.Clone(src)
114-
115-
for _, formatter := range p.formatters {
116-
formatted, err := formatter.Format(filename, data)
117-
if err != nil {
118-
p.log.Warnf("(%s) formatting file %s: %v", formatter.Name(), filename, err)
119-
continue
120-
}
121-
122-
data = formatted
123-
}
124-
125-
return data
126-
}
127-
12885
func (*Formatter) Finish() {}

0 commit comments

Comments
 (0)