Skip to content

Commit

Permalink
Merge pull request #445 from visualfc/importname
Browse files Browse the repository at this point in the history
fix autoNames check importName
  • Loading branch information
xushiwei authored Nov 8, 2024
2 parents 38cdc04 + 6716d24 commit d741757
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
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

0 comments on commit d741757

Please sign in to comment.