Skip to content

Commit

Permalink
Require an option to be set on analyzer to expand type info (#27)
Browse files Browse the repository at this point in the history
Because type expansion is a bit experimental and to avoid letting the presence of TypesInfo in the config control the analyzer behavior, let's require a new option to enable analyzing types.
  • Loading branch information
ashanbrown authored Feb 25, 2023
1 parent 1396000 commit 7298305
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 23 deletions.
100 changes: 92 additions & 8 deletions forbidigo/config_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion forbidigo/forbidigo.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type config struct {
// don't check inside Godoc examples (see https://blog.golang.org/examples)
ExcludeGodocExamples bool `options:",true"`
IgnorePermitDirectives bool // don't check for `permit` directives(for example, in favor of `nolint`)
AnalyzeTypes bool // enable to match canonical names for types and interfaces using type info
}

func NewLinter(patterns []string, options ...Option) (*Linter, error) {
Expand Down Expand Up @@ -241,7 +242,7 @@ func (v *visitor) expandMatchText(node ast.Node, srcText string) (matchTexts []s
// come up with something different.
matchText := srcText

if v.runConfig.TypesInfo == nil {
if !v.cfg.AnalyzeTypes || v.runConfig.TypesInfo == nil {
return []string{matchText}, pkgText
}

Expand Down
6 changes: 4 additions & 2 deletions forbidigo/forbidigo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func foo() {
})

t.Run("it finds forbidden, renamed identifiers", func(t *testing.T) {
linter, _ := NewLinter([]string{`fmt\.Printf`})
linter, _ := NewLinter([]string{`fmt\.Printf`}, OptionAnalyzeTypes(true))
expectIssues(t, linter, true, `
package bar
Expand Down Expand Up @@ -165,7 +165,9 @@ func ExampleFoo() {
})

t.Run("import renames detected with type information", func(t *testing.T) {
linter, err := NewLinter([]string{`^fmt\.Printf`}, OptionExcludeGodocExamples(false))
linter, err := NewLinter([]string{`^fmt\.Printf`},
OptionExcludeGodocExamples(false),
OptionAnalyzeTypes(true))
require.NoError(t, err)
expectIssues(t, linter, true, `
package bar
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ module github.com/ashanbrown/forbidigo
go 1.12

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.6
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
github.com/stretchr/testify v1.5.1
golang.org/x/sys v0.5.0 // indirect
golang.org/x/tools v0.3.0
gopkg.in/yaml.v2 v2.2.2
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.4.0
)
18 changes: 13 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand All @@ -26,8 +29,9 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand All @@ -41,7 +45,11 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.3.0 h1:SrNbZl6ECOS1qFzgTdQfWXZM9XBkiA6tkFrH9YSTPHM=
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
11 changes: 6 additions & 5 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func (v *listVar) String() string {
type analyzer struct {
patterns []string
usePermitDirective bool
includeExamples bool
expand bool
debugLog func(format string, args ...interface{})
includeExamples bool
analyzeTypes bool
debugLog func(format string, args ...interface{})
}

// NewAnalyzer returns a go/analysis-compatible analyzer
Expand All @@ -52,7 +52,7 @@ func newAnalyzer(debugLog func(format string, args ...interface{})) *analysis.An
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`)
flags.BoolVar(&a.expand, "analyze_types", false, `when set, expressions get expanded instead of matching the literal source code`)
flags.BoolVar(&a.analyzeTypes, "analyze_types", false, `when set, expressions get expanded instead of matching the literal source code`)
return &analysis.Analyzer{
Name: "forbidigo",
Doc: "forbid identifiers",
Expand All @@ -68,6 +68,7 @@ func (a *analyzer) runAnalysis(pass *analysis.Pass) (interface{}, error) {
linter, err := forbidigo.NewLinter(a.patterns,
forbidigo.OptionIgnorePermitDirectives(!a.usePermitDirective),
forbidigo.OptionExcludeGodocExamples(!a.includeExamples),
forbidigo.OptionAnalyzeTypes(a.analyzeTypes),
)
if err != nil {
return nil, errors.Wrapf(err, "failed to configure linter")
Expand All @@ -77,7 +78,7 @@ func (a *analyzer) runAnalysis(pass *analysis.Pass) (interface{}, error) {
nodes = append(nodes, f)
}
config := forbidigo.RunConfig{Fset: pass.Fset, DebugLog: a.debugLog}
if a.expand {
if a.analyzeTypes {
config.TypesInfo = pass.TypesInfo
}
issues, err := linter.RunWithConfig(config, nodes...)
Expand Down

0 comments on commit 7298305

Please sign in to comment.