Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File: CheckGopDeps #375

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ func getConf() *Config {
return &Config{Fset: fset, Importer: imp}
}

func TestCheckGopDeps(t *testing.T) {
pkg := NewPackage("", "foo", nil)
file := pkg.CurFile()
id := file.newImport("env", "github.com/goplus/gop/env")
file.forceImport("github.com/qiniu/x/errors")
file.getDecls(pkg)
if v := file.CheckGopDeps(pkg); v != FlagDepModX {
t.Fatal("CheckGopDeps:", v)
}
id.Obj.Data = importUsed(true)
if v := file.CheckGopDeps(pkg); v != FlagDepModGop|FlagDepModX {
t.Fatal("CheckGopDeps:", v)
}
}

func TestInitGopPkg(t *testing.T) {
pkg := NewPackage("", "foo", nil)
pkg.Types.Scope().Insert(types.NewVar(
Expand Down
45 changes: 42 additions & 3 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go/types"
"log"
"sort"
"strings"
"syscall"

"github.com/goplus/gox/packages"
Expand Down Expand Up @@ -139,7 +140,8 @@ type importUsed bool
type File struct {
decls []ast.Decl
fname string
imps map[string]*ast.Ident // importPath => impRef
imps map[string]*ast.Ident // importPath => impRef (nil means force-import)
dirty bool
}

func newFile(fname string) *File {
Expand All @@ -151,13 +153,22 @@ func (p *File) newImport(name, pkgPath string) *ast.Ident {
if id == nil {
id = &ast.Ident{Name: name, Obj: &ast.Object{Data: importUsed(false)}}
p.imps[pkgPath] = id
p.dirty = true
}
return id
}

func (p *File) forceImport(pkgPath string) {
if _, ok := p.imps[pkgPath]; !ok {
p.imps[pkgPath] = nil
p.dirty = true
}
}

func (p *File) markUsed(this *Package) {
if p.dirty {
astVisitor{this}.markUsed(p.decls)
p.dirty = false
}
}

Expand Down Expand Up @@ -218,9 +229,37 @@ func (p astVisitor) markUsed(decls []ast.Decl) {
}
}

func (p *File) getDecls(this *Package) (decls []ast.Decl) {
astVisitor{this}.markUsed(p.decls)
func isPkgInMod(pkgPath, modPath string) bool {
if strings.HasPrefix(pkgPath, modPath) {
suffix := pkgPath[len(modPath):]
return suffix == "" || suffix[0] == '/'
}
return false
}

const (
FlagDepModGop = 1 << iota // depends module github.com/goplus/gop
FlagDepModX // depends module github.com/qiniu/x
)

// CheckGopDeps checks dependencies of Go+ modules.
// The return flags can be FlagDepModGop and FlagDepModX.
func (p *File) CheckGopDeps(this *Package) (flags int) {
p.markUsed(this)
for pkgPath, id := range p.imps {
if id == nil || id.Obj.Data.(importUsed) {
if isPkgInMod(pkgPath, "github.com/goplus/gop") {
flags |= FlagDepModGop
} else if isPkgInMod(pkgPath, "github.com/qiniu/x") {
flags |= FlagDepModX
}
}
}
return
}

func (p *File) getDecls(this *Package) (decls []ast.Decl) {
p.markUsed(this)
specs := make([]ast.Spec, 0, len(p.imps))
for pkgPath, id := range p.imps {
if id == nil { // force-used
Expand Down