Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Fix panic when capture has no next sibling #171

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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