Repository navigation
[2.x] Check the transforms of types that stop a recursion - #1286
Conversation
🦋 Changeset detectedLatest commit: a5a39cb The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
trevor-cortex
left a comment
There was a problem hiding this comment.
Summary
getHasFiniteValueVisitor previously short-circuited to true on the nodes that can stop a recursion (options, non-fixed collections, unresolvable links, enums) without looking at anything but their item/variants. This PR keeps that logic for the item but additionally visits the parts of those nodes that are present in every value: count, size, prefix, zeroValue and transforms. A constant whose type links back to the defined type being visited (e.g. hiddenPrefixTransformNode([constantValueNode(link(self), …)])) now correctly yields false. Unresolvable links go through next(node) instead of returning true so their transforms are checked too. Nine new negative test cases, one per newly inspected slot, plus a patch changeset for @codama/visitors-core.
Verification notes
I traced the mechanics rather than trusting the description:
extendVisitorpassesself: this, sovisit(node.count, self)etc. go through the outerrecordNodeStackVisitorand links inside counts/transforms resolve with the right path. Same mechanism the existing struct-field tests already rely on.- The generated
mergeVisitor.visitDefinedTypeLinkvisitsprogram,transformsandplugins, sonext(node)on a link does what the comment says. For the resolved case,stack.visitPath(linkedPath, self) && next(node)runsnextwith the stack back at the link node, which is correct. sentinelCountNode→sentinel→constantValueNode→typereaches the self link;prefixedCountNode→prefixandoptionTypeNode.prefix/enumTypeNode.sizeare number types whose owntransformsare traversed by the merge visitor, so those slots are covered without special-casing.- Short-circuit order is item-first in every override, so the fixed-array/map paths bail before touching counts/transforms when the item already fails. No double visits: the overrides replace the merge visitor's traversal entirely for those kinds.
- Changeset:
patchon a lockstep core package for a bug fix, entry starts with a verb — matches CONTRIBUTING.md.
Nits (non-blocking)
- Doc comment, ~L20–21 (unchanged lines): "Links that cannot be resolved count as finite" is now only true of the link itself — an unresolvable link with a self-constant in its transforms returns
false(and there's a test for exactly that). Something like "count as finite on their own" would keep the sentence accurate. - See inline: two cheap extra test cases would cover the
prefix/size/prefixedCountslots the changeset explicitly lists, which currently only get exercised indirectly.
Logic is correct and the tests pin every new branch. LGTM.
| ['transforms of links', field(definedTypeLinkNode('missing', { transforms: [prefixOfSelf] }))], | ||
| ['transforms of options', field(optionTypeNode(self, { prefix: u8, transforms: [prefixOfSelf] }))], | ||
| ['zero values of zeroable options', field(zeroableOptionTypeNode(u8, { zeroValue: constantOfSelf }))], | ||
| ['transforms of remainder options', field(remainderOptionTypeNode(u8, { transforms: [prefixOfSelf] }))], | ||
| ['sentinel counts', arrayTypeNode(u8, sentinelCountNode(constantOfSelf))], | ||
| ['transforms of prefixed arrays', arrayTypeNode(u8, prefixedCountNode(u8), { transforms: [prefixOfSelf] })], | ||
| ['transforms of sets', setTypeNode(u8, remainderCountNode(), { transforms: [prefixOfSelf] })], | ||
| ['transforms of maps', mapTypeNode(u8, u8, prefixedCountNode(u8), { transforms: [prefixOfSelf] })], | ||
| ['transforms of enums', enumTypeNode([enumVariantTypeNode('nil')], { transforms: [prefixOfSelf] })], |
There was a problem hiding this comment.
These cover every transforms, the zeroValue and the sentinel count, but not the three remaining slots the changeset calls out — the option prefix, the enum size, and a prefixedCountNode's prefix. They all reduce to "a number type whose transforms carry a self constant", so one shared helper would do, e.g.:
const u8OfSelf = integerTypeNode('u8', { transforms: [prefixOfSelf] });
// …
['prefixes of options', field(optionTypeNode(u8, { prefix: u8OfSelf }))],
['sizes of enums', enumTypeNode([enumVariantTypeNode('nil')], { size: u8OfSelf })],
['prefixed counts', arrayTypeNode(u8, prefixedCountNode(u8OfSelf))],Not blocking — I traced the merge visitor and these already pass through visitIntegerType → transforms — but it would make the test list match the contract in the doc comment one-to-one and guard the visit(node.prefix|size|count, self) calls against a future refactor dropping one.
There was a problem hiding this comment.
Added all three, with the shared u8OfSelf helper. Dropping the prefix or size visit now fails its case. Also narrowed the doc sentence to "count as finite on their own".
This PR makes
getHasFiniteValueVisitorcheck every part of a type that may stop a recursion, not just its item, so a constant of the type itself hidden in a link's or an option's transforms, a sentinel count or a zero value is no longer mistaken for a finite value. Found while porting the visitor to codama-rs (codama-idl/codama-rs#190).