Skip to content

Commit

Permalink
Remove TS unique symbols for nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Dec 29, 2023
1 parent 828f31b commit 728e68b
Show file tree
Hide file tree
Showing 28 changed files with 52 additions and 98 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-ladybugs-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Remove TS unique symbols for nodes
12 changes: 5 additions & 7 deletions src/nodes/AccountDataNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import type { Node } from './Node';
import { StructTypeNode } from './typeNodes/StructTypeNode';

export type AccountDataNode = {
readonly __accountDataNode: unique symbol;
readonly kind: 'accountDataNode';
readonly name: MainCaseString;
readonly struct: StructTypeNode;
readonly link?: LinkTypeNode;
};

export type AccountDataNodeInput = Omit<
AccountDataNode,
'__accountDataNode' | 'kind' | 'name'
> & {
name: string;
export type AccountDataNodeInput = {
readonly name: string;
readonly struct: StructTypeNode;
readonly link?: LinkTypeNode;
};

export function accountDataNode(input: AccountDataNodeInput): AccountDataNode {
Expand All @@ -27,7 +25,7 @@ export function accountDataNode(input: AccountDataNodeInput): AccountDataNode {
name: mainCase(input.name),
struct: input.struct,
link: input.link,
} as AccountDataNode;
};
}

export function isAccountDataNode(node: Node | null): node is AccountDataNode {
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/AccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { TypeNode, createTypeNodeFromIdl } from './typeNodes/TypeNode';
import { vScalar } from './ValueNode';

export type AccountNode = {
readonly __accountNode: unique symbol;
readonly kind: 'accountNode';
readonly name: MainCaseString;
readonly data: AccountDataNode;
Expand All @@ -31,9 +30,9 @@ export type AccountNode = {

export type AccountNodeInput = Omit<
PartialExcept<AccountNode, 'name' | 'data'>,
'__accountNode' | 'kind' | 'name'
'kind' | 'name'
> & {
name: string;
readonly name: string;
};

export function accountNode(input: AccountNodeInput): AccountNode {
Expand All @@ -50,7 +49,7 @@ export function accountNode(input: AccountNodeInput): AccountNode {
size: input.size,
seeds: input.seeds ?? [],
discriminator: input.discriminator,
} as AccountNode;
};
}

export function accountNodeFromIdl(idl: Partial<IdlAccount>): AccountNode {
Expand Down
3 changes: 1 addition & 2 deletions src/nodes/DefinedTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Node } from './Node';
import { TypeNode, createTypeNodeFromIdl } from './typeNodes/TypeNode';

export type DefinedTypeNode = {
readonly __definedTypeNode: unique symbol;
readonly kind: 'definedTypeNode';
readonly name: MainCaseString;
readonly data: TypeNode;
Expand Down Expand Up @@ -32,7 +31,7 @@ export function definedTypeNode(input: DefinedTypeNodeInput): DefinedTypeNode {
idlName: input.idlName ?? input.name,
docs: input.docs ?? [],
internal: input.internal ?? false,
} as DefinedTypeNode;
};
}

export function definedTypeNodeFromIdl(
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/ErrorNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import type { Node } from './Node';

export type ErrorNode = {
readonly __errorNode: unique symbol;
readonly kind: 'errorNode';
readonly name: MainCaseString;
readonly idlName: string;
Expand All @@ -19,9 +18,9 @@ export type ErrorNode = {

export type ErrorNodeInput = Omit<
PartialExcept<ErrorNode, 'name' | 'code' | 'message'>,
'__errorNode' | 'kind' | 'name'
'kind' | 'name'
> & {
name: string;
readonly name: string;
};

export function errorNode(input: ErrorNodeInput): ErrorNode {
Expand All @@ -38,7 +37,7 @@ export function errorNode(input: ErrorNodeInput): ErrorNode {
code: input.code,
message: input.message,
docs: input.docs ?? [],
} as ErrorNode;
};
}

export function errorNodeFromIdl(idl: Partial<IdlError>): ErrorNode {
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/InstructionAccountNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import type { Node } from './Node';

export type InstructionAccountNode = {
readonly __instructionAccountNode: unique symbol;
readonly kind: 'instructionAccountNode';
readonly name: MainCaseString;
readonly isWritable: boolean;
Expand All @@ -20,9 +19,9 @@ export type InstructionAccountNode = {

export type InstructionAccountNodeInput = Omit<
PartialExcept<InstructionAccountNode, 'isWritable' | 'isSigner'>,
'__instructionAccountNode' | 'kind' | 'name'
'kind' | 'name'
> & {
name: string;
readonly name: string;
};

export function instructionAccountNode(
Expand All @@ -36,7 +35,7 @@ export function instructionAccountNode(
isOptional: input.isOptional ?? false,
docs: input.docs ?? [],
defaultsTo: input.defaultsTo,
} as InstructionAccountNode;
};
}

export function instructionAccountNodeFromIdl(
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/InstructionDataArgsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Node } from './Node';
import { StructTypeNode } from './typeNodes/StructTypeNode';

export type InstructionDataArgsNode = {
readonly __instructionDataArgsNode: unique symbol;
readonly kind: 'instructionDataArgsNode';
readonly name: MainCaseString;
readonly struct: StructTypeNode;
Expand All @@ -13,9 +12,9 @@ export type InstructionDataArgsNode = {

export type InstructionDataArgsNodeInput = Omit<
InstructionDataArgsNode,
'__instructionDataArgsNode' | 'kind' | 'name'
'kind' | 'name'
> & {
name: string;
readonly name: string;
};

export function instructionDataArgsNode(
Expand All @@ -31,7 +30,7 @@ export function instructionDataArgsNode(
name: mainCase(input.name),
struct: input.struct,
link: input.link,
} as InstructionDataArgsNode;
};
}

export function isInstructionDataArgsNode(
Expand Down
7 changes: 3 additions & 4 deletions src/nodes/InstructionExtraArgsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Node } from './Node';
import { StructTypeNode } from './typeNodes/StructTypeNode';

export type InstructionExtraArgsNode = {
readonly __instructionExtraArgsNode: unique symbol;
readonly kind: 'instructionExtraArgsNode';
readonly name: MainCaseString;
readonly struct: StructTypeNode;
Expand All @@ -13,9 +12,9 @@ export type InstructionExtraArgsNode = {

export type InstructionExtraArgsNodeInput = Omit<
InstructionExtraArgsNode,
'__instructionExtraArgsNode' | 'kind' | 'name'
'kind' | 'name'
> & {
name: string;
readonly name: string;
};

export function instructionExtraArgsNode(
Expand All @@ -31,7 +30,7 @@ export function instructionExtraArgsNode(
name: mainCase(input.name),
struct: input.struct,
link: input.link,
} as InstructionExtraArgsNode;
};
}

export function isInstructionExtraArgsNode(
Expand Down
9 changes: 4 additions & 5 deletions src/nodes/InstructionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { createTypeNodeFromIdl } from './typeNodes/TypeNode';
import { vScalar } from './ValueNode';

export type InstructionNode = {
readonly __instructionNode: unique symbol;
readonly kind: 'instructionNode';
readonly name: MainCaseString;
readonly accounts: InstructionAccountNode[];
Expand All @@ -50,10 +49,10 @@ export type InstructionNode = {

export type InstructionNodeInput = Omit<
PartialExcept<InstructionNode, 'accounts' | 'dataArgs'>,
'__instructionNode' | 'kind' | 'name' | 'argDefaults'
'kind' | 'name' | 'argDefaults'
> & {
name: string;
argDefaults?: Record<string, InstructionArgDefault>;
readonly name: string;
readonly argDefaults?: Record<string, InstructionArgDefault>;
};

export function instructionNode(input: InstructionNodeInput): InstructionNode {
Expand Down Expand Up @@ -85,7 +84,7 @@ export function instructionNode(input: InstructionNodeInput): InstructionNode {
])
),
optionalAccountStrategy: input.optionalAccountStrategy ?? 'programId',
} as InstructionNode;
};
}

export function instructionNodeFromIdl(
Expand Down
9 changes: 4 additions & 5 deletions src/nodes/ProgramNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { InstructionNode, instructionNodeFromIdl } from './InstructionNode';
import type { Node } from './Node';

export type ProgramNode = {
readonly __programNode: unique symbol;
readonly kind: 'programNode';
readonly accounts: AccountNode[];
readonly instructions: InstructionNode[];
Expand All @@ -31,10 +30,10 @@ export type ProgramNodeInput = Omit<
| 'publicKey'
| 'version'
>,
'__programNode' | 'kind' | 'name' | 'prefix'
'kind' | 'name' | 'prefix'
> & {
name: string;
prefix?: string;
readonly name: string;
readonly prefix?: string;
};

export function programNode(input: ProgramNodeInput): ProgramNode {
Expand All @@ -50,7 +49,7 @@ export function programNode(input: ProgramNodeInput): ProgramNode {
version: input.version,
origin: input.origin,
internal: input.internal ?? false,
} as ProgramNode;
};
}

export function programNodeFromIdl(idl: Partial<Idl>): ProgramNode {
Expand Down
3 changes: 1 addition & 2 deletions src/nodes/RootNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import { ProgramNode, programNodeFromIdl } from './ProgramNode';
export type IdlInputs = string | Partial<Idl> | (string | Partial<Idl>)[];

export type RootNode = {
readonly __rootNode: unique symbol;
readonly kind: 'rootNode';
readonly programs: ProgramNode[];
};

export function rootNode(programs: ProgramNode[]): RootNode {
return { kind: 'rootNode', programs } as RootNode;
return { kind: 'rootNode', programs };
}

export function rootNodeFromIdls(idls: IdlInputs): RootNode {
Expand Down
7 changes: 1 addition & 6 deletions src/nodes/typeNodes/ArrayTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { numberTypeNode } from './NumberTypeNode';
import { TypeNode, createTypeNodeFromIdl } from './TypeNode';

export type ArrayTypeNode = {
readonly __arrayTypeNode: unique symbol;
readonly kind: 'arrayTypeNode';
readonly child: TypeNode;
readonly size: SizeStrategy;
Expand All @@ -22,11 +21,7 @@ export function arrayTypeNode(
readonly size?: ArrayTypeNode['size'];
} = {}
): ArrayTypeNode {
return {
kind: 'arrayTypeNode',
child,
size: options.size ?? prefixedSize(),
} as ArrayTypeNode;
return { kind: 'arrayTypeNode', child, size: options.size ?? prefixedSize() };
}

export function arrayTypeNodeFromIdl(
Expand Down
6 changes: 1 addition & 5 deletions src/nodes/typeNodes/BoolTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import {
} from './NumberTypeNode';

export type BoolTypeNode = {
readonly __boolTypeNode: unique symbol;
readonly kind: 'boolTypeNode';
readonly size: NumberTypeNode;
};

export function boolTypeNode(size?: NumberTypeNode): BoolTypeNode {
return {
kind: 'boolTypeNode',
size: size ?? numberTypeNode('u8'),
} as BoolTypeNode;
return { kind: 'boolTypeNode', size: size ?? numberTypeNode('u8') };
}

export function displayBoolTypeNode(node: BoolTypeNode): string {
Expand Down
6 changes: 1 addition & 5 deletions src/nodes/typeNodes/BytesTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import {
import type { Node } from '../Node';

export type BytesTypeNode = {
readonly __bytesTypeNode: unique symbol;
readonly kind: 'bytesTypeNode';
readonly size: SizeStrategy;
};

export function bytesTypeNode(size?: SizeStrategy): BytesTypeNode {
return {
kind: 'bytesTypeNode',
size: size ?? remainderSize(),
} as BytesTypeNode;
return { kind: 'bytesTypeNode', size: size ?? remainderSize() };
}

export function displayBytesTypeNode(node: BytesTypeNode): string {
Expand Down
6 changes: 1 addition & 5 deletions src/nodes/typeNodes/EnumEmptyVariantTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../../shared';
import type { Node } from '../Node';

export type EnumEmptyVariantTypeNode = {
readonly __enumEmptyVariantTypeNode: unique symbol;
readonly kind: 'enumEmptyVariantTypeNode';
readonly name: MainCaseString;
};
Expand All @@ -16,10 +15,7 @@ export function enumEmptyVariantTypeNode(
'EnumEmptyVariantTypeNode must have a name.'
);
}
return {
kind: 'enumEmptyVariantTypeNode',
name: mainCase(name),
} as EnumEmptyVariantTypeNode;
return { kind: 'enumEmptyVariantTypeNode', name: mainCase(name) };
}

export function enumEmptyVariantTypeNodeFromIdl(
Expand Down
7 changes: 1 addition & 6 deletions src/nodes/typeNodes/EnumStructVariantTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Node } from '../Node';
import { StructTypeNode, structTypeNodeFromIdl } from './StructTypeNode';

export type EnumStructVariantTypeNode = {
readonly __enumStructVariantTypeNode: unique symbol;
readonly kind: 'enumStructVariantTypeNode';
readonly name: MainCaseString;
readonly struct: StructTypeNode;
Expand All @@ -19,11 +18,7 @@ export function enumStructVariantTypeNode(
'EnumStructVariantTypeNode must have a name.'
);
}
return {
kind: 'enumStructVariantTypeNode',
name: mainCase(name),
struct,
} as EnumStructVariantTypeNode;
return { kind: 'enumStructVariantTypeNode', name: mainCase(name), struct };
}

export function enumStructVariantTypeNodeFromIdl(
Expand Down
7 changes: 1 addition & 6 deletions src/nodes/typeNodes/EnumTupleVariantTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Node } from '../Node';
import { TupleTypeNode, tupleTypeNodeFromIdl } from './TupleTypeNode';

export type EnumTupleVariantTypeNode = {
readonly __enumTupleVariantTypeNode: unique symbol;
readonly kind: 'enumTupleVariantTypeNode';
readonly name: MainCaseString;
readonly tuple: TupleTypeNode;
Expand All @@ -19,11 +18,7 @@ export function enumTupleVariantTypeNode(
'EnumTupleVariantTypeNode must have a name.'
);
}
return {
kind: 'enumTupleVariantTypeNode',
name: mainCase(name),
tuple,
} as EnumTupleVariantTypeNode;
return { kind: 'enumTupleVariantTypeNode', name: mainCase(name), tuple };
}

export function enumTupleVariantTypeNodeFromIdl(
Expand Down
Loading

0 comments on commit 728e68b

Please sign in to comment.