@macalinao/codama-rename-visitor 0.1.0 → 0.2.0

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.
Files changed (36) hide show
  1. package/README.md +24 -1
  2. package/dist/index.d.ts +5 -97
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +4 -220
  5. package/dist/index.js.map +1 -1
  6. package/dist/rename-accounts-visitor.d.ts +26 -0
  7. package/dist/rename-accounts-visitor.d.ts.map +1 -0
  8. package/dist/rename-accounts-visitor.js +43 -0
  9. package/dist/rename-accounts-visitor.js.map +1 -0
  10. package/dist/rename-defined-types-visitor.d.ts +27 -0
  11. package/dist/rename-defined-types-visitor.d.ts.map +1 -0
  12. package/dist/rename-defined-types-visitor.js +46 -0
  13. package/dist/rename-defined-types-visitor.js.map +1 -0
  14. package/dist/rename-instructions-visitor.d.ts +26 -0
  15. package/dist/rename-instructions-visitor.d.ts.map +1 -0
  16. package/dist/rename-instructions-visitor.js +43 -0
  17. package/dist/rename-instructions-visitor.js.map +1 -0
  18. package/dist/rename-visitor.d.ts +35 -0
  19. package/dist/rename-visitor.d.ts.map +1 -0
  20. package/dist/rename-visitor.js +75 -0
  21. package/dist/rename-visitor.js.map +1 -0
  22. package/dist/types.d.ts +12 -0
  23. package/dist/types.d.ts.map +1 -0
  24. package/dist/types.js +2 -0
  25. package/dist/types.js.map +1 -0
  26. package/package.json +7 -5
  27. package/src/index.ts +14 -291
  28. package/src/rename-accounts-visitor.ts +50 -0
  29. package/src/rename-defined-types-visitor.test.ts +44 -0
  30. package/src/rename-defined-types-visitor.ts +59 -0
  31. package/src/rename-instructions-visitor.test.ts +83 -0
  32. package/src/rename-instructions-visitor.ts +50 -0
  33. package/src/rename-visitor.test.ts +175 -0
  34. package/src/rename-visitor.ts +94 -0
  35. package/src/types.ts +11 -0
  36. package/src/index.test.ts +0 -409
@@ -0,0 +1,35 @@
1
+ import { rootNodeVisitor } from "codama";
2
+ import type { ProgramRenameOptions } from "./types.js";
3
+ /**
4
+ * Creates a visitor that renames accounts, instructions, and defined types in specific programs.
5
+ * This follows the same pattern as addPdasVisitor from Codama.
6
+ *
7
+ * @param renamesByProgram - Object mapping program names to their rename configurations
8
+ * @returns A root node visitor that performs all specified renames
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const visitor = renameVisitor({
13
+ * quarryMine: {
14
+ * instructions: {
15
+ * claimRewards: "claimRewardsMine"
16
+ * },
17
+ * accounts: {
18
+ * miner: "minerAccount"
19
+ * }
20
+ * },
21
+ * token: {
22
+ * instructions: {
23
+ * transfer: "transferTokens",
24
+ * mint: "mintNft"
25
+ * },
26
+ * definedTypes: {
27
+ * tokenData: "tokenMetadata"
28
+ * }
29
+ * }
30
+ * });
31
+ * codama.update(visitor);
32
+ * ```
33
+ */
34
+ export declare function renameVisitor(renamesByProgram: Record<string, ProgramRenameOptions>): ReturnType<typeof rootNodeVisitor>;
35
+ //# sourceMappingURL=rename-visitor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rename-visitor.d.ts","sourceRoot":"","sources":["../src/rename-visitor.ts"],"names":[],"mappings":"AACA,OAAO,EAA8B,eAAe,EAAS,MAAM,QAAQ,CAAC;AAI5E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,aAAa,CAC3B,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,oBAAoB,CAAC,GACrD,UAAU,CAAC,OAAO,eAAe,CAAC,CAqDpC"}
@@ -0,0 +1,75 @@
1
+ import { bottomUpTransformerVisitor, rootNodeVisitor, visit } from "codama";
2
+ import { renameAccountTransform } from "./rename-accounts-visitor.js";
3
+ import { renameDefinedTypeTransform } from "./rename-defined-types-visitor.js";
4
+ import { renameInstructionTransform } from "./rename-instructions-visitor.js";
5
+ /**
6
+ * Creates a visitor that renames accounts, instructions, and defined types in specific programs.
7
+ * This follows the same pattern as addPdasVisitor from Codama.
8
+ *
9
+ * @param renamesByProgram - Object mapping program names to their rename configurations
10
+ * @returns A root node visitor that performs all specified renames
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const visitor = renameVisitor({
15
+ * quarryMine: {
16
+ * instructions: {
17
+ * claimRewards: "claimRewardsMine"
18
+ * },
19
+ * accounts: {
20
+ * miner: "minerAccount"
21
+ * }
22
+ * },
23
+ * token: {
24
+ * instructions: {
25
+ * transfer: "transferTokens",
26
+ * mint: "mintNft"
27
+ * },
28
+ * definedTypes: {
29
+ * tokenData: "tokenMetadata"
30
+ * }
31
+ * }
32
+ * });
33
+ * codama.update(visitor);
34
+ * ```
35
+ */
36
+ export function renameVisitor(renamesByProgram) {
37
+ return rootNodeVisitor((root) => {
38
+ const transforms = [];
39
+ // Process each program's rename configuration
40
+ Object.entries(renamesByProgram).forEach(([programName, renameOptions]) => {
41
+ // Add instruction renames for this program
42
+ if (renameOptions.instructions) {
43
+ Object.entries(renameOptions.instructions).forEach(([oldName, newName]) => {
44
+ transforms.push({
45
+ select: `[programNode]${programName}.[instructionNode]${oldName}`,
46
+ transform: (node) => renameInstructionTransform(node, { [oldName]: newName }),
47
+ });
48
+ });
49
+ }
50
+ if (renameOptions.accounts) {
51
+ Object.entries(renameOptions.accounts).forEach(([oldName, newName]) => {
52
+ transforms.push({
53
+ select: `[programNode]${programName}.[accountNode]${oldName}`,
54
+ transform: (node) => renameAccountTransform(node, { [oldName]: newName }),
55
+ });
56
+ });
57
+ }
58
+ // Add defined type renames for this program
59
+ if (renameOptions.definedTypes) {
60
+ Object.entries(renameOptions.definedTypes ?? {}).forEach(([oldName, newName]) => {
61
+ transforms.push({
62
+ select: `[programNode]${programName}.[definedTypeNode]${oldName}`,
63
+ transform: (node) => renameDefinedTypeTransform(node, { [oldName]: newName }),
64
+ });
65
+ });
66
+ }
67
+ });
68
+ if (transforms.length === 0) {
69
+ return root;
70
+ }
71
+ const visitor = bottomUpTransformerVisitor(transforms);
72
+ return visit(root, visitor);
73
+ });
74
+ }
75
+ //# sourceMappingURL=rename-visitor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rename-visitor.js","sourceRoot":"","sources":["../src/rename-visitor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,0BAA0B,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,0BAA0B,EAAE,MAAM,kCAAkC,CAAC;AAG9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,aAAa,CAC3B,gBAAsD;IAEtD,OAAO,eAAe,CAAC,CAAC,IAAI,EAAE,EAAE;QAC9B,MAAM,UAAU,GAGV,EAAE,CAAC;QAET,8CAA8C;QAC9C,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,EAAE,aAAa,CAAC,EAAE,EAAE;YACxE,2CAA2C;YAC3C,IAAI,aAAa,CAAC,YAAY,EAAE,CAAC;gBAC/B,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,OAAO,CAChD,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE;oBACrB,UAAU,CAAC,IAAI,CAAC;wBACd,MAAM,EAAE,gBAAgB,WAAW,qBAAqB,OAAO,EAAE;wBACjE,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE,CAClB,0BAA0B,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC;qBAC3D,CAAC,CAAC;gBACL,CAAC,CACF,CAAC;YACJ,CAAC;YAED,IAAI,aAAa,CAAC,QAAQ,EAAE,CAAC;gBAC3B,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE;oBACpE,UAAU,CAAC,IAAI,CAAC;wBACd,MAAM,EAAE,gBAAgB,WAAW,iBAAiB,OAAO,EAAE;wBAC7D,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE,CAClB,sBAAsB,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC;qBACvD,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;YACL,CAAC;YAED,4CAA4C;YAC5C,IAAI,aAAa,CAAC,YAAY,EAAE,CAAC;gBAC/B,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC,OAAO,CACtD,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE;oBACrB,UAAU,CAAC,IAAI,CAAC;wBACd,MAAM,EAAE,gBAAgB,WAAW,qBAAqB,OAAO,EAAE;wBACjE,SAAS,EAAE,CAAC,IAAI,EAAE,EAAE,CAClB,0BAA0B,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC;qBAC3D,CAAC,CAAC;gBACL,CAAC,CACF,CAAC;YACJ,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC5B,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,0BAA0B,CAAC,UAAU,CAAC,CAAC;QACvD,OAAO,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { Node } from \"codama\";\nimport { bottomUpTransformerVisitor, rootNodeVisitor, visit } from \"codama\";\nimport { renameAccountTransform } from \"./rename-accounts-visitor.js\";\nimport { renameDefinedTypeTransform } from \"./rename-defined-types-visitor.js\";\nimport { renameInstructionTransform } from \"./rename-instructions-visitor.js\";\nimport type { ProgramRenameOptions } from \"./types.js\";\n\n/**\n * Creates a visitor that renames accounts, instructions, and defined types in specific programs.\n * This follows the same pattern as addPdasVisitor from Codama.\n *\n * @param renamesByProgram - Object mapping program names to their rename configurations\n * @returns A root node visitor that performs all specified renames\n *\n * @example\n * ```typescript\n * const visitor = renameVisitor({\n * quarryMine: {\n * instructions: {\n * claimRewards: \"claimRewardsMine\"\n * },\n * accounts: {\n * miner: \"minerAccount\"\n * }\n * },\n * token: {\n * instructions: {\n * transfer: \"transferTokens\",\n * mint: \"mintNft\"\n * },\n * definedTypes: {\n * tokenData: \"tokenMetadata\"\n * }\n * }\n * });\n * codama.update(visitor);\n * ```\n */\nexport function renameVisitor(\n renamesByProgram: Record<string, ProgramRenameOptions>,\n): ReturnType<typeof rootNodeVisitor> {\n return rootNodeVisitor((root) => {\n const transforms: {\n select: string;\n transform: (node: Node) => Node | null;\n }[] = [];\n\n // Process each program's rename configuration\n Object.entries(renamesByProgram).forEach(([programName, renameOptions]) => {\n // Add instruction renames for this program\n if (renameOptions.instructions) {\n Object.entries(renameOptions.instructions).forEach(\n ([oldName, newName]) => {\n transforms.push({\n select: `[programNode]${programName}.[instructionNode]${oldName}`,\n transform: (node) =>\n renameInstructionTransform(node, { [oldName]: newName }),\n });\n },\n );\n }\n\n if (renameOptions.accounts) {\n Object.entries(renameOptions.accounts).forEach(([oldName, newName]) => {\n transforms.push({\n select: `[programNode]${programName}.[accountNode]${oldName}`,\n transform: (node) =>\n renameAccountTransform(node, { [oldName]: newName }),\n });\n });\n }\n\n // Add defined type renames for this program\n if (renameOptions.definedTypes) {\n Object.entries(renameOptions.definedTypes ?? {}).forEach(\n ([oldName, newName]) => {\n transforms.push({\n select: `[programNode]${programName}.[definedTypeNode]${oldName}`,\n transform: (node) =>\n renameDefinedTypeTransform(node, { [oldName]: newName }),\n });\n },\n );\n }\n });\n\n if (transforms.length === 0) {\n return root;\n }\n\n const visitor = bottomUpTransformerVisitor(transforms);\n return visit(root, visitor);\n });\n}\n"]}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Rename mapping for a single program
3
+ */
4
+ export interface ProgramRenameOptions {
5
+ /** Mapping of old account names to new account names */
6
+ accounts?: Record<string, string>;
7
+ /** Mapping of old instruction names to new instruction names */
8
+ instructions?: Record<string, string>;
9
+ /** Mapping of old defined type names to new defined type names */
10
+ definedTypes?: Record<string, string>;
11
+ }
12
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,wDAAwD;IACxD,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,gEAAgE;IAChE,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,kEAAkE;IAClE,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACvC"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Rename mapping for a single program\n */\nexport interface ProgramRenameOptions {\n /** Mapping of old account names to new account names */\n accounts?: Record<string, string>;\n /** Mapping of old instruction names to new instruction names */\n instructions?: Record<string, string>;\n /** Mapping of old defined type names to new defined type names */\n definedTypes?: Record<string, string>;\n}\n"]}
package/package.json CHANGED
@@ -1,20 +1,22 @@
1
1
  {
2
2
  "name": "@macalinao/codama-rename-visitor",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Codama visitor for renaming instructions and events within a program",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "author": "Ian Macalinao <me@ianm.com>",
8
8
  "homepage": "https://github.com/macalinao/coda",
9
- "license": "MIT",
9
+ "license": "Apache-2.0",
10
10
  "keywords": [
11
11
  "codama",
12
+ "coda",
12
13
  "solana",
13
14
  "anchor",
14
15
  "idl",
15
16
  "rename",
16
17
  "visitor",
17
- "typescript"
18
+ "typescript",
19
+ "ian-macalinao"
18
20
  ],
19
21
  "main": "dist/index.js",
20
22
  "types": "dist/index.d.ts",
@@ -40,10 +42,10 @@
40
42
  "build": "tsc",
41
43
  "clean": "rm -fr dist/",
42
44
  "lint": "eslint . --cache",
43
- "test": "bun test"
45
+ "test": "bun test src/"
44
46
  },
45
47
  "dependencies": {
46
- "codama": "*"
48
+ "codama": "^1.3.3"
47
49
  },
48
50
  "devDependencies": {
49
51
  "@macalinao/eslint-config": "^5",
package/src/index.ts CHANGED
@@ -1,291 +1,14 @@
1
- import type { DefinedTypeNode, InstructionNode, Node, RootNode } from "codama";
2
- import {
3
- assertIsNode,
4
- bottomUpTransformerVisitor,
5
- camelCase,
6
- rootNodeVisitor,
7
- visit,
8
- } from "codama";
9
-
10
- /**
11
- * Rename mapping for a single program
12
- */
13
- export interface ProgramRenameOptions {
14
- /** Mapping of old instruction names to new instruction names */
15
- instructions?: Record<string, string>;
16
- /** Mapping of old event names (as defined types) to new event names */
17
- events?: Record<string, string>;
18
- /** Mapping of old defined type names to new defined type names */
19
- definedTypes?: Record<string, string>;
20
- }
21
-
22
- /**
23
- * Options for the rename visitor (legacy interface)
24
- */
25
- export type RenameVisitorOptions = ProgramRenameOptions;
26
-
27
- /**
28
- * Creates a visitor that renames instructions in a Codama IDL.
29
- *
30
- * @param mapping - Object mapping old instruction names to new instruction names
31
- * @returns A root node visitor that renames instructions
32
- *
33
- * @example
34
- * ```typescript
35
- * const visitor = renameInstructionsVisitor({
36
- * "transfer": "transferTokens",
37
- * "mint": "mintNft"
38
- * });
39
- * codama.update(visitor);
40
- * ```
41
- */
42
- export function renameInstructionsVisitor(
43
- mapping: Record<string, string>,
44
- ): ReturnType<typeof rootNodeVisitor> {
45
- return rootNodeVisitor((root) => {
46
- const instructionVisitor = bottomUpTransformerVisitor([
47
- {
48
- select: "[instructionNode]",
49
- transform: (node) => {
50
- assertIsNode(node, "instructionNode");
51
- const newName = mapping[node.name];
52
- if (!newName) {
53
- return node;
54
- }
55
- return {
56
- ...node,
57
- name: camelCase(newName),
58
- } as InstructionNode;
59
- },
60
- },
61
- ]);
62
- return visit(root, instructionVisitor);
63
- });
64
- }
65
-
66
- /**
67
- * Creates a visitor that renames events (as defined types) in a Codama IDL.
68
- *
69
- * Events in Anchor IDLs are typically converted to defined types in Codama,
70
- * so this visitor renames specific defined types that represent events.
71
- *
72
- * @param mapping - Object mapping old event names to new event names
73
- * @param eventSuffix - Optional suffix to identify event types (default: "Event")
74
- * @returns A root node visitor that renames events
75
- *
76
- * @example
77
- * ```typescript
78
- * const visitor = renameEventsVisitor({
79
- * "tokenMinted": "nftMinted",
80
- * "transferComplete": "transferFinished"
81
- * });
82
- * codama.update(visitor);
83
- * ```
84
- */
85
- export function renameEventsVisitor(
86
- mapping: Record<string, string>,
87
- eventSuffix = "Event",
88
- ): ReturnType<typeof rootNodeVisitor> {
89
- return rootNodeVisitor((root) => {
90
- const eventVisitor = bottomUpTransformerVisitor([
91
- {
92
- select: "[definedTypeNode]",
93
- transform: (node) => {
94
- assertIsNode(node, "definedTypeNode");
95
-
96
- // Check if this is an event type (by suffix or by being in the mapping)
97
- const isEventBySuffix = node.name.endsWith(eventSuffix);
98
- const isEventInMapping = mapping[node.name] !== undefined;
99
-
100
- if (!(isEventBySuffix || isEventInMapping)) {
101
- return node;
102
- }
103
-
104
- const newName = mapping[node.name];
105
- if (!newName) {
106
- return node;
107
- }
108
-
109
- return {
110
- ...node,
111
- name: camelCase(newName),
112
- } as DefinedTypeNode;
113
- },
114
- },
115
- ]);
116
- return visit(root, eventVisitor);
117
- });
118
- }
119
-
120
- /**
121
- * Creates a visitor that renames defined types in a Codama IDL.
122
- *
123
- * @param mapping - Object mapping old defined type names to new defined type names
124
- * @returns A root node visitor that renames defined types
125
- *
126
- * @example
127
- * ```typescript
128
- * const visitor = renameDefinedTypesVisitor({
129
- * "counter": "counterAccount",
130
- * "config": "programConfig"
131
- * });
132
- * codama.update(visitor);
133
- * ```
134
- */
135
- export function renameDefinedTypesVisitor(
136
- mapping: Record<string, string>,
137
- ): ReturnType<typeof rootNodeVisitor> {
138
- return rootNodeVisitor((root) => {
139
- const typeVisitor = bottomUpTransformerVisitor([
140
- {
141
- select: "[definedTypeNode]",
142
- transform: (node) => {
143
- assertIsNode(node, "definedTypeNode");
144
- const newName = mapping[node.name];
145
- if (!newName) {
146
- return node;
147
- }
148
- return {
149
- ...node,
150
- name: camelCase(newName),
151
- } as DefinedTypeNode;
152
- },
153
- },
154
- ]);
155
- return visit(root, typeVisitor);
156
- });
157
- }
158
-
159
- /**
160
- * Creates a visitor that renames instructions, events, and defined types in specific programs.
161
- * This follows the same pattern as addPdasVisitor from Codama.
162
- *
163
- * @param renamesByProgram - Object mapping program names to their rename configurations
164
- * @returns A root node visitor that performs all specified renames
165
- *
166
- * @example
167
- * ```typescript
168
- * const visitor = renameVisitor({
169
- * quarryMine: {
170
- * instructions: {
171
- * claimRewards: "claimRewardsMine"
172
- * }
173
- * },
174
- * token: {
175
- * instructions: {
176
- * transfer: "transferTokens",
177
- * mint: "mintNft"
178
- * },
179
- * events: {
180
- * tokenMinted: "nftMinted"
181
- * }
182
- * }
183
- * });
184
- * codama.update(visitor);
185
- * ```
186
- */
187
- export function renameVisitor(
188
- renamesByProgram: Record<string, ProgramRenameOptions>,
189
- ): ReturnType<typeof rootNodeVisitor> {
190
- return rootNodeVisitor((root) => {
191
- // Check if this is the legacy single-program format
192
- // (has instructions, events, or definedTypes at the top level)
193
- const isLegacyFormat =
194
- "instructions" in renamesByProgram ||
195
- "events" in renamesByProgram ||
196
- "definedTypes" in renamesByProgram;
197
-
198
- if (isLegacyFormat) {
199
- // Legacy support: treat as single program renames
200
- const options = renamesByProgram as unknown as RenameVisitorOptions;
201
- let transformedRoot = root;
202
-
203
- // Apply instruction renaming
204
- if (
205
- options.instructions &&
206
- Object.keys(options.instructions).length > 0
207
- ) {
208
- transformedRoot = visit(
209
- transformedRoot,
210
- renameInstructionsVisitor(options.instructions),
211
- ) as RootNode;
212
- }
213
-
214
- // Apply event renaming
215
- if (options.events && Object.keys(options.events).length > 0) {
216
- transformedRoot = visit(
217
- transformedRoot,
218
- renameEventsVisitor(options.events),
219
- ) as RootNode;
220
- }
221
-
222
- // Apply defined type renaming
223
- if (
224
- options.definedTypes &&
225
- Object.keys(options.definedTypes).length > 0
226
- ) {
227
- transformedRoot = visit(
228
- transformedRoot,
229
- renameDefinedTypesVisitor(options.definedTypes),
230
- ) as RootNode;
231
- }
232
-
233
- return transformedRoot;
234
- }
235
-
236
- // New format: program-specific renames
237
- const transforms: {
238
- select: string;
239
- transform: (node: Node) => Node | null;
240
- }[] = [];
241
-
242
- // Process each program's rename configuration
243
- Object.entries(renamesByProgram).forEach(([programName, renameOptions]) => {
244
- // Add instruction renames for this program
245
- if (renameOptions.instructions) {
246
- Object.entries(renameOptions.instructions).forEach(
247
- ([oldName, newName]) => {
248
- transforms.push({
249
- select: `[programNode]${programName}.[instructionNode]${oldName}`,
250
- transform: (node) => {
251
- assertIsNode(node, "instructionNode");
252
- return {
253
- ...node,
254
- name: camelCase(newName),
255
- } as InstructionNode;
256
- },
257
- });
258
- },
259
- );
260
- }
261
-
262
- // Add event/defined type renames for this program
263
- if (renameOptions.events || renameOptions.definedTypes) {
264
- const allTypeRenames = {
265
- ...(renameOptions.events ?? {}),
266
- ...(renameOptions.definedTypes ?? {}),
267
- };
268
-
269
- Object.entries(allTypeRenames).forEach(([oldName, newName]) => {
270
- transforms.push({
271
- select: `[programNode]${programName}.[definedTypeNode]${oldName}`,
272
- transform: (node) => {
273
- assertIsNode(node, "definedTypeNode");
274
- return {
275
- ...node,
276
- name: camelCase(newName),
277
- } as DefinedTypeNode;
278
- },
279
- });
280
- });
281
- }
282
- });
283
-
284
- if (transforms.length === 0) {
285
- return root;
286
- }
287
-
288
- const visitor = bottomUpTransformerVisitor(transforms);
289
- return visit(root, visitor);
290
- });
291
- }
1
+ export {
2
+ renameAccountsVisitor,
3
+ renameAccountTransform,
4
+ } from "./rename-accounts-visitor.js";
5
+ export {
6
+ renameDefinedTypesVisitor,
7
+ renameDefinedTypeTransform,
8
+ } from "./rename-defined-types-visitor.js";
9
+ export {
10
+ renameInstructionsVisitor,
11
+ renameInstructionTransform,
12
+ } from "./rename-instructions-visitor.js";
13
+ export { renameVisitor } from "./rename-visitor.js";
14
+ export type { ProgramRenameOptions } from "./types.js";
@@ -0,0 +1,50 @@
1
+ import type { AccountNode, Node, rootNodeVisitor } from "codama";
2
+ import { assertIsNode, bottomUpTransformerVisitor, camelCase } from "codama";
3
+
4
+ /**
5
+ * Transform function that renames an account node based on a mapping.
6
+ *
7
+ * @param node - The node to transform
8
+ * @param mapping - Object mapping old account names to new account names
9
+ * @returns The transformed account node
10
+ */
11
+ export function renameAccountTransform(
12
+ node: Node,
13
+ mapping: Record<string, string>,
14
+ ): AccountNode {
15
+ assertIsNode(node, "accountNode");
16
+ const newName = mapping[node.name];
17
+ if (!newName) {
18
+ return node;
19
+ }
20
+ return {
21
+ ...node,
22
+ name: camelCase(newName),
23
+ };
24
+ }
25
+
26
+ /**
27
+ * Creates a visitor that renames accounts in a Codama IDL.
28
+ *
29
+ * @param mapping - Object mapping old account names to new account names
30
+ * @returns A root node visitor that renames accounts
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const visitor = renameAccountsVisitor({
35
+ * "userAccount": "user",
36
+ * "configAccount": "config"
37
+ * });
38
+ * codama.update(visitor);
39
+ * ```
40
+ */
41
+ export function renameAccountsVisitor(
42
+ mapping: Record<string, string>,
43
+ ): ReturnType<typeof rootNodeVisitor> {
44
+ return bottomUpTransformerVisitor([
45
+ {
46
+ select: "[accountNode]",
47
+ transform: (node) => renameAccountTransform(node, mapping),
48
+ },
49
+ ]);
50
+ }
@@ -0,0 +1,44 @@
1
+ /// <reference types="bun-types" />
2
+ import { describe, expect, it } from "bun:test";
3
+ import type { ProgramNode, RootNode } from "codama";
4
+ import {
5
+ camelCase,
6
+ definedTypeNode,
7
+ numberTypeNode,
8
+ programNode,
9
+ publicKeyTypeNode,
10
+ rootNode,
11
+ visit,
12
+ } from "codama";
13
+ import { renameDefinedTypesVisitor } from "./rename-defined-types-visitor.js";
14
+
15
+ describe("renameDefinedTypesVisitor", () => {
16
+ it("should rename defined types based on the mapping", () => {
17
+ const program: ProgramNode = programNode({
18
+ name: camelCase("testProgram"),
19
+ publicKey: "11111111111111111111111111111111",
20
+ definedTypes: [
21
+ definedTypeNode({
22
+ name: camelCase("counter"),
23
+ type: numberTypeNode("u64"),
24
+ }),
25
+ definedTypeNode({
26
+ name: camelCase("config"),
27
+ type: publicKeyTypeNode(),
28
+ }),
29
+ ],
30
+ });
31
+
32
+ const root = rootNode(program);
33
+ const visitor = renameDefinedTypesVisitor({
34
+ counter: "counterAccount",
35
+ config: "programConfig",
36
+ });
37
+
38
+ const updatedRoot = visit(root, visitor) as RootNode;
39
+ const types = updatedRoot.program.definedTypes;
40
+
41
+ expect(types[0].name.toString()).toBe("counterAccount");
42
+ expect(types[1].name.toString()).toBe("programConfig");
43
+ });
44
+ });
@@ -0,0 +1,59 @@
1
+ import type { DefinedTypeNode, Node } from "codama";
2
+ import {
3
+ assertIsNode,
4
+ bottomUpTransformerVisitor,
5
+ camelCase,
6
+ rootNodeVisitor,
7
+ visit,
8
+ } from "codama";
9
+
10
+ /**
11
+ * Transform function that renames a defined type node based on a mapping.
12
+ *
13
+ * @param node - The node to transform
14
+ * @param mapping - Object mapping old defined type names to new defined type names
15
+ * @returns The transformed defined type node
16
+ */
17
+ export function renameDefinedTypeTransform(
18
+ node: Node,
19
+ mapping: Record<string, string>,
20
+ ): DefinedTypeNode {
21
+ assertIsNode(node, "definedTypeNode");
22
+ const newName = mapping[node.name];
23
+ if (!newName) {
24
+ return node;
25
+ }
26
+ return {
27
+ ...node,
28
+ name: camelCase(newName),
29
+ };
30
+ }
31
+
32
+ /**
33
+ * Creates a visitor that renames defined types in a Codama IDL.
34
+ *
35
+ * @param mapping - Object mapping old defined type names to new defined type names
36
+ * @returns A root node visitor that renames defined types
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * const visitor = renameDefinedTypesVisitor({
41
+ * "counter": "counterAccount",
42
+ * "config": "programConfig"
43
+ * });
44
+ * codama.update(visitor);
45
+ * ```
46
+ */
47
+ export function renameDefinedTypesVisitor(
48
+ mapping: Record<string, string>,
49
+ ): ReturnType<typeof rootNodeVisitor> {
50
+ return rootNodeVisitor((root) => {
51
+ const typeVisitor = bottomUpTransformerVisitor([
52
+ {
53
+ select: "[definedTypeNode]",
54
+ transform: (node) => renameDefinedTypeTransform(node, mapping),
55
+ },
56
+ ]);
57
+ return visit(root, typeVisitor);
58
+ });
59
+ }