Skip to content

Commit 985c282

Browse files
committed
go/ast/inspector: add commentary about Cursor API
It's too bad we added Inspector.PreorderSeq and All just before the Cursor API made them redundant. Change-Id: I7ad53e9a9863f4ff7336a3aee3a1583c5c1b5e69 Reviewed-on: https://go-review.googlesource.com/c/tools/+/733181 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com>
1 parent 0540962 commit 985c282

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

go/ast/inspector/inspector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type event struct {
8787
// Type can be recovered from the sole bit in typ.
8888
// [Tried this, wasn't faster. --adonovan]
8989

90-
// Preorder visits all the nodes of the files supplied to New in
90+
// Preorder visits all the nodes of the files supplied to [New] in
9191
// depth-first order. It calls f(n) for each node n before it visits
9292
// n's children.
9393
//
@@ -133,7 +133,7 @@ func (in *Inspector) Preorder(types []ast.Node, f func(ast.Node)) {
133133
}
134134
}
135135

136-
// Nodes visits the nodes of the files supplied to New in depth-first
136+
// Nodes visits the nodes of the files supplied to [New] in depth-first
137137
// order. It calls f(n, true) for each node n before it visits n's
138138
// children. If f returns true, Nodes invokes f recursively for each
139139
// of the non-nil children of the node, followed by a call of

go/ast/inspector/iter.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,31 @@ import (
1212
)
1313

1414
// PreorderSeq returns an iterator that visits all the
15-
// nodes of the files supplied to New in depth-first order.
15+
// nodes of the files supplied to [New] in depth-first order.
1616
// It visits each node n before n's children.
1717
// The complete traversal sequence is determined by ast.Inspect.
1818
//
19-
// The types argument, if non-empty, enables type-based
20-
// filtering of events: only nodes whose type matches an
21-
// element of the types slice are included in the sequence.
19+
// The types argument, if non-empty, enables type-based filtering:
20+
// only nodes whose type matches an element of the types slice are
21+
// included in the sequence.
22+
//
23+
// Example:
24+
//
25+
// for call := range in.PreorderSeq((*ast.CallExpr)(nil)) { ... }
26+
//
27+
// The [All] function is more convenient if there is exactly one node type:
28+
//
29+
// for call := range All[*ast.CallExpr](in) { ... }
30+
//
31+
// See also the newer and more flexible [Cursor] API, which lets you
32+
// start the traversal at an arbitrary node, and reports each matching
33+
// node by its Cursor, enabling easier navigation.
34+
// The above example would be written thus:
35+
//
36+
// for curCall := range in.Root().Preorder((*ast.CallExpr)(nil)) {
37+
// call := curCall.Node().(*ast.CallExpr)
38+
// ...
39+
// }
2240
func (in *Inspector) PreorderSeq(types ...ast.Node) iter.Seq[ast.Node] {
2341

2442
// This implementation is identical to Preorder,
@@ -53,6 +71,16 @@ func (in *Inspector) PreorderSeq(types ...ast.Node) iter.Seq[ast.Node] {
5371
// Example:
5472
//
5573
// for call := range All[*ast.CallExpr](in) { ... }
74+
//
75+
// See also the newer and more flexible [Cursor] API, which lets you
76+
// start the traversal at an arbitrary node, and reports each matching
77+
// node by its Cursor, enabling easier navigation.
78+
// The above example would be written thus:
79+
//
80+
// for curCall := range in.Root().Preorder((*ast.CallExpr)(nil)) {
81+
// call := curCall.Node().(*ast.CallExpr)
82+
// ...
83+
// }
5684
func All[N interface {
5785
*S
5886
ast.Node

0 commit comments

Comments
 (0)