Skip to content

Commit

Permalink
feat: merge formatter and fixer processors
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 31, 2024
1 parent 7600885 commit fd8f938
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 93 deletions.
7 changes: 0 additions & 7 deletions pkg/lint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
return nil, fmt.Errorf("failed to create meta-formatter: %w", err)
}

formatter, err := processors.NewFormatter(log, cfg, metaFormatter)
if err != nil {
return nil, fmt.Errorf("failed to create formatter: %w", err)
}

return &Runner{
Processors: []processors.Processor{
processors.NewCgo(goenv),
Expand Down Expand Up @@ -106,8 +101,6 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti

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

// Now we can modify the issues for output.
processors.NewPathPrefixer(cfg.Output.PathPrefix),
Expand Down
27 changes: 26 additions & 1 deletion pkg/result/processors/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
formatters := []string{gofumpt.Name, goimports.Name, gofmt.Name, gci.Name}

var notFixableIssues []result.Issue
var formatIssues []result.Issue

for i := range issues {
issue := issues[i]

if slices.Contains(formatters, issue.FromLinter) {
notFixableIssues = append(notFixableIssues, issue)
formatIssues = append(formatIssues, issue)
continue
}

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

var editError error

var formattedFiles []string

// Now we've got a set of valid edits for each file. Apply them.
for path, edits := range editsByPath {
contents, err := p.fileCache.GetFileBytes(path)
Expand All @@ -196,6 +199,28 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
editError = errors.Join(editError, fmt.Errorf("%s: %w", path, err))
continue
}

formattedFiles = append(formattedFiles, path)
}

for i := range formatIssues {
path := issues[i].FilePath()

// Skips files already formatted by the previous fix step.
if slices.Contains(formattedFiles, path) {
content, err := p.fileCache.GetFileBytes(path)
if err != nil {
p.log.Warnf("Error reading file %s: %v", path, err)
continue
}

out := p.formatter.Format(path, content)

if err := os.WriteFile(path, out, filePerm); err != nil {
editError = errors.Join(editError, fmt.Errorf("%s: %w", path, err))
continue
}
}
}

return notFixableIssues, editError
Expand Down
85 changes: 0 additions & 85 deletions pkg/result/processors/formatter.go

This file was deleted.

0 comments on commit fd8f938

Please sign in to comment.