Skip to content

Commit cd7ebad

Browse files
committed
Add AncestorN helper
1 parent a780c23 commit cd7ebad

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

analysis/passes/jsobjectptr/jsobjectptr.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
3232
if !internal.Is_jsObject(pass, node) {
3333
return true
3434
}
35-
parent := stack[len(stack)-2]
35+
parent, ok := internal.AncestorN(stack, 1)
36+
if !ok {
37+
return true
38+
}
3639
switch pt := parent.(type) {
3740
case *ast.StarExpr:
3841
return true

internal/jsobject.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ func Is_jsObject(pass *analysis.Pass, node ast.Node) bool {
4343
}
4444
return pkg.Path() == "github.com/gopherjs/gopherjs/js"
4545
}
46+
47+
// AncestorN returns the nth ancestor from stack, and true, or nil and false if
48+
// it does not exist.
49+
func AncestorN(stack []ast.Node, n int) (ast.Node, bool) {
50+
l := len(stack)
51+
if n > l {
52+
return nil, false
53+
}
54+
return stack[l-(n+1)], true
55+
}

0 commit comments

Comments
 (0)