Skip to content

proposal: x/tools/go/ast/astutil: PathContainingPos #74367

Closed as not planned
Closed as not planned
@cszczepaniak

Description

@cszczepaniak

Proposal Details

I sometimes find myself writing static analysis tools which load packages and iterate something other than the syntax (like pkg.TypesInfo.Defs) to do some analysis. Sometimes in the course of such analysis, it's useful to map some position (perhaps from a types.Object) to its path in the AST.

The best way I've found to do this is to first find the containing *ast.File by iterating the fileset and checking the position in question against the file's Pos() and End(), then subsequently calling astutil.PathEnclosingInterval to find the path within the file.

I propose the addition of a function that combines these two operations to make this more ergonomic:

// PathContainingPos returns the node that encloses the source
// position and all its ancestors up to the AST root.
func PathContainingPos(fset *token.FileSet, pos token.Position) []ast.Node

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions