Skip to content

Commit 4c8431a

Browse files
committed
fixes
1 parent e12151a commit 4c8431a

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

importer.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ func NewImporter(options ...Option) *Importer {
3939
return i
4040
}
4141

42-
// Import returns go package scope
43-
func (i *Importer) Import(path, src string) (Type, error) {
44-
return i.importParse(path, src)
45-
}
46-
4742
// ImportPackage returns go package scope
4843
func (i *Importer) ImportPackage(path string, pkg *ast.Package) (Type, error) {
4944
np := newParser(i, i.isCommentLocator, path, false)
@@ -79,7 +74,14 @@ func (i *Importer) ImportBuild(path string, src string) (*build.Package, error)
7974
src = filepath.Clean(src)
8075
gopath := filepath.Join(i.ctx.GOPATH, "src")
8176
rsrc := src
82-
if !filepath.HasPrefix(src, gopath) {
77+
78+
if filepath.HasPrefix(src, ".") {
79+
pwd, err := os.Getwd()
80+
if err != nil {
81+
return nil, err
82+
}
83+
rsrc = filepath.Join(pwd, src)
84+
} else if !filepath.HasPrefix(src, gopath) {
8385
rsrc = filepath.Join(gopath, src)
8486
}
8587
k := path + " " + rsrc
@@ -88,22 +90,31 @@ func (i *Importer) ImportBuild(path string, src string) (*build.Package, error)
8890
}
8991
imp, err := i.ctx.Import(path, rsrc, 0)
9092
if err != nil {
91-
i.errorHandler(err)
93+
i.appendError(err)
9294
return nil, err
9395
}
9496
i.bufBuild[k] = imp
9597
return imp, nil
9698
}
9799

98-
func (i *Importer) importName(path string, src string) (name string, goroot bool) {
100+
// appendError append error
101+
func (i *Importer) appendError(err error) {
102+
if i.errorHandler != nil {
103+
i.errorHandler(err)
104+
}
105+
}
106+
107+
// ImportName returns go package name
108+
func (i *Importer) ImportName(path string, src string) (name string, goroot bool) {
99109
imp, err := i.ImportBuild(path, src)
100110
if err != nil {
101111
return "", false
102112
}
103113
return imp.Name, imp.Goroot
104114
}
105115

106-
func (i *Importer) importParse(path string, src string) (Type, error) {
116+
// Import returns go package scope
117+
func (i *Importer) Import(path string, src string) (Type, error) {
107118
imp, err := i.ImportBuild(path, src)
108119
if err != nil {
109120
return nil, err
@@ -125,7 +136,7 @@ func (i *Importer) importParse(path string, src string) (Type, error) {
125136
}, i.mode)
126137

127138
if err != nil {
128-
i.errorHandler(err)
139+
i.appendError(err)
129140
return nil, err
130141
}
131142

@@ -136,6 +147,6 @@ func (i *Importer) importParse(path string, src string) (Type, error) {
136147
return t, nil
137148
}
138149
err = fmt.Errorf(`No go source code was found under the package path "%s"`, path)
139-
i.errorHandler(err)
150+
i.appendError(err)
140151
return nil, err
141152
}

misc.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
)
88

99
type importer interface {
10-
importParse(path string, src string) (Type, error)
11-
importName(path string, src string) (name string, goroot bool)
10+
appendError(err error)
11+
Import(path string, src string) (Type, error)
12+
ImportName(path string, src string) (name string, goroot bool)
1213
}
1314

1415
// typeName 解析表达式获取类型名字以及是否是导入的

parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ func (r *parser) parseImport(decl *ast.GenDecl) {
145145
switch s.Name.Name {
146146
case "_":
147147
case ".":
148-
p, err := r.importer.importParse(path, r.info.PkgPath)
148+
p, err := r.importer.Import(path, r.info.PkgPath)
149149
if err != nil {
150+
r.importer.appendError(err)
150151
continue
151152
}
152153
l := p.NumChild()

types_importer.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ func (t *typeImport) check() {
2323
return
2424
}
2525

26-
s, _ := t.imp.importParse(t.path, t.src)
26+
s, err := t.imp.Import(t.path, t.src)
27+
if err != nil {
28+
t.imp.appendError(err)
29+
}
2730
t.scope = s
2831
}
2932

@@ -32,7 +35,7 @@ func (t *typeImport) PkgPath() string {
3235
}
3336

3437
func (t *typeImport) IsGoroot() bool {
35-
_, ok := t.imp.importName(t.path, t.src)
38+
_, ok := t.imp.ImportName(t.path, t.src)
3639
return ok
3740
}
3841

@@ -45,7 +48,7 @@ func (t *typeImport) Name() string {
4548
return t.name
4649
}
4750

48-
t.name, _ = t.imp.importName(t.path, t.src)
51+
t.name, _ = t.imp.ImportName(t.path, t.src)
4952
return t.name
5053
}
5154

0 commit comments

Comments
 (0)