-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Update gochecknoglobals, use source analyzer #1422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
960f2ca
Update gochecknoglobals, use source analyzer
bombsimon 6a04c41
Adress linting issues
bombsimon f46c578
gochecknoglobals is no longer internal
bombsimon a60b4ee
Always include include tests in the analyser
bombsimon 617a591
Use proper way to configure linter
bombsimon 853506e
Merge branch 'master' into bump-gochecknoglobals
bombsimon 8bc840e
Fix go.mod
bombsimon 1a6c182
Merge remote-tracking branch 'origin/master' into bump-gochecknoglobals
bombsimon f1cadc7
Fix go.mod - again
bombsimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,100 +1,20 @@ | ||
package golinters | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
"strings" | ||
"sync" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
|
||
"4d63.com/gochecknoglobals/checknoglobals" | ||
|
||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const gochecknoglobalsName = "gochecknoglobals" | ||
|
||
//nolint:dupl | ||
func NewGochecknoglobals() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: gochecknoglobalsName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
Run: func(pass *analysis.Pass) (interface{}, error) { | ||
var res []goanalysis.Issue | ||
for _, file := range pass.Files { | ||
fileIssues := checkFileForGlobals(file, pass.Fset) | ||
for i := range fileIssues { | ||
res = append(res, goanalysis.NewIssue(&fileIssues[i], pass)) | ||
} | ||
} | ||
if len(res) == 0 { | ||
return nil, nil | ||
} | ||
gochecknoglobals := checknoglobals.Analyzer() | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, res...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
}, | ||
} | ||
return goanalysis.NewLinter( | ||
gochecknoglobalsName, | ||
"Checks that no globals are present in Go code", | ||
[]*analysis.Analyzer{analyzer}, | ||
gochecknoglobals.Name, | ||
gochecknoglobals.Doc, | ||
[]*analysis.Analyzer{gochecknoglobals}, | ||
nil, | ||
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} | ||
|
||
func checkFileForGlobals(f *ast.File, fset *token.FileSet) []result.Issue { | ||
var res []result.Issue | ||
for _, decl := range f.Decls { | ||
genDecl, ok := decl.(*ast.GenDecl) | ||
if !ok { | ||
continue | ||
} | ||
if genDecl.Tok != token.VAR { | ||
continue | ||
} | ||
|
||
for _, spec := range genDecl.Specs { | ||
valueSpec := spec.(*ast.ValueSpec) | ||
for _, vn := range valueSpec.Names { | ||
if isWhitelisted(vn) { | ||
continue | ||
} | ||
|
||
res = append(res, result.Issue{ | ||
Pos: fset.Position(vn.Pos()), | ||
Text: fmt.Sprintf("%s is a global variable", formatCode(vn.Name, nil)), | ||
FromLinter: gochecknoglobalsName, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return res | ||
} | ||
|
||
func isWhitelisted(i *ast.Ident) bool { | ||
return i.Name == "_" || i.Name == "version" || looksLikeError(i) | ||
} | ||
|
||
// looksLikeError returns true if the AST identifier starts | ||
// with 'err' or 'Err', or false otherwise. | ||
// | ||
// TODO: https://github.com/leighmcculloch/gochecknoglobals/issues/5 | ||
func looksLikeError(i *ast.Ident) bool { | ||
prefix := "err" | ||
if i.IsExported() { | ||
prefix = "Err" | ||
} | ||
return strings.HasPrefix(i.Name, prefix) | ||
).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.