Skip to content

x/tools/go/astutil/inspector: add Cursor.ParentEdge{Kind,Index} convenience accessors #77195

@adonovan

Description

@adonovan

Update: see #77195 (comment) for latest.

Background: the inspector.Cursor.ParentEdge method returns both the kind and index of the edge to the cursor relative to its parent (e.g "c is the third argument to a function call"), since it is more efficient to compute both at once. However, it is common to need only the kind, resulting in statements like this one that discards the second component:

if ek, _ := cur.ParentEdge(); ek == edge.CallExpr_Args { ... }

Since these often appear as one clause in a long conjunction a && ... && z, we have an internal helper function IsChildOf:

if astutil.IsChildOf(cur, edge.SelectorExpr_Sel) { ... }

It is frequently used: there are currently 23 calls in x/tools.

Proposal: For convenience, we propose to publish it as a method of Cursor:

package inspector // golang.org/x/tools/go/ast/inspector

type Cursor ...

// IsChildOf(k) reports whether the first component of ParentEdge equals k.
func (Cursor) IsChildOf(edge.Kind) bool

Metadata

Metadata

Assignees

Labels

LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposalProposal-AcceptedToolsThis label describes issues relating to any tools in the x/tools repository.

Type

No type

Projects

Status

Accepted

Relationships

None yet

Development

No branches or pull requests

Issue actions