-
-
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.
* Introduce gci as new linter Signed-off-by: Xiang Dai <long0dai@foxmail.com> * use goimports setting if not specified Signed-off-by: Xiang Dai <long0dai@foxmail.com>
- Loading branch information
Showing
11 changed files
with
155 additions
and
1 deletion.
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
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,77 @@ | ||
package golinters | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/daixiang0/gci/pkg/gci" | ||
"github.com/pkg/errors" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"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 | ||
|
||
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 | ||
} | ||
|
||
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 | ||
|
||
for _, f := range fileNames { | ||
diff, err := gci.Run(f, &gci.FlagSet{LocalFlag: localFlag}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if diff == nil { | ||
continue | ||
} | ||
|
||
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, gciName) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "can't extract issues from gci diff output %q", string(diff)) | ||
} | ||
|
||
for i := range is { | ||
issues = append(issues, goanalysis.NewIssue(&is[i], pass)) | ||
} | ||
} | ||
|
||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, issues...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
} | ||
}).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//args: -Egci | ||
package testdata | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func GoimportsLocalTest() { | ||
fmt.Print("x") | ||
_ = config.Config{} | ||
_ = errors.New("") | ||
} |
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,17 @@ | ||
//args: -Egci | ||
//config: linters-settings.gci.local-prefixes=github.com/golangci/golangci-lint | ||
package gci | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func GoimportsLocalTest() { | ||
fmt.Print("x") | ||
_ = config.Config{} | ||
_ = errors.New("") | ||
} |