Skip to content

Commit 28f3d38

Browse files
authored
Fix TS bug on accept and update Kinobi function (#181)
1 parent f753676 commit 28f3d38

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

.changeset/two-swans-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'kinobi': patch
3+
---
4+
5+
Fix TS bug on `accept` and `update` Kinobi function

packages/library/src/kinobi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import { assertIsNode, KinobiVersion, Node, RootNode } from '@kinobi-so/nodes';
44
import { visit, Visitor } from '@kinobi-so/visitors';
55

66
export interface Kinobi {
7-
accept<T>(visitor: Visitor<T>): T;
7+
accept<T>(visitor: Visitor<T, 'rootNode'>): T;
88
clone(): Kinobi;
99
getJson(): string;
1010
getRoot(): RootNode;
11-
update(visitor: Visitor<Node | null>): void;
11+
update(visitor: Visitor<Node | null, 'rootNode'>): void;
1212
}
1313

1414
export function createFromRoot(root: RootNode): Kinobi {
1515
let currentRoot = root;
1616
validateKinobiVersion(currentRoot.version);
1717
return {
18-
accept<T>(visitor: Visitor<T>): T {
18+
accept<T>(visitor: Visitor<T, 'rootNode'>): T {
1919
return visit(currentRoot, visitor);
2020
},
2121
clone(): Kinobi {
@@ -27,7 +27,7 @@ export function createFromRoot(root: RootNode): Kinobi {
2727
getRoot(): RootNode {
2828
return currentRoot;
2929
},
30-
update(visitor: Visitor<Node | null>): void {
30+
update(visitor: Visitor<Node | null, 'rootNode'>): void {
3131
const newRoot = visit(currentRoot, visitor);
3232
assertIsNode(newRoot, 'rootNode');
3333
currentRoot = newRoot;
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from 'vitest';
22

3-
import { identityVisitor, rootNode } from '../src';
3+
import { createFromRoot, identityVisitor, programNode, rootNode, rootNodeVisitor, voidVisitor } from '../src';
44

55
test('it exports node helpers', () => {
66
expect(typeof rootNode).toBe('function');
@@ -9,3 +9,17 @@ test('it exports node helpers', () => {
99
test('it exports visitors', () => {
1010
expect(typeof identityVisitor).toBe('function');
1111
});
12+
13+
test('it accepts visitors', () => {
14+
const kinobi = createFromRoot(rootNode(programNode({ name: 'myProgram', publicKey: '1111' })));
15+
const visitor = voidVisitor(['rootNode']);
16+
const result = kinobi.accept(visitor) satisfies void;
17+
expect(typeof result).toBe('undefined');
18+
});
19+
20+
test('it updates the root node returned by visitors', () => {
21+
const kinobi = createFromRoot(rootNode(programNode({ name: 'myProgram', publicKey: '1111' })));
22+
const visitor = rootNodeVisitor(node => rootNode(programNode({ ...node.program, name: 'myTransformedProgram' })));
23+
kinobi.update(visitor) satisfies void;
24+
expect(kinobi.getRoot()).toEqual(rootNode(programNode({ name: 'myTransformedProgram', publicKey: '1111' })));
25+
});

0 commit comments

Comments
 (0)