|
1 | 1 | package golinters
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "go/ast" |
6 |
| - "go/token" |
7 |
| - "strings" |
8 |
| - "sync" |
9 |
| - |
10 | 4 | "golang.org/x/tools/go/analysis"
|
11 | 5 |
|
| 6 | + "4d63.com/gochecknoglobals/checknoglobals" |
| 7 | + |
12 | 8 | "github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
|
13 |
| - "github.com/golangci/golangci-lint/pkg/lint/linter" |
14 |
| - "github.com/golangci/golangci-lint/pkg/result" |
15 | 9 | )
|
16 | 10 |
|
17 |
| -const gochecknoglobalsName = "gochecknoglobals" |
18 |
| - |
19 |
| -//nolint:dupl |
20 | 11 | func NewGochecknoglobals() *goanalysis.Linter {
|
21 |
| - var mu sync.Mutex |
22 |
| - var resIssues []goanalysis.Issue |
23 |
| - |
24 |
| - analyzer := &analysis.Analyzer{ |
25 |
| - Name: gochecknoglobalsName, |
26 |
| - Doc: goanalysis.TheOnlyanalyzerDoc, |
27 |
| - Run: func(pass *analysis.Pass) (interface{}, error) { |
28 |
| - var res []goanalysis.Issue |
29 |
| - for _, file := range pass.Files { |
30 |
| - fileIssues := checkFileForGlobals(file, pass.Fset) |
31 |
| - for i := range fileIssues { |
32 |
| - res = append(res, goanalysis.NewIssue(&fileIssues[i], pass)) |
33 |
| - } |
34 |
| - } |
35 |
| - if len(res) == 0 { |
36 |
| - return nil, nil |
37 |
| - } |
38 |
| - |
39 |
| - mu.Lock() |
40 |
| - resIssues = append(resIssues, res...) |
41 |
| - mu.Unlock() |
42 |
| - |
43 |
| - return nil, nil |
| 12 | + gochecknoglobals := checknoglobals.Analyzer() |
| 13 | + |
| 14 | + // gochecknoglobals only lints test files if the `-t` flag is passed so we |
| 15 | + // pass the `t` flag as true to the analyzer before running it. This can be |
| 16 | + // turned of by using the regular golangci-lint flags such as `--tests` or |
| 17 | + // `--skip-files`. |
| 18 | + linterConfig := map[string]map[string]interface{}{ |
| 19 | + gochecknoglobals.Name: { |
| 20 | + "t": true, |
44 | 21 | },
|
45 | 22 | }
|
46 |
| - return goanalysis.NewLinter( |
47 |
| - gochecknoglobalsName, |
48 |
| - "Checks that no globals are present in Go code", |
49 |
| - []*analysis.Analyzer{analyzer}, |
50 |
| - nil, |
51 |
| - ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
52 |
| - return resIssues |
53 |
| - }).WithLoadMode(goanalysis.LoadModeSyntax) |
54 |
| -} |
55 |
| - |
56 |
| -func checkFileForGlobals(f *ast.File, fset *token.FileSet) []result.Issue { |
57 |
| - var res []result.Issue |
58 |
| - for _, decl := range f.Decls { |
59 |
| - genDecl, ok := decl.(*ast.GenDecl) |
60 |
| - if !ok { |
61 |
| - continue |
62 |
| - } |
63 |
| - if genDecl.Tok != token.VAR { |
64 |
| - continue |
65 |
| - } |
66 |
| - |
67 |
| - for _, spec := range genDecl.Specs { |
68 |
| - valueSpec := spec.(*ast.ValueSpec) |
69 |
| - for _, vn := range valueSpec.Names { |
70 |
| - if isWhitelisted(vn) { |
71 |
| - continue |
72 |
| - } |
73 |
| - |
74 |
| - res = append(res, result.Issue{ |
75 |
| - Pos: fset.Position(vn.Pos()), |
76 |
| - Text: fmt.Sprintf("%s is a global variable", formatCode(vn.Name, nil)), |
77 |
| - FromLinter: gochecknoglobalsName, |
78 |
| - }) |
79 |
| - } |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - return res |
84 |
| -} |
85 | 23 |
|
86 |
| -func isWhitelisted(i *ast.Ident) bool { |
87 |
| - return i.Name == "_" || i.Name == "version" || looksLikeError(i) |
88 |
| -} |
89 |
| - |
90 |
| -// looksLikeError returns true if the AST identifier starts |
91 |
| -// with 'err' or 'Err', or false otherwise. |
92 |
| -// |
93 |
| -// TODO: https://github.com/leighmcculloch/gochecknoglobals/issues/5 |
94 |
| -func looksLikeError(i *ast.Ident) bool { |
95 |
| - prefix := "err" |
96 |
| - if i.IsExported() { |
97 |
| - prefix = "Err" |
98 |
| - } |
99 |
| - return strings.HasPrefix(i.Name, prefix) |
| 24 | + return goanalysis.NewLinter( |
| 25 | + gochecknoglobals.Name, |
| 26 | + gochecknoglobals.Doc, |
| 27 | + []*analysis.Analyzer{gochecknoglobals}, |
| 28 | + linterConfig, |
| 29 | + ).WithLoadMode(goanalysis.LoadModeSyntax) |
100 | 30 | }
|
0 commit comments