astutil: delete unused function RenameTop.

Fixes golang/go#9008
(in a manner of speaking)

LGTM=crawshaw
R=crawshaw
CC=bradfitz, golang-codereviews
https://golang.org/cl/165010043
This commit is contained in:
Alan Donovan 2014-10-28 12:19:23 -04:00
parent 4bb917e48d
commit dcf508a4ed
2 changed files with 0 additions and 104 deletions

View File

@ -14,7 +14,6 @@ import (
"go/parser" "go/parser"
"go/token" "go/token"
"log" "log"
"path"
"strconv" "strconv"
"strings" "strings"
) )
@ -300,77 +299,6 @@ func declImports(gen *ast.GenDecl, path string) bool {
return false return false
} }
// RenameTop renames all references to the top-level name old.
// It returns true if it makes any changes.
func RenameTop(f *ast.File, old, new string) bool {
var fixed bool
// Rename any conflicting imports
// (assuming package name is last element of path).
for _, s := range f.Imports {
if s.Name != nil {
if s.Name.Name == old {
s.Name.Name = new
fixed = true
}
} else {
_, thisName := path.Split(importPath(s))
if thisName == old {
s.Name = ast.NewIdent(new)
fixed = true
}
}
}
// Rename any top-level declarations.
for _, d := range f.Decls {
switch d := d.(type) {
case *ast.FuncDecl:
if d.Recv == nil && d.Name.Name == old {
d.Name.Name = new
d.Name.Obj.Name = new
fixed = true
}
case *ast.GenDecl:
for _, s := range d.Specs {
switch s := s.(type) {
case *ast.TypeSpec:
if s.Name.Name == old {
s.Name.Name = new
s.Name.Obj.Name = new
fixed = true
}
case *ast.ValueSpec:
for _, n := range s.Names {
if n.Name == old {
n.Name = new
n.Obj.Name = new
fixed = true
}
}
}
}
}
}
// Rename top-level old to new, both unresolved names
// (probably defined in another file) and names that resolve
// to a declaration we renamed.
ast.Walk(visitFn(func(n ast.Node) {
id, ok := n.(*ast.Ident)
if ok && isTopName(id, old) {
id.Name = new
fixed = true
}
if ok && id.Obj != nil && id.Name == old && id.Obj.Name == new {
id.Name = id.Obj.Name
fixed = true
}
}), f)
return fixed
}
// matchLen returns the length of the longest prefix shared by x and y. // matchLen returns the length of the longest prefix shared by x and y.
func matchLen(x, y string) int { func matchLen(x, y string) int {
i := 0 i := 0

View File

@ -657,38 +657,6 @@ func TestRewriteImport(t *testing.T) {
} }
} }
var renameTests = []rewriteTest{
{
name: "rename pkg use",
srcPkg: "bytes",
dstPkg: "bytes_",
in: `package main
func f() []byte {
buf := new(bytes.Buffer)
return buf.Bytes()
}
`,
out: `package main
func f() []byte {
buf := new(bytes_.Buffer)
return buf.Bytes()
}
`,
},
}
func TestRenameTop(t *testing.T) {
for _, test := range renameTests {
file := parse(t, test.name, test.in)
RenameTop(file, test.srcPkg, test.dstPkg)
if got := print(t, test.name, file); got != test.out {
t.Errorf("%s:\ngot: %s\nwant: %s", test.name, got, test.out)
}
}
}
var importsTests = []struct { var importsTests = []struct {
name string name string
in string in string