-
Notifications
You must be signed in to change notification settings - Fork 132
#488 type def first checker #960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d1adeaa
eaa4b99
8d8e606
b47f2b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package checker_test | ||
|
|
||
| type rec struct{} | ||
|
|
||
| func (r rec) Method() {} | ||
| func (r *rec) MethodWithRef() {} | ||
|
|
||
| // Considering no type declaration in file it is also ok | ||
| func (r recv) Method2() {} | ||
|
|
||
| func JustFunction() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package checker_test | ||
|
|
||
| func (r recv) MethodBefore() {} | ||
|
|
||
| /*! definition of type 'recv' should appear before its methods */ | ||
| type recv struct{} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package checker_test | ||
|
|
||
| func (r *reciv) Method() {} | ||
|
|
||
| /*! definition of type 'reciv' should appear before its methods */ | ||
| type reciv struct{ x, y int } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package checkers | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/token" | ||
|
|
||
| "github.com/go-critic/go-critic/checkers/internal/astwalk" | ||
| "github.com/go-critic/go-critic/framework/linter" | ||
| ) | ||
|
|
||
| func init() { | ||
| var info linter.CheckerInfo | ||
| info.Name = "typeDefFirst" | ||
| info.Tags = []string{"style", "experimental"} | ||
| info.Summary = "File-scoped checker, that requires type definition before its method definitions" | ||
| info.Before = ` | ||
| func (r rec) Method() {} | ||
| type rec struct{} | ||
| ` | ||
| info.After = ` | ||
| type rec struct{} | ||
| func (r rec) Method() {} | ||
| ` | ||
| collection.AddChecker(&info, func(ctx *linter.CheckerContext) linter.FileWalker { | ||
| return &typeDefFirstChecker{ | ||
| ctx: ctx, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| type typeDefFirstChecker struct { | ||
| astwalk.WalkHandler | ||
| ctx *linter.CheckerContext | ||
| } | ||
|
|
||
| func (c *typeDefFirstChecker) WalkFile(f *ast.File) { | ||
| if f.Decls == nil { | ||
| return | ||
| } | ||
|
|
||
| typeUsageMap := make(map[string]bool) | ||
| for _, declaration := range f.Decls { | ||
| switch decl := declaration.(type) { | ||
| case *ast.FuncDecl: | ||
| if decl.Recv != nil { | ||
| receiver := decl.Recv.List[0] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should I check the array for not nilness? as far as I understood, in this case (where its receiver) there are always should be some fields, but the safer the better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can add a simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not fail, because it goes after |
||
| typeName := c.receiverType(receiver.Type) | ||
| typeUsageMap[typeName] = true | ||
| } | ||
|
|
||
| case *ast.GenDecl: | ||
| if decl.Tok != token.TYPE { | ||
| continue | ||
| } | ||
| for _, spec := range decl.Specs { | ||
| if spec, ok := spec.(*ast.TypeSpec); ok { | ||
| typeName := spec.Name.Name | ||
| if val, ok := typeUsageMap[typeName]; ok && val { | ||
| c.warn(decl, typeName) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (c *typeDefFirstChecker) warn(cause ast.Node, typeName string) { | ||
| c.ctx.Warn(cause, "definition of type '%s' should appear before its methods", typeName) | ||
| } | ||
|
|
||
| func (c *typeDefFirstChecker) receiverType(e ast.Expr) string { | ||
| switch e := e.(type) { | ||
| case *ast.StarExpr: | ||
| return c.receiverType(e.X) | ||
| case *ast.Ident: | ||
| return e.Name | ||
| default: | ||
| panic("unreachable") | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File-scoped checkerI'm curious - should be use this phrase in other checkers? maybe another tag or subtype? @quasilyte :)