aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gostaticanalysis/analysisutil/pkg.go
blob: b64150d8104a7f5bfdd645f8f35c1d0716dfe6f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package analysisutil

import (
	"go/types"
	"strconv"
	"strings"

	"golang.org/x/tools/go/analysis"
)

// RemoVendor removes vendoring information from import path.
func RemoveVendor(path string) string {
	i := strings.Index(path, "vendor/")
	if i >= 0 {
		return path[i+len("vendor/"):]
	}
	return path
}

// LookupFromImports finds an object from import paths.
func LookupFromImports(imports []*types.Package, path, name string) types.Object {
	path = RemoveVendor(path)
	for i := range imports {
		if path == RemoveVendor(imports[i].Path()) {
			return imports[i].Scope().Lookup(name)
		}
	}
	return nil
}

// Imported returns true when the given pass imports the pkg.
func Imported(pkgPath string, pass *analysis.Pass) bool {
	fs := pass.Files
	if len(fs) == 0 {
		return false
	}
	for _, f := range fs {
		for _, i := range f.Imports {
			path, err := strconv.Unquote(i.Path.Value)
			if err != nil {
				continue
			}
			if RemoveVendor(path) == pkgPath {
				return true
			}
		}
	}
	return false
}