-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analysis.Analyzer interface (#14)
Also update linters
- Loading branch information
1 parent
d31d534
commit 4ddb62d
Showing
8 changed files
with
126 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
linters: | ||
enable-all: true | ||
enable: | ||
- prealloc |
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,5 +1,5 @@ | ||
repos: | ||
- repo: https://github.com/golangci/golangci-lint | ||
rev: v1.17.1 | ||
rev: v1.44.2 | ||
hooks: | ||
- id: golangci-lint |
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,87 @@ | ||
package analyzer | ||
|
||
import ( | ||
"flag" | ||
"go/ast" | ||
|
||
"github.com/ashanbrown/forbidigo/forbidigo" | ||
"github.com/pkg/errors" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
type listVar struct { | ||
values *[]string | ||
} | ||
|
||
func (v *listVar) Set(value string) error { | ||
*v.values = append(*v.values, value) | ||
if value == "" { | ||
return errors.New("value cannot be empty") | ||
} | ||
return nil | ||
} | ||
|
||
func (v *listVar) String() string { | ||
return "" | ||
} | ||
|
||
type analyzer struct { | ||
patterns []string | ||
usePermitDirective bool | ||
includeExamples bool | ||
} | ||
|
||
// NewAnalyzer returns a go/analysis-compatible analyzer | ||
// The "-p" argument can be used to add a pattern. | ||
// Set "-examples" to analyze godoc examples | ||
// Set "-permit=false" to ignore "//permit:<identifier>" directives. | ||
func NewAnalyzer() *analysis.Analyzer { | ||
var flags flag.FlagSet | ||
a := analyzer{ | ||
usePermitDirective: true, | ||
includeExamples: true, | ||
} | ||
flags.Var(&listVar{values: &a.patterns}, "p", "pattern") | ||
flags.BoolVar(&a.includeExamples, "examples", false, "check godoc examples") | ||
flags.BoolVar(&a.usePermitDirective, "permit", true, `when set, lines with "//permit" directives will be ignored`) | ||
return &analysis.Analyzer{ | ||
Name: "forbidigo", | ||
Doc: "forbid identifiers", | ||
Run: a.runAnalysis, | ||
Flags: flags, | ||
} | ||
} | ||
|
||
func (a *analyzer) runAnalysis(pass *analysis.Pass) (interface{}, error) { | ||
if a.patterns == nil { | ||
a.patterns = forbidigo.DefaultPatterns() | ||
} | ||
linter, err := forbidigo.NewLinter(a.patterns, | ||
forbidigo.OptionIgnorePermitDirectives(!a.usePermitDirective), | ||
forbidigo.OptionExcludeGodocExamples(!a.includeExamples), | ||
) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to configure linter") | ||
} | ||
nodes := make([]ast.Node, 0, len(pass.Files)) | ||
for _, f := range pass.Files { | ||
nodes = append(nodes, f) | ||
} | ||
issues, err := linter.Run(pass.Fset, nodes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reportIssues(pass, issues) | ||
return nil, nil | ||
} | ||
|
||
func reportIssues(pass *analysis.Pass, issues []forbidigo.Issue) { | ||
for _, i := range issues { | ||
diag := analysis.Diagnostic{ | ||
Pos: i.Pos(), | ||
Message: i.Details(), | ||
Category: "restriction", | ||
} | ||
pass.Report(diag) | ||
} | ||
} |
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,14 @@ | ||
package analyzer_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ashanbrown/forbidigo/pkg/analyzer" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestAnalyzer(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
a := analyzer.NewAnalyzer() | ||
analysistest.Run(t, testdata, a, "") | ||
} |
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,10 @@ | ||
package testdata | ||
|
||
import "fmt" | ||
|
||
func Foo() { | ||
fmt.Println("here I am") // want "forbidden by pattern" | ||
fmt.Printf("this is ok") //permit:fmt.Printf // this is ok | ||
print("not ok") // want "forbidden by pattern" | ||
println("also not ok") // want "forbidden by pattern" | ||
} |