Skip to content

Commit fd8f938

Browse files
committed
feat: merge formatter and fixer processors
1 parent 7600885 commit fd8f938

File tree

3 files changed

+26
-93
lines changed

3 files changed

+26
-93
lines changed

pkg/lint/runner.go

-7
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
6666
return nil, fmt.Errorf("failed to create meta-formatter: %w", err)
6767
}
6868

69-
formatter, err := processors.NewFormatter(log, cfg, metaFormatter)
70-
if err != nil {
71-
return nil, fmt.Errorf("failed to create formatter: %w", err)
72-
}
73-
7469
return &Runner{
7570
Processors: []processors.Processor{
7671
processors.NewCgo(goenv),
@@ -106,8 +101,6 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
106101

107102
// The fixer still needs to see paths for the issues that are relative to the current directory.
108103
processors.NewFixer(cfg, log, fileCache, metaFormatter),
109-
// The formatter needs to be after the fixer and the last processor that write files.
110-
formatter,
111104

112105
// Now we can modify the issues for output.
113106
processors.NewPathPrefixer(cfg.Output.PathPrefix),

pkg/result/processors/fixer.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
8080
formatters := []string{gofumpt.Name, goimports.Name, gofmt.Name, gci.Name}
8181

8282
var notFixableIssues []result.Issue
83+
var formatIssues []result.Issue
8384

8485
for i := range issues {
8586
issue := issues[i]
8687

8788
if slices.Contains(formatters, issue.FromLinter) {
88-
notFixableIssues = append(notFixableIssues, issue)
89+
formatIssues = append(formatIssues, issue)
8990
continue
9091
}
9192

@@ -175,6 +176,8 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
175176

176177
var editError error
177178

179+
var formattedFiles []string
180+
178181
// Now we've got a set of valid edits for each file. Apply them.
179182
for path, edits := range editsByPath {
180183
contents, err := p.fileCache.GetFileBytes(path)
@@ -196,6 +199,28 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
196199
editError = errors.Join(editError, fmt.Errorf("%s: %w", path, err))
197200
continue
198201
}
202+
203+
formattedFiles = append(formattedFiles, path)
204+
}
205+
206+
for i := range formatIssues {
207+
path := issues[i].FilePath()
208+
209+
// Skips files already formatted by the previous fix step.
210+
if slices.Contains(formattedFiles, path) {
211+
content, err := p.fileCache.GetFileBytes(path)
212+
if err != nil {
213+
p.log.Warnf("Error reading file %s: %v", path, err)
214+
continue
215+
}
216+
217+
out := p.formatter.Format(path, content)
218+
219+
if err := os.WriteFile(path, out, filePerm); err != nil {
220+
editError = errors.Join(editError, fmt.Errorf("%s: %w", path, err))
221+
continue
222+
}
223+
}
199224
}
200225

201226
return notFixableIssues, editError

pkg/result/processors/formatter.go

-85
This file was deleted.

0 commit comments

Comments
 (0)