Skip to content

Commit

Permalink
Merge pull request #349 from xushiwei/imp
Browse files Browse the repository at this point in the history
MarkForceUsed
  • Loading branch information
xushiwei committed Jan 24, 2024
2 parents 1ed2a9e + a6a47f1 commit 576b08a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (p PkgRef) TryRef(name string) Ref {
return p.Types.Scope().Lookup(name)
}

// MarkForceUsed marks to import a package always (i.e. `import _ pkgPath`).
func (p PkgRef) MarkForceUsed(pkg *Package) {
pkg.file.forceImport(p.Types.Path())
}

// Deprecated: EnsureImported is nothing to do now.
func (p PkgRef) EnsureImported() {
}
Expand Down
13 changes: 13 additions & 0 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,19 @@ func main() {
`)
}

func TestImportForceUsed2(t *testing.T) {
pkg := newMainPackage()
pkg.Import("fmt").MarkForceUsed(pkg)
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).End()
domTest(t, pkg, `package main
import _ "fmt"
func main() {
}
`)
}

func TestImportAnyWhere(t *testing.T) {
pkg := newMainPackage()

Expand Down

0 comments on commit 576b08a

Please sign in to comment.