-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
94 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,116 @@ | ||
package gci | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"io" | ||
"os" | ||
|
||
gcicfg "github.com/daixiang0/gci/pkg/config" | ||
"github.com/daixiang0/gci/pkg/gci" | ||
"github.com/daixiang0/gci/pkg/log" | ||
"github.com/shazow/go-diff/difflib" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/golinters/gci/internal" | ||
"github.com/golangci/golangci-lint/pkg/golinters/internal" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
) | ||
|
||
const linterName = "gci" | ||
|
||
const prefixSeparator = "¤" | ||
type differ interface { | ||
Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error | ||
} | ||
|
||
func New(settings *config.GciSettings) *goanalysis.Linter { | ||
a := internal.NewAnalyzer() | ||
|
||
var cfg map[string]map[string]any | ||
if settings != nil { | ||
var sections []string | ||
for _, section := range settings.Sections { | ||
if strings.HasPrefix(section, "prefix(") { | ||
sections = append(sections, strings.ReplaceAll(section, ",", prefixSeparator)) | ||
continue | ||
log.InitLogger() | ||
_ = log.L().Sync() | ||
|
||
diff := difflib.New() | ||
|
||
a := &analysis.Analyzer{ | ||
Name: linterName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
Run: goanalysis.DummyRun, | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
linterName, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
a.Run = func(pass *analysis.Pass) (any, error) { | ||
err := run(lintCtx, pass, settings, diff) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sections = append(sections, section) | ||
return nil, nil | ||
} | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} | ||
|
||
func run(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GciSettings, diff differ) error { | ||
cfg := gcicfg.YamlConfig{ | ||
Cfg: gcicfg.BoolConfig{ | ||
NoInlineComments: settings.NoInlineComments, | ||
NoPrefixComments: settings.NoPrefixComments, | ||
SkipGenerated: settings.SkipGenerated, | ||
CustomOrder: settings.CustomOrder, | ||
NoLexOrder: settings.NoLexOrder, | ||
}, | ||
SectionStrings: settings.Sections, | ||
ModPath: pass.Module.Path, | ||
} | ||
|
||
cfg = map[string]map[string]any{ | ||
a.Name: { | ||
internal.NoInlineCommentsFlag: settings.NoInlineComments, | ||
internal.NoPrefixCommentsFlag: settings.NoPrefixComments, | ||
internal.SkipGeneratedFlag: settings.SkipGenerated, | ||
internal.SectionsFlag: sections, // bug because prefix contains comas. | ||
internal.CustomOrderFlag: settings.CustomOrder, | ||
internal.NoLexOrderFlag: settings.NoLexOrder, | ||
internal.PrefixDelimiterFlag: prefixSeparator, | ||
}, | ||
if settings.LocalPrefixes != "" { | ||
cfg.SectionStrings = []string{ | ||
"standard", | ||
"default", | ||
fmt.Sprintf("prefix(%s)", settings.LocalPrefixes), | ||
} | ||
} | ||
|
||
parsedCfg, err := cfg.Parse() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if settings.LocalPrefixes != "" { | ||
prefix := []string{ | ||
"standard", | ||
"default", | ||
fmt.Sprintf("prefix(%s)", strings.Join(strings.Split(settings.LocalPrefixes, ","), prefixSeparator)), | ||
for _, file := range pass.Files { | ||
position, isGoFile := goanalysis.GetGoFilePosition(pass, file) | ||
if !isGoFile { | ||
continue | ||
} | ||
|
||
input, err := os.ReadFile(position.Filename) | ||
if err != nil { | ||
return fmt.Errorf("unable to open file %s: %w", position.Filename, err) | ||
} | ||
|
||
_, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg) | ||
if err != nil { | ||
return fmt.Errorf("error while running gci: %w", err) | ||
} | ||
|
||
if !bytes.Equal(input, output) { | ||
out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename)) | ||
|
||
err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output)) | ||
if err != nil { | ||
return fmt.Errorf("error while running gci: %w", err) | ||
} | ||
|
||
diff := out.String() | ||
|
||
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx) | ||
if err != nil { | ||
return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err) | ||
} | ||
cfg[a.Name][internal.SectionsFlag] = prefix | ||
} | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
linterName, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
cfg, | ||
).WithLoadMode(goanalysis.LoadModeSyntax) | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters