Skip to content

Commit 629ec5e

Browse files
fix: add nil guards for GetToken() in findKeyPos and findKeyAtLine (#338)
1 parent 3dd2183 commit 629ec5e

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

parser.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,12 @@ func findKeyPos(node ast.Node, parts []string) (int, int) {
466466
switch n := node.(type) {
467467
case *ast.MappingNode:
468468
for _, mv := range n.Values {
469-
keyVal := mv.Key.GetToken().Value
469+
tok := mv.Key.GetToken()
470+
if tok == nil {
471+
continue
472+
}
473+
474+
keyVal := tok.Value
470475

471476
if keyVal != basePart {
472477
continue
@@ -560,6 +565,9 @@ func findKeyAtLine(node ast.Node, targetLine int, prefix string) string {
560565
}
561566
case *ast.MappingValueNode:
562567
keyTok := n.Key.GetToken()
568+
if keyTok == nil {
569+
return ""
570+
}
563571

564572
var fullKey string
565573

@@ -569,7 +577,7 @@ func findKeyAtLine(node ast.Node, targetLine int, prefix string) string {
569577
fullKey = prefix + "." + keyTok.Value
570578
}
571579

572-
if keyTok.Position.Line == targetLine {
580+
if keyTok.Position == nil || keyTok.Position.Line == targetLine {
573581
return fullKey
574582
}
575583

@@ -580,7 +588,7 @@ func findKeyAtLine(node ast.Node, targetLine int, prefix string) string {
580588
for i, entry := range n.Values {
581589
seqKey := fmt.Sprintf("%s[%d]", prefix, i)
582590

583-
if entry.GetToken().Position.Line == targetLine {
591+
if tok := entry.GetToken(); tok != nil && tok.Position != nil && tok.Position.Line == targetLine {
584592
return seqKey
585593
}
586594

0 commit comments

Comments
 (0)