From dcf508a4edb3f4fc13325d704b2fbc5c2505c22a Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 28 Oct 2014 12:19:23 -0400 Subject: [PATCH] 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 --- astutil/imports.go | 72 ----------------------------------------- astutil/imports_test.go | 32 ------------------ 2 files changed, 104 deletions(-) diff --git a/astutil/imports.go b/astutil/imports.go index 01a27557..27262d39 100644 --- a/astutil/imports.go +++ b/astutil/imports.go @@ -14,7 +14,6 @@ import ( "go/parser" "go/token" "log" - "path" "strconv" "strings" ) @@ -300,77 +299,6 @@ func declImports(gen *ast.GenDecl, path string) bool { 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. func matchLen(x, y string) int { i := 0 diff --git a/astutil/imports_test.go b/astutil/imports_test.go index 2a7166aa..3621972c 100644 --- a/astutil/imports_test.go +++ b/astutil/imports_test.go @@ -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 { name string in string