codama-idl / codama

Generate clients, CLIs, documentation and more from your Solana programs
MIT License
73 stars 16 forks source link

Add `interceptFirstVisitVisitor` helper #172

Closed lorisleiva closed 1 month ago

lorisleiva commented 1 month ago

This PR adds a new visitor primitive called interceptFirstVisitVisitor.

It works the same way as the interceptVisitor primitive but only intercepts the first visit of a node. This means that the provided function is called when visiting the specific node provided but not when visiting its children. The parameters are the same as for the interceptVisitor.

For instance, the following visitor intercepts a voidVisitor and captures events only during the first visit.

const events: string[] = [];
const visitor = interceptFirstVisitVisitor(voidVisitor(), (node, next) => {
    events.push(`down:${node.kind}`);
    next(node);
    events.push(`up:${node.kind}`);
});

visit(tupleTypeNode([numberTypeNode('u32'), publicKeyTypeNode()]), visitor);
// events === [
//     'down:tupleTypeNode',
//     'up:tupleTypeNode',
// ]
changeset-bot[bot] commented 1 month ago

🦋 Changeset detected

Latest commit: fe76592d15d4d062b64e7c96c4edb427c8502a52

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages | Name | Type | | ---------------------------- | ----- | | @kinobi-so/visitors-core | Patch | | @kinobi-so/renderers-core | Patch | | @kinobi-so/renderers-js-umi | Patch | | @kinobi-so/renderers-js | Patch | | @kinobi-so/renderers-rust | Patch | | @kinobi-so/validators | Patch | | @kinobi-so/visitors | Patch | | @kinobi-so/renderers | Patch | | kinobi | Patch | | @kinobi-so/nodes-from-anchor | Patch | | @kinobi-so/errors | Patch | | @kinobi-so/node-types | Patch | | @kinobi-so/nodes | Patch |

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