@codama/renderers-js 1.5.5 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -216,6 +216,7 @@ var DEFAULT_NAME_TRANSFORMERS = {
216
216
  programInstructionsEnum: (name) => `${nodes.pascalCase(name)}Instruction`,
217
217
  programInstructionsEnumVariant: (name) => `${nodes.pascalCase(name)}`,
218
218
  programInstructionsIdentifierFunction: (name) => `identify${nodes.pascalCase(name)}Instruction`,
219
+ programInstructionsParseFunction: (name) => `parse${nodes.pascalCase(name)}Instruction`,
219
220
  programInstructionsParsedUnionType: (name) => `Parsed${nodes.pascalCase(name)}Instruction`,
220
221
  programIsErrorFunction: (name) => `is${nodes.pascalCase(name)}Error`,
221
222
  resolverFunction: (name) => `${nodes.camelCase(name)}`
@@ -2113,7 +2114,8 @@ function getProgramInstructionsFragment(scope) {
2113
2114
  [
2114
2115
  getProgramInstructionsEnumFragment(scopeWithInstructions),
2115
2116
  getProgramInstructionsIdentifierFunctionFragment(scopeWithInstructions),
2116
- getProgramInstructionsParsedUnionTypeFragment(scopeWithInstructions)
2117
+ getProgramInstructionsParsedUnionTypeFragment(scopeWithInstructions),
2118
+ getProgramInstructionsParseFunctionFragment(scopeWithInstructions)
2117
2119
  ],
2118
2120
  (c) => c.join("\n\n")
2119
2121
  );
@@ -2182,6 +2184,39 @@ function getProgramInstructionsParsedUnionTypeFragment(scope) {
2182
2184
  (c) => c.join("\n")
2183
2185
  );
2184
2186
  }
2187
+ function getProgramInstructionsParseFunctionFragment(scope) {
2188
+ const { programNode, nameApi, allInstructions } = scope;
2189
+ const instructionsWithDiscriminators = allInstructions.filter(
2190
+ (instruction) => (instruction.discriminators ?? []).length > 0
2191
+ );
2192
+ if (instructionsWithDiscriminators.length === 0) return;
2193
+ const programInstructionsEnum = nameApi.programInstructionsEnum(programNode.name);
2194
+ const programInstructionsIdentifierFunction = nameApi.programInstructionsIdentifierFunction(programNode.name);
2195
+ const programInstructionsParsedUnionType = nameApi.programInstructionsParsedUnionType(programNode.name);
2196
+ const parseFunction = nameApi.programInstructionsParseFunction(programNode.name);
2197
+ const switchCases = mergeFragments(
2198
+ allInstructions.map((instruction) => {
2199
+ const enumVariant = nameApi.programInstructionsEnumVariant(instruction.name);
2200
+ const parseFunction2 = use(nameApi.instructionParseFunction(instruction.name), "generatedInstructions");
2201
+ const assertIsInstructionWithAccounts = use("assertIsInstructionWithAccounts", "solanaInstructions");
2202
+ const hasAccounts = instruction.accounts.length > 0;
2203
+ const assertionsCode = hasAccounts ? fragment`${assertIsInstructionWithAccounts}(instruction);\n` : fragment``;
2204
+ return fragment`case ${programInstructionsEnum}.${enumVariant}: { ${assertionsCode}return { instructionType: ${programInstructionsEnum}.${enumVariant}, ...${parseFunction2}(instruction) }; }`;
2205
+ }),
2206
+ (c) => c.join("\n")
2207
+ );
2208
+ return fragment`
2209
+ export function ${parseFunction}<TProgram extends string>(
2210
+ instruction: ${use("type Instruction", "solanaInstructions")}<TProgram>
2211
+ & ${use("type InstructionWithData", "solanaInstructions")}<${use("type ReadonlyUint8Array", "solanaCodecsCore")}>
2212
+ ): ${programInstructionsParsedUnionType}<TProgram> {
2213
+ const instructionType = ${programInstructionsIdentifierFunction}(instruction);
2214
+ switch (instructionType) {
2215
+ ${switchCases}
2216
+ default: throw new Error(\`Unrecognized instruction type: \${instructionType}\`);
2217
+ }
2218
+ }`;
2219
+ }
2185
2220
 
2186
2221
  // src/fragments/programPage.ts
2187
2222
  function getProgramPageFragment(scope) {
@@ -3230,6 +3265,9 @@ function renderVisitor(path, options = {}) {
3230
3265
  const formatCode = await getCodeFormatter(options);
3231
3266
  renderMap = await renderersCore.mapRenderMapContentAsync(renderMap, formatCode);
3232
3267
  await syncPackageJson(renderMap, formatCode, options);
3268
+ if (options.formatCode ?? true) {
3269
+ renderMap = renderersCore.addToRenderMap(renderMap, ".prettierignore", fragment`**\n`);
3270
+ }
3233
3271
  renderersCore.writeRenderMap(renderMap, path);
3234
3272
  });
3235
3273
  }