Skip to content

Commit

Permalink
Fix panic when capture has no next sibling
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-klotho committed Feb 1, 2023
1 parent 547a801 commit 0332a1f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 1 addition & 5 deletions pkg/lang/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,15 @@ func (c *capabilityFinder) findAllCommentsBlocks(f *core.SourceFile) []*commentB
comment := capture.Content()
comment = c.preprocessor(comment)

combineWithNext := capture.NextSibling() != nil && capture.NextNamedSibling().Type() == capture.Type()
node := capture.NextNamedSibling()
if node == nil {
continue // this is the last node in the AST, so it's effectively a break :)
}
if combineWithPrevious {
prevBlock := blocks[len(blocks)-1]
prevBlock.comment = prevBlock.comment + "\n" + comment
prevBlock.node = node // The previous "node" was just this capture, so we want to basically push it forward
} else {
blocks = append(blocks, &commentBlock{comment: comment, node: node})
}
combineWithPrevious = combineWithNext
combineWithPrevious = node != nil && node.Type() == capture.Type()
}
return blocks
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/lang/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,22 @@ const y = 456`,
},
{"comment is last node",
`// only line in source`,
[]FindAllCommentBlocksExpected{},
[]FindAllCommentBlocksExpected{
{
"only line in source",
"",
},
},
},
{"multi-line comment is last node",
`// first line
// second line`,
[]FindAllCommentBlocksExpected{
{
"first line\nsecond line",
"",
},
},
},
}
for _, tt := range cases {
Expand Down
7 changes: 6 additions & 1 deletion pkg/lang/capabilities_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ func FindAllCommentBlocksForTest(language core.SourceLanguage, source string) ([
blocks := capFinder.findAllCommentsBlocks(f)
found := []FindAllCommentBlocksExpected{}
for _, block := range blocks {
content := ""
if block.node != nil {
content = block.node.Content()
}
found = append(found, FindAllCommentBlocksExpected{
Comment: block.comment,
Node: block.node.Content()})
Node: content,
})
}
return found, nil

Expand Down

0 comments on commit 0332a1f

Please sign in to comment.