forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bump github.com/daixiang0/gci from 0.2.9 to 0.3.0 (golangci#2532)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
- Loading branch information
Showing
13 changed files
with
89 additions
and
110 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
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
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
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,96 +1,48 @@ | ||
package golinters | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"sync" | ||
"strings" | ||
|
||
"github.com/daixiang0/gci/pkg/gci" | ||
"github.com/pkg/errors" | ||
"github.com/shazow/go-diff/difflib" | ||
gci "github.com/daixiang0/gci/pkg/analyzer" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
) | ||
|
||
const gciName = "gci" | ||
|
||
func NewGci() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
differ := difflib.New() | ||
func NewGci(settings *config.GciSettings) *goanalysis.Linter { | ||
var linterCfg map[string]map[string]interface{} | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: gciName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
return goanalysis.NewLinter( | ||
gciName, | ||
"Gci control golang package import order and make it always deterministic.", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
localFlag := lintCtx.Settings().Gci.LocalPrefixes | ||
goimportsFlag := lintCtx.Settings().Goimports.LocalPrefixes | ||
if localFlag == "" && goimportsFlag != "" { | ||
localFlag = goimportsFlag | ||
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), | ||
} | ||
|
||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
var fileNames []string | ||
for _, f := range pass.Files { | ||
pos := pass.Fset.PositionFor(f.Pos(), false) | ||
fileNames = append(fileNames, pos.Filename) | ||
} | ||
|
||
var issues []goanalysis.Issue | ||
|
||
flagSet := gci.FlagSet{ | ||
LocalFlag: gci.ParseLocalFlag(localFlag), | ||
} | ||
|
||
for _, f := range fileNames { | ||
source, result, err := gci.Run(f, &flagSet) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if result == nil { | ||
continue | ||
} | ||
|
||
diff := bytes.Buffer{} | ||
_, err = diff.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f)) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't write diff header: %v", err) | ||
} | ||
|
||
err = differ.Diff(&diff, bytes.NewReader(source), bytes.NewReader(result)) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't get gci diff output: %v", err) | ||
} | ||
|
||
is, err := extractIssuesFromPatch(diff.String(), lintCtx.Log, lintCtx, gciName) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "can't extract issues from gci diff output %q", diff.String()) | ||
} | ||
|
||
for i := range is { | ||
issues = append(issues, goanalysis.NewIssue(&is[i], pass)) | ||
} | ||
} | ||
|
||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
if settings.LocalPrefixes != "" { | ||
prefix := []string{"standard", "default", fmt.Sprintf("prefix(%s)", settings.LocalPrefixes)} | ||
cfg[gci.SectionsFlag] = strings.Join(prefix, gci.SectionDelimiter) | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, issues...) | ||
mu.Unlock() | ||
linterCfg = map[string]map[string]interface{}{ | ||
gci.Analyzer.Name: cfg, | ||
} | ||
} | ||
|
||
return nil, nil | ||
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) | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
linters-settings: | ||
gci: | ||
sections: | ||
- standard | ||
- prefix(github.com/golangci/golangci-lint) | ||
- default |
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