Skip to content

Commit

Permalink
Remove internal booleans from nodes (#133)
Browse files Browse the repository at this point in the history
This is specific to the JS renderers, not the Kinobi tree itself.
  • Loading branch information
lorisleiva authored Jan 3, 2024
1 parent a318698 commit 9432c41
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 171 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-rings-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': minor
---

Remove internal booleans from nodes
2 changes: 0 additions & 2 deletions src/nodes/AccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type AccountNode = {
readonly name: MainCaseString;
readonly idlName: string;
readonly docs: string[];
readonly internal: boolean;
readonly size?: number | null;
};

Expand All @@ -43,7 +42,6 @@ export function accountNode(input: AccountNodeInput): AccountNode {
name: mainCase(input.name),
idlName: input.idlName ?? input.name,
docs: input.docs ?? [],
internal: input.internal ?? false,
size: input.size,
discriminator: input.discriminator,
};
Expand Down
3 changes: 0 additions & 3 deletions src/nodes/DefinedTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ export type DefinedTypeNode = {
readonly name: MainCaseString;
readonly idlName: string;
readonly docs: string[];
readonly internal: boolean;
};

export type DefinedTypeNodeInput = {
readonly name: string;
readonly type: TypeNode;
readonly idlName?: string;
readonly docs?: string[];
readonly internal?: boolean;
};

export function definedTypeNode(input: DefinedTypeNodeInput): DefinedTypeNode {
Expand All @@ -33,7 +31,6 @@ export function definedTypeNode(input: DefinedTypeNodeInput): DefinedTypeNode {
type: input.type,
idlName: input.idlName ?? input.name,
docs: input.docs ?? [],
internal: input.internal ?? false,
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/nodes/InstructionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export type InstructionNode = {
readonly name: MainCaseString;
readonly idlName: string;
readonly docs: string[];
readonly internal: boolean;
readonly optionalAccountStrategy: 'omitted' | 'programId';
};

Expand All @@ -63,7 +62,6 @@ export function instructionNode(input: InstructionNodeInput): InstructionNode {
subInstructions: input.subInstructions,
idlName: input.idlName ?? input.name,
docs: input.docs ?? [],
internal: input.internal ?? false,
bytesCreatedOnChain: input.bytesCreatedOnChain,
remainingAccounts: input.remainingAccounts,
optionalAccountStrategy: input.optionalAccountStrategy ?? 'programId',
Expand Down
2 changes: 0 additions & 2 deletions src/nodes/ProgramNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export type ProgramNode = {
readonly publicKey: string;
readonly version: string;
readonly origin?: 'shank' | 'anchor';
readonly internal: boolean;
};

export type ProgramNodeInput = Omit<
Expand All @@ -46,7 +45,6 @@ export function programNode(input: ProgramNodeInput): ProgramNode {
publicKey: input.publicKey,
version: input.version ?? '',
origin: input.origin,
internal: input.internal ?? false,
};
}

Expand Down
25 changes: 9 additions & 16 deletions src/renderers/js-experimental/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export type GetRenderMapOptions = {
asyncResolvers?: string[];
nameTransformers?: Partial<NameTransformers>;
nonScalarEnums?: string[];
internalNodes?: string[];
customAccountData?: CustomDataOptions[];
customInstructionData?: CustomDataOptions[];
};
Expand Down Expand Up @@ -122,6 +123,7 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
const dependencyMap = options.dependencyMap ?? {};
const asyncResolvers = (options.asyncResolvers ?? []).map(mainCase);
const nonScalarEnums = (options.nonScalarEnums ?? []).map(mainCase);
const internalNodes = (options.internalNodes ?? []).map(mainCase);
const customAccountData = parseCustomDataOptions(
options.customAccountData ?? [],
'AccountData'
Expand Down Expand Up @@ -191,21 +193,20 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
(v) =>
extendVisitor(v, {
visitRoot(node, { self }) {
const programsToExport = node.programs.filter((p) => !p.internal);
const isNotInternal = (n: { name: MainCaseString }) =>
!internalNodes.includes(n.name);
const programsToExport = node.programs.filter(isNotInternal);
const programsWithErrorsToExport = programsToExport.filter(
(p) => p.errors.length > 0
);
const pdasToExport = getAllPdas(node);
const accountsToExport = getAllAccounts(node).filter(
(a) => !a.internal
);
const accountsToExport = getAllAccounts(node).filter(isNotInternal);
const instructionsToExport = getAllInstructionsWithSubs(
node,
!renderParentInstructions
).filter((i) => !i.internal);
const definedTypesToExport = getAllDefinedTypes(node).filter(
(t) => !t.internal
);
).filter(isNotInternal);
const definedTypesToExport =
getAllDefinedTypes(node).filter(isNotInternal);
const hasAnythingToExport =
programsToExport.length > 0 ||
accountsToExport.length > 0 ||
Expand Down Expand Up @@ -270,14 +271,6 @@ export function getRenderMapVisitor(options: GetRenderMapOptions = {}) {
.mergeWith(...node.definedTypes.map((t) => visit(t, self)))
.mergeWith(...customDataDefinedType.map((t) => visit(t, self)));

// Internal programs are support programs that
// were added to fill missing types or accounts.
// They don't need to render anything else.
if (node.internal) {
program = null;
return renderMap;
}

if (node.errors.length > 0) {
const programErrorsFragment = getProgramErrorsFragment({
programNode: node,
Expand Down
30 changes: 11 additions & 19 deletions src/renderers/js/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
LinkableDictionary,
logWarn,
mainCase,
MainCaseString,
pascalCase,
pipe,
RenderMap,
Expand Down Expand Up @@ -66,6 +67,7 @@ export type GetJavaScriptRenderMapOptions = {
prettierOptions?: PrettierOptions;
dependencyMap?: Record<ImportFrom, string>;
nonScalarEnums?: string[];
internalNodes?: string[];
customAccountData?: CustomDataOptions[];
customInstructionData?: CustomDataOptions[];
};
Expand Down Expand Up @@ -98,6 +100,7 @@ export function getRenderMapVisitor(
generatedTypes: '../types',
};
const nonScalarEnums = (options.nonScalarEnums ?? []).map(mainCase);
const internalNodes = (options.internalNodes ?? []).map(mainCase);
const customAccountData = parseCustomDataOptions(
options.customAccountData ?? [],
'AccountData'
Expand Down Expand Up @@ -176,17 +179,16 @@ export function getRenderMapVisitor(
(v) =>
extendVisitor(v, {
visitRoot(node, { self }) {
const programsToExport = node.programs.filter((p) => !p.internal);
const accountsToExport = getAllAccounts(node).filter(
(a) => !a.internal
);
const isNotInternal = (n: { name: MainCaseString }) =>
!internalNodes.includes(n.name);
const programsToExport = node.programs.filter(isNotInternal);
const accountsToExport = getAllAccounts(node).filter(isNotInternal);
const instructionsToExport = getAllInstructionsWithSubs(
node,
!renderParentInstructions
).filter((i) => !i.internal);
const definedTypesToExport = getAllDefinedTypes(node).filter(
(t) => !t.internal
);
).filter(isNotInternal);
const definedTypesToExport =
getAllDefinedTypes(node).filter(isNotInternal);
const hasAnythingToExport =
programsToExport.length > 0 ||
accountsToExport.length > 0 ||
Expand Down Expand Up @@ -242,17 +244,7 @@ export function getRenderMapVisitor(
const renderMap = new RenderMap()
.mergeWith(...node.accounts.map((a) => visit(a, self)))
.mergeWith(...node.definedTypes.map((t) => visit(t, self)))
.mergeWith(...customDataDefinedType.map((t) => visit(t, self)));

// Internal programs are support programs that
// were added to fill missing types or accounts.
// They don't need to render anything else.
if (node.internal) {
program = null;
return renderMap;
}

renderMap
.mergeWith(...customDataDefinedType.map((t) => visit(t, self)))
.mergeWith(
...getAllInstructionsWithSubs(
node,
Expand Down
56 changes: 25 additions & 31 deletions src/renderers/js/getValidatorBagVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ export function getValidatorBagVisitor(): Visitor<ValidatorBag> {
[`get${pascalCaseName}GpaBuilder`]: 'function',
[`get${pascalCaseName}Size`]: 'function',
};
if (!node.internal) {
bag.mergeWith([checkExportConflicts(node, exports)]);
}
bag.mergeWith([checkExportConflicts(node, exports)]);

const reservedAccountFields = new Set(['publicKey', 'header']);
const invalidFields = node.data.fields
Expand All @@ -128,41 +126,37 @@ export function getValidatorBagVisitor(): Visitor<ValidatorBag> {
const pascalCaseName = pascalCase(node.name);
const pascalCaseData = `${pascalCaseName}InstructionData`;
const pascalCaseExtra = `${pascalCaseName}InstructionExtra`;
if (!node.internal) {
bag.mergeWith([
checkExportConflicts(node, {
[camelCaseName]: 'function',
[`${pascalCaseName}InstructionAccounts`]: 'type',
[`${pascalCaseName}InstructionArgs`]: 'type',
[`${pascalCaseData}`]: 'type',
[`${pascalCaseData}Args`]: 'type',
[`get${pascalCaseData}Serializer`]: 'function',
[`${pascalCaseExtra}Args`]: 'type',
}),
]);
}
bag.mergeWith([
checkExportConflicts(node, {
[camelCaseName]: 'function',
[`${pascalCaseName}InstructionAccounts`]: 'type',
[`${pascalCaseName}InstructionArgs`]: 'type',
[`${pascalCaseData}`]: 'type',
[`${pascalCaseData}Args`]: 'type',
[`get${pascalCaseData}Serializer`]: 'function',
[`${pascalCaseExtra}Args`]: 'type',
}),
]);
return bag.mergeWith([next(node)]);
},

visitDefinedType(node, { next }) {
const bag = new ValidatorBag();
const camelCaseName = camelCase(node.name);
const pascalCaseName = pascalCase(node.name);
if (!node.internal) {
bag.mergeWith([
checkExportConflicts(node, {
[pascalCaseName]: 'type',
[`${pascalCaseName}Args`]: 'type',
[`fetch${pascalCaseName}`]: 'function',
...(isNode(node.type, 'enumTypeNode') && isDataEnum(node.type)
? {
[camelCaseName]: 'function',
[`is${pascalCaseName}`]: 'function',
}
: {}),
}),
]);
}
bag.mergeWith([
checkExportConflicts(node, {
[pascalCaseName]: 'type',
[`${pascalCaseName}Args`]: 'type',
[`fetch${pascalCaseName}`]: 'function',
...(isNode(node.type, 'enumTypeNode') && isDataEnum(node.type)
? {
[camelCaseName]: 'function',
[`is${pascalCaseName}`]: 'function',
}
: {}),
}),
]);
return bag.mergeWith([next(node)]);
},

Expand Down
32 changes: 10 additions & 22 deletions src/renderers/rust/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,13 @@ export function getRenderMapVisitor(options: GetRustRenderMapOptions = {}) {
(v) =>
extendVisitor(v, {
visitRoot(node, { self }) {
const programsToExport = node.programs.filter((p) => !p.internal);
const accountsToExport = getAllAccounts(node).filter(
(a) => !a.internal
);
const programsToExport = node.programs;
const accountsToExport = getAllAccounts(node);
const instructionsToExport = getAllInstructionsWithSubs(
node,
!renderParentInstructions
).filter((i) => !i.internal);
const definedTypesToExport = getAllDefinedTypes(node).filter(
(t) => !t.internal
);
const definedTypesToExport = getAllDefinedTypes(node);
const hasAnythingToExport =
programsToExport.length > 0 ||
accountsToExport.length > 0 ||
Expand Down Expand Up @@ -108,21 +104,13 @@ export function getRenderMapVisitor(options: GetRustRenderMapOptions = {}) {
program = node;
const renderMap = new RenderMap()
.mergeWith(...node.accounts.map((account) => visit(account, self)))
.mergeWith(...node.definedTypes.map((type) => visit(type, self)));

// Internal programs are support programs that
// were added to fill missing types or accounts.
// They don't need to render anything else.
if (node.internal) {
program = null;
return renderMap;
}

renderMap.mergeWith(
...getAllInstructionsWithSubs(node, !renderParentInstructions).map(
(ix) => visit(ix, self)
)
);
.mergeWith(...node.definedTypes.map((type) => visit(type, self)))
.mergeWith(
...getAllInstructionsWithSubs(
node,
!renderParentInstructions
).map((ix) => visit(ix, self))
);

// Errors.
if (node.errors.length > 0) {
Expand Down
1 change: 0 additions & 1 deletion src/visitors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export * from './setAnchorDiscriminatorsVisitor';
export * from './setFixedAccountSizesVisitor';
export * from './setInstructionAccountDefaultValuesVisitor';
export * from './setInstructionDiscriminatorsVisitor';
export * from './setMissingDefinedTypesVisitor';
export * from './setNumberWrappersVisitor';
export * from './setStructDefaultValuesVisitor';
export * from './singleNodeVisitor';
Expand Down
Loading

0 comments on commit 9432c41

Please sign in to comment.