Last active
November 10, 2024 09:45
-
-
Save hajimehoshi/c93fb989308df34b6fc881ef4be575c4 to your computer and use it in GitHub Desktop.
Go AST memo
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
// https://cs.opensource.google/go/x/tools/+/refs/tags/v0.27.0:go/types/typeutil/callee.go might be better | |
func checkGlobalFunctionCall(call *ast.CallExpr, pkg *packages.Package) (pkgPath, funcName string, ok bool) { | |
switch caller := call.Fun.(type) { | |
case *ast.SelectorExpr: | |
// Skip if the selector is a method call. | |
if sel := pkg.TypesInfo.Selections[caller]; sel != nil { | |
return | |
} | |
ident, ok := caller.X.(*ast.Ident) | |
if !ok { | |
return "", "", false | |
} | |
pkgName, ok := pkg.TypesInfo.ObjectOf(ident).(*types.PkgName) | |
if !ok { | |
return "", "", false | |
} | |
return pkgName.Imported().Path(), caller.Sel.Name, true | |
case *ast.Ident: | |
// Consider the dot import. | |
fn, ok := pkg.TypesInfo.ObjectOf(caller).(*types.Func) | |
if !ok { | |
return "", "", false | |
} | |
return fn.Pkg().Path(), fn.Name(), true | |
default: | |
return "", "", false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment