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

fix autoNames check importName #445

Merged
merged 1 commit into from
Nov 8, 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
9 changes: 4 additions & 5 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ func (p *Package) big() PkgRef {
type null struct{}
type autoNames struct {
names map[string]null
reqIdx int
autoIdx int
}

Expand All @@ -611,14 +610,14 @@ func (p *autoNames) hasName(name string) bool {
return ok
}

func (p *autoNames) requireName(name string) (ret string, renamed bool) {
func (p *autoNames) importName(name string) (ret string, renamed bool) {
ret = name
var idx int
for p.hasName(ret) {
p.reqIdx++
ret = name + strconv.Itoa(p.reqIdx)
idx++
ret = name + strconv.Itoa(idx)
renamed = true
}
p.useName(ret)
return
}

Expand Down
2 changes: 1 addition & 1 deletion package.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (p astVisitor) Visit(node ast.Node) (w ast.Visitor) {
if id, ok := x.(*ast.Ident); ok && id.Obj != nil {
if used, ok := id.Obj.Data.(importUsed); ok && bool(!used) {
id.Obj.Data = importUsed(true)
if name, renamed := p.this.requireName(id.Name); renamed {
if name, renamed := p.this.importName(id.Name); renamed {
id.Name = name
id.Obj.Name = name
}
Expand Down
82 changes: 82 additions & 0 deletions package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,88 @@ func main() {
`)
}

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

_, err := pkg.SetCurFile("a.go", true)
if err != nil {
t.Fatal("pkg.SetCurFile failed:", err)
}
fmt := pkg.Import("fmt")
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fmt.Ref("Println")).Val("Hello").Call(1).EndStmt().
End()

_, err = pkg.SetCurFile("b.go", true)
if err != nil {
t.Fatal("pkg.SetCurFile failed:", err)
}
pkg.NewFunc(nil, "demo", nil, nil, false).BodyStart(pkg).
Val(fmt.Ref("Println")).Val("Hello").Call(1).EndStmt().
End()

domTestEx(t, pkg, `package main

import "fmt"

func main() {
fmt.Println("Hello")
}
`, "a.go")

domTestEx(t, pkg, `package main

import "fmt"

func demo() {
fmt.Println("Hello")
}
`, "b.go")
}

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

_, err := pkg.SetCurFile("a.go", true)
if err != nil {
t.Fatal("pkg.SetCurFile failed:", err)
}
fmt := pkg.Import("fmt")
v := pkg.NewParam(token.NoPos, "v", types.NewSlice(gogen.TyByte))
pkg.NewFunc(nil, "fmt", gogen.NewTuple(v), nil, false).BodyStart(pkg).End()
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Val(fmt.Ref("Println")).Val("Hello").Call(1).EndStmt().
End()

_, err = pkg.SetCurFile("b.go", true)
if err != nil {
t.Fatal("pkg.SetCurFile failed:", err)
}
pkg.NewFunc(nil, "demo", nil, nil, false).BodyStart(pkg).
Val(fmt.Ref("Println")).Val("Hello").Call(1).EndStmt().
End()

domTestEx(t, pkg, `package main

import fmt1 "fmt"

func fmt(v []byte) {
}
func main() {
fmt1.Println("Hello")
}
`, "a.go")

domTestEx(t, pkg, `package main

import fmt1 "fmt"

func demo() {
fmt1.Println("Hello")
}
`, "b.go")
}

func TestImportUnused(t *testing.T) {
pkg := newMainPackage()
pkg.Import("fmt")
Expand Down