Skip to content

Commit

Permalink
fixup! Render a union type for parsed instructions in js-experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
mcintyre94 committed Feb 28, 2024
1 parent 8ba6e5f commit a4052c4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-bats-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Render a union type for parsed instructions in js-experimental
1 change: 1 addition & 0 deletions src/renderers/js-experimental/ImportMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const DEFAULT_MODULE_MAP: Record<string, string> = {
generatedAccounts: '../accounts',
generatedErrors: '../errors',
generatedTypes: '../types',
generatedInstructions: '../instructions',
};

export class ImportMap {
Expand Down
12 changes: 5 additions & 7 deletions src/renderers/js-experimental/fragments/programInstructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ProgramNode,
structTypeNodeFromInstructionArgumentNodes,
} from '../../../nodes';
import { ImportMap } from '../ImportMap';
import type { GlobalFragmentScope } from '../getRenderMapVisitor';
import { Fragment, fragment, mergeFragments } from './common';
import { getDiscriminatorConditionFragment } from './discriminatorCondition';
Expand All @@ -30,7 +29,7 @@ export function getProgramInstructionsFragment(
[
getProgramInstructionsEnumFragment(scopeWithInstructions),
getProgramInstructionsIdentifierFunctionFragment(scopeWithInstructions),
getProgramUnionParsedInstructionTypeFragment(scopeWithInstructions),
getProgramInstructionsParsedUnionTypeFragment(scopeWithInstructions),
],
(r) => `${r.join('\n\n')}\n`
);
Expand Down Expand Up @@ -106,15 +105,15 @@ function getProgramInstructionsIdentifierFunctionFragment(
);
}

function getProgramUnionParsedInstructionTypeFragment(
function getProgramInstructionsParsedUnionTypeFragment(
scope: Pick<GlobalFragmentScope, 'nameApi'> & {
programNode: ProgramNode;
allInstructions: InstructionNode[];
}
): Fragment {
const { programNode, allInstructions, nameApi } = scope;

const programInstructionsType = nameApi.programInstructionsTypeUnion(
const programInstructionsType = nameApi.programInstructionsParsedUnionType(
programNode.name
);

Expand All @@ -132,9 +131,8 @@ function getProgramUnionParsedInstructionTypeFragment(
);

return fragment(
`| {instructionType: ${programInstructionsEnum}.${instructionEnumVariant}} & ${parsedInstructionType}`,
new ImportMap().add('../instructions', parsedInstructionType)
);
`| {instructionType: ${programInstructionsEnum}.${instructionEnumVariant}} & ${parsedInstructionType}`
).addImports('generatedInstructions', parsedInstructionType);
});

return mergeFragments(
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/js-experimental/nameTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export type NameTransformerKey =
| 'programInstructionsEnum'
| 'programInstructionsEnumVariant'
| 'programInstructionsIdentifierFunction'
| 'programInstructionsTypeUnion'
| 'programInstructionsParsedUnionType'
| 'programErrorClass'
| 'programErrorCodeEnum'
| 'programErrorCodeMap'
Expand Down Expand Up @@ -148,7 +148,7 @@ export const DEFAULT_NAME_TRANSFORMERS: NameTransformers = {
programInstructionsEnumVariant: (name) => `${pascalCase(name)}`,
programInstructionsIdentifierFunction: (name) =>
`identify${pascalCase(name)}Instruction`,
programInstructionsTypeUnion: (name) =>
programInstructionsParsedUnionType: (name) =>
`Parsed${pascalCase(name)}Instruction`,
programErrorClass: (name) => `${pascalCase(name)}ProgramError`,
programErrorCodeEnum: (name) => `${pascalCase(name)}ProgramErrorCode`,
Expand Down
24 changes: 24 additions & 0 deletions test/renderers/js-experimental/programsPage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,27 @@ test('it checks the discriminator of sub-instructions before their parents.', (t
`if (memcmp(data, getU8Encoder().encode(1), 0)) { return SplTokenInstruction.MintTokens; }`,
]);
});

test('it renders a parsed union type of all available instructions for a program', (t) => {
// Given the following program.
const node = programNode({
name: 'splToken',
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
instructions: [
instructionNode({ name: 'mintTokens' }),
instructionNode({ name: 'transferTokens' }),
instructionNode({ name: 'updateAuthority' }),
],
});

// When we render it.
const renderMap = visit(node, getRenderMapVisitor());

// Then we expect the following program instruction enum.
renderMapContains(t, renderMap, 'programs/splToken.ts', [
'export type ParsedSplTokenInstruction=',
'|({instructionType: SplTokenInstruction.MintTokens;} & ParsedMintTokensInstruction)',
'|({instructionType: SplTokenInstruction.TransferTokens;} & ParsedTransferTokensInstruction)',
'|({instructionType: SplTokenInstruction.UpdateAuthority;} & ParsedUpdateAuthorityInstruction)',
]);
});

0 comments on commit a4052c4

Please sign in to comment.