-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimports.go
44 lines (36 loc) · 1 KB
/
imports.go
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
package varnamelen
import (
"go/ast"
"go/types"
"strings"
)
// importDeclaration is an import declaration.
type importDeclaration struct {
// name is the short name or alias for the imported package. This is either the package's
// default name, or the alias specified in the import statement.
// Not used if self is true.
name string
// path is the full path to the imported package.
path string
// self is true when this is an implicit import declaration for the current package.
self bool
}
// importSpecToDecl returns an import declaration for spec.
func importSpecToDecl(spec *ast.ImportSpec, imports []*types.Package) (importDeclaration, bool) {
path := strings.TrimSuffix(strings.TrimPrefix(spec.Path.Value, `"`), `"`)
if spec.Name != nil {
return importDeclaration{
name: spec.Name.Name,
path: path,
}, true
}
for _, imp := range imports {
if imp.Path() == path {
return importDeclaration{
name: imp.Name(),
path: path,
}, true
}
}
return importDeclaration{}, false
}