Skip to content

Commit ab8eef6

Browse files
authored
Don't check names in declarations (#40)
Restrict checks to types, methods and fields
1 parent f6adf99 commit ab8eef6

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

forbidigo/forbidigo.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,40 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
186186
if isGodocExample && v.cfg.ExcludeGodocExamples {
187187
return nil
188188
}
189-
return v
189+
ast.Walk(v, node.Type)
190+
if node.Body != nil {
191+
ast.Walk(v, node.Body)
192+
}
193+
return nil
194+
// Ignore constant and type names
195+
case *ast.ValueSpec:
196+
// Look at only type and values for const and variable specs, and not names
197+
if node.Type != nil {
198+
ast.Walk(v, node.Type)
199+
}
200+
if node.Values != nil {
201+
for _, x := range node.Values {
202+
ast.Walk(v, x)
203+
}
204+
}
205+
return nil
206+
// Ignore import alias names
207+
case *ast.ImportSpec:
208+
return nil
209+
// Ignore type names
210+
case *ast.TypeSpec:
211+
// Look at only type parameters for type spec
212+
if node.TypeParams != nil {
213+
ast.Walk(v, node.TypeParams)
214+
}
215+
ast.Walk(v, node.Type)
216+
return nil
217+
// Ignore field names
218+
case *ast.Field:
219+
if node.Type != nil {
220+
ast.Walk(v, node.Type)
221+
}
222+
return nil
190223
// The following two are handled below.
191224
case *ast.SelectorExpr:
192225
case *ast.Ident:

forbidigo/forbidigo_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,63 @@ func ExampleFoo() {
179179
}`, "use of `fmt2.Printf` forbidden by pattern `^fmt\\.Printf` at testing.go:7:2")
180180
})
181181

182+
t.Run("it ignores function names but checks return type", func(t *testing.T) {
183+
linter, _ := NewLinter([]string{`Foo`}, OptionAnalyzeTypes(true))
184+
expectIssues(t, linter, true, `
185+
package bar
186+
187+
func Foo() {}
188+
func Bad() Foo {}
189+
}`, "use of `Foo` forbidden by pattern `Foo` at testing.go:5:12")
190+
})
191+
192+
t.Run("it ignores type names but checks type", func(t *testing.T) {
193+
linter, _ := NewLinter([]string{`Foo`}, OptionAnalyzeTypes(true))
194+
expectIssues(t, linter, true, `
195+
package bar
196+
197+
type Foo struct {
198+
Ok int
199+
Bad Foo
200+
}`, "use of `Foo` forbidden by pattern `Foo` at testing.go:6:7")
201+
})
202+
203+
t.Run("it ignores constant names but checks type", func(t *testing.T) {
204+
linter, _ := NewLinter([]string{`Foo`}, OptionAnalyzeTypes(true))
205+
expectIssues(t, linter, true, `
206+
package bar
207+
208+
const Foo = 1;
209+
const Bad Foo = 1;
210+
`, "use of `Foo` forbidden by pattern `Foo` at testing.go:5:11")
211+
})
212+
213+
t.Run("it ignores import alises", func(t *testing.T) {
214+
linter, _ := NewLinter([]string{`Foo`}, OptionAnalyzeTypes(true))
215+
expectIssues(t, linter, true, `
216+
package bar
217+
218+
import Foo "foo"
219+
`)
220+
})
182221
}
183222

184223
// sourcePath matches "at /tmp/TestForbiddenIdentifiersdisplays_custom_messages4260088387/001/testing.go".
185224
var sourcePath = regexp.MustCompile(`at .*/([[:alnum:]]+.go)`)
186225

187226
func expectIssues(t *testing.T, linter *Linter, expand bool, contents string, issues ...string) {
227+
t.Helper()
188228
actualIssues := parseFile(t, linter, expand, "testing.go", contents)
189229
actualIssueStrs := make([]string, 0, len(actualIssues))
190230
for _, i := range actualIssues {
191231
str := i.String()
192232
str = sourcePath.ReplaceAllString(str, "at $1")
193233
actualIssueStrs = append(actualIssueStrs, str)
194234
}
195-
assert.ElementsMatch(t, issues, actualIssueStrs)
235+
if !assert.ElementsMatch(t, issues, actualIssueStrs) {
236+
t.Logf("Expected: %v", issues)
237+
t.Logf("Got: %v", actualIssueStrs)
238+
}
196239
}
197240

198241
func parseFile(t *testing.T, linter *Linter, expand bool, fileName, contents string) []Issue {

0 commit comments

Comments
 (0)