MCPcopy Create free account
hub / github.com/decomp/decomp / deleteImport

Function deleteImport

cmd/go-post/fix.go:630–680  ·  view source on GitHub ↗

deleteImport deletes the import path from the file f, if present.

(f *ast.File, path string)

Source from the content-addressed store, hash-verified

628
629// deleteImport deletes the import path from the file f, if present.
630func deleteImport(f *ast.File, path string) (deleted bool) {
631 oldImport := importSpec(f, path)
632
633 // Find the import node that imports path, if any.
634 for i, decl := range f.Decls {
635 gen, ok := decl.(*ast.GenDecl)
636 if !ok || gen.Tok != token.IMPORT {
637 continue
638 }
639 for j, spec := range gen.Specs {
640 impspec := spec.(*ast.ImportSpec)
641 if oldImport != impspec {
642 continue
643 }
644
645 // We found an import spec that imports path.
646 // Delete it.
647 deleted = true
648 copy(gen.Specs[j:], gen.Specs[j+1:])
649 gen.Specs = gen.Specs[:len(gen.Specs)-1]
650
651 // If this was the last import spec in this decl,
652 // delete the decl, too.
653 if len(gen.Specs) == 0 {
654 copy(f.Decls[i:], f.Decls[i+1:])
655 f.Decls = f.Decls[:len(f.Decls)-1]
656 } else if len(gen.Specs) == 1 {
657 gen.Lparen = token.NoPos // drop parens
658 }
659 if j > 0 {
660 // We deleted an entry but now there will be
661 // a blank line-sized hole where the import was.
662 // Close the hole by making the previous
663 // import appear to "end" where this one did.
664 gen.Specs[j-1].(*ast.ImportSpec).EndPos = impspec.End()
665 }
666 break
667 }
668 }
669
670 // Delete it from f.Imports.
671 for i, imp := range f.Imports {
672 if imp == oldImport {
673 copy(f.Imports[i:], f.Imports[i+1:])
674 f.Imports = f.Imports[:len(f.Imports)-1]
675 break
676 }
677 }
678
679 return
680}
681
682// rewriteImport rewrites any import of path oldPath to path newPath.
683func rewriteImport(f *ast.File, oldPath, newPath string) (rewrote bool) {

Callers 3

deleteImportFnFunction · 0.85
addDelImportFnFunction · 0.85
renameFixTabFunction · 0.85

Calls 1

importSpecFunction · 0.85

Tested by 2

deleteImportFnFunction · 0.68
addDelImportFnFunction · 0.68