Package astutil contains common utilities for working with the Go AST.
Internal call graph ▹
Internal call graph ▾
In the call graph viewer below, each node is a function belonging to this package and its children are the functions it calls—perhaps dynamically.
The root nodes are the entry points of the package: functions that may be called from outside the package. There may be non-exported or anonymous functions among them if they are called dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring
Functions may be omitted if they were determined to be unreachable in the particular programs or tests that were analyzed.
func AddImport(fset *token.FileSet, f *ast.File, ipath string) (added bool)
AddImport adds the import path to the file f, if absent.
func AddNamedImport ¶
func AddNamedImport(fset *token.FileSet, f *ast.File, name, ipath string) (added bool)
AddNamedImport adds the import path to the file f, if absent. If name is not empty, it is used to rename the import.
For example, calling
AddNamedImport(fset, f, "pathpkg", "path")
import pathpkg "path"
func DeleteImport ¶
func DeleteImport(fset *token.FileSet, f *ast.File, path string) (deleted bool)
DeleteImport deletes the import path from the file f, if present.
func DeleteNamedImport ¶
func DeleteNamedImport(fset *token.FileSet, f *ast.File, name, path string) (deleted bool)
DeleteNamedImport deletes the import with the given name and path from the file f, if present.
func Imports(fset *token.FileSet, f *ast.File) *ast.ImportSpec
Imports returns the file imports grouped by paragraph.
func NodeDescription ¶
func NodeDescription(n ast.Node) string
NodeDescription returns a description of the concrete type of n suitable for a user interface.
TODO(adonovan): in some cases (e.g. Field, FieldList, Ident, StarExpr) we could be much more specific given the path to the AST root. Perhaps we should do that.
func PathEnclosingInterval ¶
func PathEnclosingInterval(root *ast.File, start, end token.Pos) (path ast.Node, exact bool)
PathEnclosingInterval returns the node that encloses the source interval [start, end), and all its ancestors up to the AST root.
The definition of "enclosing" used by this function considers additional whitespace abutting a node to be enclosed by it. In this example:
z := x + y // add them <-A-> <----B----->
the ast.BinaryExpr(+) node is considered to enclose interval B even though its [Pos()..End()) is actually only interval A. This behaviour makes user interfaces more tolerant of imperfect input.
This function treats tokens as nodes, though they are not included in the result. e.g. PathEnclosingInterval("+") returns the enclosing ast.BinaryExpr("x + y").
If start==end, the 1-char interval following start is used instead.
The 'exact' result is true if the interval contains only path and perhaps some adjacent whitespace. It is false if the interval overlaps multiple children of path, or if it contains only interior whitespace of path. In this example:
z := x + y // add them <--C--> <---E--> ^ D
intervals C, D and E are inexact. C is contained by the z-assignment statement, because it spans three of its children (:=, x, +). So too is the 1-char interval D, because it contains only interior whitespace of the assignment. E is considered interior whitespace of the BlockStmt containing the assignment.
Precondition: [start, end) both lie within the same file as root. TODO(adonovan): return (nil, false) in this case and remove precond. Requires FileSet; see loader.tokenFileContainsPos.
Postcondition: path is never nil; it always contains at least 'root'.
func RewriteImport ¶
func RewriteImport(fset *token.FileSet, f *ast.File, oldPath, newPath string) (rewrote bool)
RewriteImport rewrites any import of path oldPath to path newPath.
func Unparen(e ast.Expr) ast.Expr
Unparen returns e with any enclosing parentheses stripped.
func UsesImport ¶
func UsesImport(f *ast.File, path string) (used bool)
UsesImport reports whether a given import is used.