renameTop renames all references to the top-level name old. It returns true if it makes any changes.
(f *ast.File, old, new string)
| 465 | // renameTop renames all references to the top-level name old. |
| 466 | // It returns true if it makes any changes. |
| 467 | func renameTop(f *ast.File, old, new string) bool { |
| 468 | var fixed bool |
| 469 | |
| 470 | // Rename any conflicting imports |
| 471 | // (assuming package name is last element of path). |
| 472 | for _, s := range f.Imports { |
| 473 | if s.Name != nil { |
| 474 | if s.Name.Name == old { |
| 475 | s.Name.Name = new |
| 476 | fixed = true |
| 477 | } |
| 478 | } else { |
| 479 | _, thisName := path.Split(importPath(s)) |
| 480 | if thisName == old { |
| 481 | s.Name = ast.NewIdent(new) |
| 482 | fixed = true |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Rename any top-level declarations. |
| 488 | for _, d := range f.Decls { |
| 489 | switch d := d.(type) { |
| 490 | case *ast.FuncDecl: |
| 491 | if d.Recv == nil && d.Name.Name == old { |
| 492 | d.Name.Name = new |
| 493 | d.Name.Obj.Name = new |
| 494 | fixed = true |
| 495 | } |
| 496 | case *ast.GenDecl: |
| 497 | for _, s := range d.Specs { |
| 498 | switch s := s.(type) { |
| 499 | case *ast.TypeSpec: |
| 500 | if s.Name.Name == old { |
| 501 | s.Name.Name = new |
| 502 | s.Name.Obj.Name = new |
| 503 | fixed = true |
| 504 | } |
| 505 | case *ast.ValueSpec: |
| 506 | for _, n := range s.Names { |
| 507 | if n.Name == old { |
| 508 | n.Name = new |
| 509 | n.Obj.Name = new |
| 510 | fixed = true |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // Rename top-level old to new, both unresolved names |
| 519 | // (probably defined in another file) and names that resolve |
| 520 | // to a declaration we renamed. |
| 521 | walk(f, func(n interface{}) { |
| 522 | id, ok := n.(*ast.Ident) |
| 523 | if ok && isTopName(id, old) { |
| 524 | id.Name = new |
no test coverage detected