Как найти полный пакет импорта из CallExpr - PullRequest
0 голосов
/ 27 марта 2019

Следующий метод извлекает все открытые вызовы метода из AST файла.Мне нужно выяснить полный пакет из CallExpr, например: ast.Inspect () импортируется из "go / ast".Я хочу сопоставить список строк pkgsInclude с именем импортированного пакета:

func functionCalls(path string, node *ast.File, pkgsInclude []string) int {
    fCalls := 0
    ast.Inspect(node, func(n ast.Node) bool {
        switch fCall := n.(type) {
        case *ast.CallExpr:
            if fun, ok := fCall.Fun.(*ast.SelectorExpr); ok {
                if fun.Sel.IsExported() {
                    fCalls += 1
                }
            }
        }
        return true
    })
    return fCalls
}

1 Ответ

2 голосов
/ 27 марта 2019

Чтобы получить полностью определенные имена, код должен быть проверен на тип с помощью пакета go / types.

В статье go / types Alan Donovan подробно рассказывается о том, как правильно использовать проверку типов, но в этом суть. Для краткости я оставил несколько утверждений типа в методе Visit. В производственном коде вы не должны принимать конкретные типы узлов.

package main

import (
    "fmt"
    "go/ast"
    "go/importer"
    "go/parser"
    "go/token"
    "go/types"
    "log"
)

// code to parse. It includes two variants of calling a package function.
var code = `package main

import (
    foo "io/ioutil"
    . "io/ioutil"
)

func main() {
    foo.ReadFile("")
    ReadFile("")
}
`

func main() {
    fset := &token.FileSet{}
    f, err := parser.ParseFile(fset, "", code, 0)
    if err != nil {
        log.Fatal(err)
    }


    // info.Uses allows to lookup import paths for identifiers.
    info := &types.Info{
        Uses: make(map[*ast.Ident]types.Object),
    }

    // Type check the parsed code using the default importer.
    // Use golang.org/x/tools/go/loader to check a program
    // consisting of multiple packages.
    conf := types.Config{Importer: importer.Default()}

    pkg, err := conf.Check("main", fset, []*ast.File{f}, info)
    if err != nil {
        log.Fatal(err)
    }

    // Do something with ast, info, and possibly pkg
    var _ = pkg

    ast.Walk(v{info}, f)
}

type v struct {
    info *types.Info
}

func (v v) Visit(node ast.Node) (w ast.Visitor) {
    switch node := node.(type) {
    case *ast.CallExpr:
        // Get some kind of *ast.Ident for the CallExpr that represents the
        // package. Then we can look it up in v.info. Where exactly it sits in
        // the ast depends on the form of the function call.

        switch node := node.Fun.(type) {
        case *ast.SelectorExpr: // foo.ReadFile
            pkgID := node.X.(*ast.Ident)
            fmt.Println(v.info.Uses[pkgID].(*types.PkgName).Imported().Path())

        case *ast.Ident:        // ReadFile
            pkgID := node
            fmt.Println(v.info.Uses[pkgID].Pkg().Path())
        }
    }

    return v
}

// Output:
// io/ioutil
// io/ioutil
...