| 1 |
// Copyright 2026 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package astutil |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
|
| 10 |
"golang.org/x/tools/go/ast/edge" |
| 11 |
"golang.org/x/tools/go/ast/inspector" |
| 12 |
) |
| 13 |
|
| 14 |
// UnparenCursor returns the cursor for an expression with any |
| 15 |
// enclosing parentheses removed, similar to [ast.Unparen]. |
| 16 |
// It is often prudent to call this before switching on the |
| 17 |
// type of cur.Node(). |
| 18 |
// |
| 19 |
// See also [UnparenEnclosingCursor]. |
| 20 |
func UnparenCursor(cur inspector.Cursor) inspector.Cursor { |
| 21 |
for is[*ast.ParenExpr](cur) { |
| 22 |
cur, _ = cur.FirstChild() |
| 23 |
} |
| 24 |
return cur |
| 25 |
} |
| 26 |
|
| 27 |
// UnparenEnclosingCursor returns the first element of |
| 28 |
// the [Cursor.Enclosing] sequence that is not itself enclosed |
| 29 |
// in parens. It is often prudent to call this before switching on |
| 30 |
// cur.ParentEdge(). |
| 31 |
// |
| 32 |
// See also [UnparenCursor]. |
| 33 |
func UnparenEnclosingCursor(cur inspector.Cursor) inspector.Cursor { |
| 34 |
for cur.ParentEdgeKind() == edge.ParenExpr_X { |
| 35 |
cur = cur.Parent() |
| 36 |
} |
| 37 |
return cur |
| 38 |
} |