Split out MatchCallByObject into two functions

Allows direct call to GetCallObject.
This commit is contained in:
Grant Murphy 2016-11-04 14:39:22 -07:00
parent 9fa0b726a0
commit 0fef3ad40a

View file

@ -55,20 +55,9 @@ func MatchCall(n ast.Node, r *regexp.Regexp) *ast.CallExpr {
// node, obj := MatchCall(n, ctx, "math/rand", "Read") // node, obj := MatchCall(n, ctx, "math/rand", "Read")
// //
func MatchCallByObject(n ast.Node, c *Context, pkg, name string) (*ast.CallExpr, types.Object) { func MatchCallByObject(n ast.Node, c *Context, pkg, name string) (*ast.CallExpr, types.Object) {
var obj types.Object call, obj := GetCallObject(n, c)
switch node := n.(type) { if obj != nil && obj.Pkg().Path() == pkg && obj.Name() == name {
case *ast.CallExpr: return call, obj
switch fn := node.Fun.(type) {
case *ast.Ident:
obj = c.Info.Uses[fn]
case *ast.SelectorExpr:
obj = c.Info.Uses[fn.Sel]
default:
obj = nil
}
if obj != nil && obj.Pkg().Path() == pkg && obj.Name() == name {
return node, obj
}
} }
return nil, nil return nil, nil
} }
@ -113,3 +102,19 @@ func GetString(n ast.Node) (string, error) {
} }
return "", fmt.Errorf("Unexpected AST node type: %T", n) return "", fmt.Errorf("Unexpected AST node type: %T", n)
} }
// GetCallObject returns the object and call expression and associated
// object for a given AST node. nil, nil will be returned if the
// object cannot be resolved.
func GetCallObject(n ast.Node, ctx *Context) (*ast.CallExpr, types.Object) {
switch node := n.(type) {
case *ast.CallExpr:
switch fn := node.Fun.(type) {
case *ast.Ident:
return node, ctx.Info.Uses[fn]
case *ast.SelectorExpr:
return node, ctx.Info.Uses[fn.Sel]
}
}
return nil, nil
}