Merge pull request #23 from carlsverre/master

support importing from source files if possible
This commit is contained in:
Álvaro López Espinosa 2018-02-05 11:29:12 +00:00 committed by GitHub
commit ebfd0d9bb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 3 deletions

View File

@ -33,7 +33,7 @@ func TestEndToEnd(t *testing.T) {
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
// Create stringer in temporary directory. // Create stringer in temporary directory.
stringer := filepath.Join(dir, "stringer.exe") stringer := filepath.Join(dir, "stringer.exe")
err = run("go", "build", "-o", stringer, "enumer.go", "sql.go", "stringer.go") err = run("go", "build", "-o", stringer)
if err != nil { if err != nil {
t.Fatalf("building stringer: %s", err) t.Fatalf("building stringer: %s", err)
} }

16
importer18.go Normal file
View File

@ -0,0 +1,16 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !go1.9
package main
import (
"go/importer"
"go/types"
)
func defaultImporter() types.Importer {
return importer.Default()
}

16
importer19.go Normal file
View File

@ -0,0 +1,16 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.9
package main
import (
"go/importer"
"go/types"
)
func defaultImporter() types.Importer {
return importer.For("source", nil)
}

View File

@ -68,7 +68,6 @@ import (
"go/build" "go/build"
exact "go/constant" exact "go/constant"
"go/format" "go/format"
"go/importer"
"go/parser" "go/parser"
"go/token" "go/token"
"go/types" "go/types"
@ -275,7 +274,7 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac
// check type-checks the package. The package must be OK to proceed. // check type-checks the package. The package must be OK to proceed.
func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) {
pkg.defs = make(map[*ast.Ident]types.Object) pkg.defs = make(map[*ast.Ident]types.Object)
config := types.Config{Importer: importer.Default(), FakeImportC: true} config := types.Config{Importer: defaultImporter(), FakeImportC: true}
info := &types.Info{ info := &types.Info{
Defs: pkg.defs, Defs: pkg.defs,
} }