Skip to content

Commit 1f9f614

Browse files
authored
Fix size computation of RemainderOptionTypeNode (#813)
* Fix size computation of `RemainderOptionTypeNode` * Add changeset
1 parent ea7f5d6 commit 1f9f614

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

.changeset/long-trees-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codama/visitors-core': patch
3+
---
4+
5+
Fix size computation of the `RemainderOptionTypeNode`. This node should always be of variable size unless the item it wraps is of size 0.

packages/visitors-core/src/getByteSizeVisitor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ export function getByteSizeVisitor(
141141
return 32;
142142
},
143143

144+
visitRemainderOptionType(node, { self }) {
145+
const itemSize = visit(node.item, self);
146+
return itemSize === 0 ? 0 : null;
147+
},
148+
144149
visitSetType(node, { self }) {
145150
if (!isNode(node.count, 'fixedCountNode')) return null;
146151
const count = node.count.value;

packages/visitors-core/test/getByteSizeVisitor.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
programNode,
1818
publicKeyTypeNode,
1919
remainderCountNode,
20+
remainderOptionTypeNode,
2021
rootNode,
2122
setTypeNode,
2223
stringTypeNode,
@@ -135,6 +136,11 @@ test('it returns null for array-like nodes with `RemainderCountNodes`', () => {
135136
expectSize(mapTypeNode(numberTypeNode('u16'), numberTypeNode('u8'), remainderCountNode()), null);
136137
});
137138

139+
test('it returns null with visiting remainder option types', () => {
140+
expectSize(remainderOptionTypeNode(stringTypeNode('utf8')), null);
141+
expectSize(remainderOptionTypeNode(numberTypeNode('u16')), null);
142+
});
143+
138144
test('it follows linked nodes using the correct paths', () => {
139145
// Given two link nodes designed so that the path would
140146
// fail if we did not save and restored linked paths.

0 commit comments

Comments
 (0)