Skip to content

Add Kind assertions to findInArray and findKeyInMap#2651

Open
jandubois wants to merge 1 commit intomikefarah:masterfrom
jandubois:harden-node-kind-assertions
Open

Add Kind assertions to findInArray and findKeyInMap#2651
jandubois wants to merge 1 commit intomikefarah:masterfrom
jandubois:harden-node-kind-assertions

Conversation

@jandubois
Copy link
Copy Markdown
Contributor

@jandubois jandubois commented Apr 6, 2026

findInArray and findKeyInMap accept any *CandidateNode but produce wrong results when called on the wrong Kind: findInArray uses stride 1, correct for SequenceNodes but dangerous on MappingNodes (where key-value pairs live at even-odd indices); findKeyInMap uses stride 2, correct for MappingNodes but silently skips elements in SequenceNodes.

Commit b0ba958 fixed two call sites that passed MappingNodes to findInArray, but nothing prevents the same mistake from recurring. These assertions make both functions panic immediately on a Kind mismatch, catching misuse in tests rather than producing silent data corruption.

I was tempted to rename findInArray to findInSequence to match SequenceNode, but didn't want to engage in gratuitous refactoring. But still may be worth harmonizing the terminology.

findInArray and findKeyInMap accept any *CandidateNode but produce
wrong results when called on the wrong Kind: findInArray uses stride 1,
correct for SequenceNodes but dangerous on MappingNodes (where
key-value pairs live at even-odd indices); findKeyInMap uses stride 2,
correct for MappingNodes but silently skips elements in SequenceNodes.

Commit b0ba958 fixed two call sites that passed MappingNodes to
findInArray, but nothing prevents the same mistake from recurring.
These assertions make both functions panic immediately on a Kind
mismatch, catching misuse in tests rather than producing silent
data corruption.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant