Repository navigation
[2.x] Add withPath and visitPath helpers to NodeStack #1186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lorisleiva
merged 1 commit into
main
from
09-28-add_withpath_and_visitpath_helpers_to_nodestack
Sep 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { definedTypeNode, integerTypeNode, programNode, publicKeyTypeNode, rootNode } from '@codama/nodes'; | ||
| import { expect, test } from 'vitest'; | ||
|
|
||
| import { NodeStack, pipe, recordNodeStackVisitor, tapVisitor, voidVisitor } from '../src'; | ||
|
|
||
| const slot = definedTypeNode({ identifier: 'slot', type: integerTypeNode('u64') }); | ||
| const owner = definedTypeNode({ identifier: 'owner', type: publicKeyTypeNode() }); | ||
| const root = rootNode(programNode({ definedTypes: [slot, owner], identifier: 'myProgram', publicKey: '1111' })); | ||
|
|
||
| test('withPath runs the callback with the given path and restores the previous one', () => { | ||
| // Given a stack within the owner type. | ||
| const stack = new NodeStack([root, root.program, owner]); | ||
|
|
||
| // When we run a callback with the path of the slot type. | ||
| const result = stack.withPath([root, root.program, slot], () => stack.getPath()); | ||
|
|
||
| // Then the callback sees that path, and the previous path is restored. | ||
| expect(result).toStrictEqual([root, root.program, slot]); | ||
| expect(stack.getPath()).toStrictEqual([root, root.program, owner]); | ||
| }); | ||
|
|
||
| test('withPath restores the previous path when the callback throws', () => { | ||
| const stack = new NodeStack([root, root.program, owner]); | ||
| expect(() => | ||
| stack.withPath([root, root.program, slot], () => { | ||
| throw new Error('boom'); | ||
| }), | ||
| ).toThrow('boom'); | ||
| expect(stack.getPath()).toStrictEqual([root, root.program, owner]); | ||
| }); | ||
|
|
||
| test('visitPath visits the last node of the path with the path recorded once', () => { | ||
| // Given a stack within the owner type and a visitor recording the paths of integer types. | ||
| const stack = new NodeStack([root, root.program, owner]); | ||
| const paths: unknown[] = []; | ||
| const visitor = pipe( | ||
| voidVisitor(), | ||
| v => tapVisitor(v, 'integerTypeNode', () => paths.push(stack.getPath())), | ||
| v => recordNodeStackVisitor(v, stack), | ||
| ); | ||
|
|
||
| // When we visit the path of the slot type. | ||
| stack.visitPath([root, root.program, slot], visitor); | ||
|
|
||
| // Then its type is visited with the slot type recorded once, rather than twice. | ||
| expect(paths).toStrictEqual([[root, root.program, slot, slot.type]]); | ||
|
|
||
| // And the previous path is restored. | ||
| expect(stack.getPath()).toStrictEqual([root, root.program, owner]); | ||
| }); | ||
|
|
||
| test('visitPath restores the previous path when the visit throws', () => { | ||
| const stack = new NodeStack([root, root.program, owner]); | ||
| const visitor = pipe( | ||
| voidVisitor(), | ||
| v => recordNodeStackVisitor(v, stack), | ||
| v => | ||
| tapVisitor(v, 'integerTypeNode', () => { | ||
| throw new Error('boom'); | ||
| }), | ||
| ); | ||
| expect(() => stack.visitPath([root, root.program, slot], visitor)).toThrow('boom'); | ||
| expect(stack.getPath()).toStrictEqual([root, root.program, owner]); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.