-
-
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.
- Loading branch information
1 parent
be02979
commit 222076f
Showing
8 changed files
with
96 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package golinters | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ashanbrown/forbidigo/forbidigo" | ||
"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" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const forbidigoName = "forbidigo" | ||
|
||
func NewForbidigo() *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: forbidigoName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
} | ||
return goanalysis.NewLinter( | ||
forbidigoName, | ||
"Forbids identifiers", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
s := &lintCtx.Settings().Forbidigo | ||
|
||
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { | ||
var res []goanalysis.Issue | ||
forbid, err := forbidigo.NewLinter(s.Forbid) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to create linter %q", forbidigoName) | ||
} | ||
|
||
for _, file := range pass.Files { | ||
hints, err := forbid.Run(pass.Fset, file) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "forbidigo linter failed on file %q", file.Name.String()) | ||
} | ||
for _, hint := range hints { | ||
res = append(res, goanalysis.NewIssue(&result.Issue{ | ||
Pos: hint.Position(), | ||
Text: hint.Details(), | ||
FromLinter: makezeroName, | ||
}, pass)) | ||
} | ||
} | ||
|
||
if len(res) == 0 { | ||
return nil, nil | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, res...) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
linters-settings: | ||
forbidigo: | ||
forbid: | ||
- fmt\.Print.* |
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,9 @@ | ||
//args: -Eforbidigo | ||
//config_path: testdata/configs/forbidigo.yml | ||
package testdata | ||
|
||
import "fmt" | ||
|
||
func Forbidigo() { | ||
fmt.Printf("too noisy!!!") // ERROR "use of `fmt.Printf` forbidden by pattern `fmt\\.Print.*`" | ||
} |