2021-11-14 20:01:54 +00:00
|
|
|
package golinters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-24 16:33:24 +00:00
|
|
|
"strings"
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
gci "github.com/daixiang0/gci/pkg/analyzer"
|
2021-11-14 20:01:54 +00:00
|
|
|
"golang.org/x/tools/go/analysis"
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
"github.com/golangci/golangci-lint/pkg/config"
|
2021-11-14 20:01:54 +00:00
|
|
|
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
|
|
|
|
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
|
|
|
)
|
|
|
|
|
|
|
|
const gciName = "gci"
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
func NewGci(settings *config.GciSettings) *goanalysis.Linter {
|
|
|
|
var linterCfg map[string]map[string]interface{}
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
if settings != nil {
|
|
|
|
cfg := map[string]interface{}{
|
|
|
|
gci.NoInlineCommentsFlag: settings.NoInlineComments,
|
|
|
|
gci.NoPrefixCommentsFlag: settings.NoPrefixComments,
|
|
|
|
gci.SectionsFlag: strings.Join(settings.Sections, gci.SectionDelimiter),
|
|
|
|
gci.SectionSeparatorsFlag: strings.Join(settings.SectionSeparator, gci.SectionDelimiter),
|
2021-11-14 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
if settings.LocalPrefixes != "" {
|
|
|
|
prefix := []string{"standard", "default", fmt.Sprintf("prefix(%s)", settings.LocalPrefixes)}
|
|
|
|
cfg[gci.SectionsFlag] = strings.Join(prefix, gci.SectionDelimiter)
|
|
|
|
}
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
linterCfg = map[string]map[string]interface{}{
|
|
|
|
gci.Analyzer.Name: cfg,
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 20:01:54 +00:00
|
|
|
|
2022-02-24 16:33:24 +00:00
|
|
|
return goanalysis.NewLinter(
|
|
|
|
gciName,
|
|
|
|
"Gci controls golang package import order and makes it always deterministic.",
|
|
|
|
[]*analysis.Analyzer{gci.Analyzer},
|
|
|
|
linterCfg,
|
|
|
|
).WithContextSetter(func(lintCtx *linter.Context) {
|
|
|
|
if settings.LocalPrefixes != "" {
|
|
|
|
lintCtx.Log.Warnf("gci: `local-prefixes` is deprecated, use `sections` and `prefix(%s)` instead.", settings.LocalPrefixes)
|
2021-11-14 20:01:54 +00:00
|
|
|
}
|
|
|
|
}).WithLoadMode(goanalysis.LoadModeSyntax)
|
|
|
|
}
|