@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.
- package/README.md +24 -1
- package/dist/index.d.ts +5 -97
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -220
- package/dist/index.js.map +1 -1
- package/dist/rename-accounts-visitor.d.ts +26 -0
- package/dist/rename-accounts-visitor.d.ts.map +1 -0
- package/dist/rename-accounts-visitor.js +43 -0
- package/dist/rename-accounts-visitor.js.map +1 -0
- package/dist/rename-defined-types-visitor.d.ts +27 -0
- package/dist/rename-defined-types-visitor.d.ts.map +1 -0
- package/dist/rename-defined-types-visitor.js +46 -0
- package/dist/rename-defined-types-visitor.js.map +1 -0
- package/dist/rename-instructions-visitor.d.ts +26 -0
- package/dist/rename-instructions-visitor.d.ts.map +1 -0
- package/dist/rename-instructions-visitor.js +43 -0
- package/dist/rename-instructions-visitor.js.map +1 -0
- package/dist/rename-visitor.d.ts +35 -0
- package/dist/rename-visitor.d.ts.map +1 -0
- package/dist/rename-visitor.js +75 -0
- package/dist/rename-visitor.js.map +1 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +7 -5
- package/src/index.ts +14 -291
- package/src/rename-accounts-visitor.ts +50 -0
- package/src/rename-defined-types-visitor.test.ts +44 -0
- package/src/rename-defined-types-visitor.ts +59 -0
- package/src/rename-instructions-visitor.test.ts +83 -0
- package/src/rename-instructions-visitor.ts +50 -0
- package/src/rename-visitor.test.ts +175 -0
- package/src/rename-visitor.ts +94 -0
- package/src/types.ts +11 -0
- package/src/index.test.ts +0 -409
package/src/index.test.ts
DELETED
|
@@ -1,409 +0,0 @@
|
|
|
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
|
-
instructionAccountNode,
|
|
8
|
-
instructionNode,
|
|
9
|
-
numberTypeNode,
|
|
10
|
-
programNode,
|
|
11
|
-
publicKeyTypeNode,
|
|
12
|
-
rootNode,
|
|
13
|
-
visit,
|
|
14
|
-
} from "codama";
|
|
15
|
-
import {
|
|
16
|
-
renameDefinedTypesVisitor,
|
|
17
|
-
renameEventsVisitor,
|
|
18
|
-
renameInstructionsVisitor,
|
|
19
|
-
renameVisitor,
|
|
20
|
-
} from "./index.js";
|
|
21
|
-
|
|
22
|
-
describe("renameInstructionsVisitor", () => {
|
|
23
|
-
it("should rename instructions based on the mapping", () => {
|
|
24
|
-
const program: ProgramNode = programNode({
|
|
25
|
-
name: camelCase("testProgram"),
|
|
26
|
-
publicKey: "11111111111111111111111111111111",
|
|
27
|
-
instructions: [
|
|
28
|
-
instructionNode({
|
|
29
|
-
name: camelCase("transfer"),
|
|
30
|
-
accounts: [
|
|
31
|
-
instructionAccountNode({
|
|
32
|
-
name: camelCase("source"),
|
|
33
|
-
isWritable: true,
|
|
34
|
-
isSigner: false,
|
|
35
|
-
}),
|
|
36
|
-
instructionAccountNode({
|
|
37
|
-
name: camelCase("destination"),
|
|
38
|
-
isWritable: true,
|
|
39
|
-
isSigner: false,
|
|
40
|
-
}),
|
|
41
|
-
],
|
|
42
|
-
}),
|
|
43
|
-
instructionNode({
|
|
44
|
-
name: camelCase("mint"),
|
|
45
|
-
accounts: [
|
|
46
|
-
instructionAccountNode({
|
|
47
|
-
name: camelCase("mint"),
|
|
48
|
-
isWritable: true,
|
|
49
|
-
isSigner: false,
|
|
50
|
-
}),
|
|
51
|
-
],
|
|
52
|
-
}),
|
|
53
|
-
],
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const root = rootNode(program);
|
|
57
|
-
const visitor = renameInstructionsVisitor({
|
|
58
|
-
transfer: "transferTokens",
|
|
59
|
-
mint: "mintNft",
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
63
|
-
const instructions = updatedRoot.program.instructions;
|
|
64
|
-
|
|
65
|
-
expect(instructions[0].name.toString()).toBe("transferTokens");
|
|
66
|
-
expect(instructions[1].name.toString()).toBe("mintNft");
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("should leave unmapped instructions unchanged", () => {
|
|
70
|
-
const program: ProgramNode = programNode({
|
|
71
|
-
name: camelCase("testProgram"),
|
|
72
|
-
publicKey: "11111111111111111111111111111111",
|
|
73
|
-
instructions: [
|
|
74
|
-
instructionNode({
|
|
75
|
-
name: camelCase("burn"),
|
|
76
|
-
accounts: [],
|
|
77
|
-
}),
|
|
78
|
-
],
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
const root = rootNode(program);
|
|
82
|
-
const visitor = renameInstructionsVisitor({
|
|
83
|
-
transfer: "transferTokens",
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
87
|
-
const instructions = updatedRoot.program.instructions;
|
|
88
|
-
|
|
89
|
-
expect(instructions[0].name.toString()).toBe("burn");
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
describe("renameDefinedTypesVisitor", () => {
|
|
94
|
-
it("should rename defined types based on the mapping", () => {
|
|
95
|
-
const program: ProgramNode = programNode({
|
|
96
|
-
name: camelCase("testProgram"),
|
|
97
|
-
publicKey: "11111111111111111111111111111111",
|
|
98
|
-
definedTypes: [
|
|
99
|
-
definedTypeNode({
|
|
100
|
-
name: camelCase("counter"),
|
|
101
|
-
type: numberTypeNode("u64"),
|
|
102
|
-
}),
|
|
103
|
-
definedTypeNode({
|
|
104
|
-
name: camelCase("config"),
|
|
105
|
-
type: publicKeyTypeNode(),
|
|
106
|
-
}),
|
|
107
|
-
],
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const root = rootNode(program);
|
|
111
|
-
const visitor = renameDefinedTypesVisitor({
|
|
112
|
-
counter: "counterAccount",
|
|
113
|
-
config: "programConfig",
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
117
|
-
const types = updatedRoot.program.definedTypes;
|
|
118
|
-
|
|
119
|
-
expect(types[0].name.toString()).toBe("counterAccount");
|
|
120
|
-
expect(types[1].name.toString()).toBe("programConfig");
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
describe("renameEventsVisitor", () => {
|
|
125
|
-
it("should rename events (as defined types with Event suffix)", () => {
|
|
126
|
-
const program: ProgramNode = programNode({
|
|
127
|
-
name: camelCase("testProgram"),
|
|
128
|
-
publicKey: "11111111111111111111111111111111",
|
|
129
|
-
definedTypes: [
|
|
130
|
-
definedTypeNode({
|
|
131
|
-
name: camelCase("tokenMintedEvent"),
|
|
132
|
-
type: numberTypeNode("u64"),
|
|
133
|
-
}),
|
|
134
|
-
definedTypeNode({
|
|
135
|
-
name: camelCase("transferCompleteEvent"),
|
|
136
|
-
type: publicKeyTypeNode(),
|
|
137
|
-
}),
|
|
138
|
-
definedTypeNode({
|
|
139
|
-
name: camelCase("regularType"),
|
|
140
|
-
type: numberTypeNode("u32"),
|
|
141
|
-
}),
|
|
142
|
-
],
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const root = rootNode(program);
|
|
146
|
-
const visitor = renameEventsVisitor({
|
|
147
|
-
tokenMintedEvent: "nftMintedEvent",
|
|
148
|
-
transferCompleteEvent: "transferFinishedEvent",
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
152
|
-
const types = updatedRoot.program.definedTypes;
|
|
153
|
-
|
|
154
|
-
expect(types[0].name.toString()).toBe("nftMintedEvent");
|
|
155
|
-
expect(types[1].name.toString()).toBe("transferFinishedEvent");
|
|
156
|
-
expect(types[2].name.toString()).toBe("regularType"); // Should remain unchanged
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it("should rename events without suffix if explicitly in mapping", () => {
|
|
160
|
-
const program: ProgramNode = programNode({
|
|
161
|
-
name: camelCase("testProgram"),
|
|
162
|
-
publicKey: "11111111111111111111111111111111",
|
|
163
|
-
definedTypes: [
|
|
164
|
-
definedTypeNode({
|
|
165
|
-
name: camelCase("tokenMinted"),
|
|
166
|
-
type: numberTypeNode("u64"),
|
|
167
|
-
}),
|
|
168
|
-
],
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const root = rootNode(program);
|
|
172
|
-
const visitor = renameEventsVisitor({
|
|
173
|
-
tokenMinted: "nftMinted",
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
177
|
-
const types = updatedRoot.program.definedTypes;
|
|
178
|
-
|
|
179
|
-
expect(types[0].name.toString()).toBe("nftMinted");
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
describe("renameVisitor (legacy format)", () => {
|
|
184
|
-
it("should rename both instructions and events", () => {
|
|
185
|
-
const program: ProgramNode = programNode({
|
|
186
|
-
name: camelCase("testProgram"),
|
|
187
|
-
publicKey: "11111111111111111111111111111111",
|
|
188
|
-
instructions: [
|
|
189
|
-
instructionNode({
|
|
190
|
-
name: camelCase("transfer"),
|
|
191
|
-
accounts: [],
|
|
192
|
-
}),
|
|
193
|
-
],
|
|
194
|
-
definedTypes: [
|
|
195
|
-
definedTypeNode({
|
|
196
|
-
name: camelCase("tokenMintedEvent"),
|
|
197
|
-
type: numberTypeNode("u64"),
|
|
198
|
-
}),
|
|
199
|
-
],
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
const root = rootNode(program);
|
|
203
|
-
const visitor = renameVisitor({
|
|
204
|
-
instructions: {
|
|
205
|
-
transfer: "transferTokens",
|
|
206
|
-
},
|
|
207
|
-
events: {
|
|
208
|
-
tokenMintedEvent: "nftMintedEvent",
|
|
209
|
-
},
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
213
|
-
const instructions = updatedRoot.program.instructions;
|
|
214
|
-
const types = updatedRoot.program.definedTypes;
|
|
215
|
-
|
|
216
|
-
expect(instructions[0].name.toString()).toBe("transferTokens");
|
|
217
|
-
expect(types[0].name.toString()).toBe("nftMintedEvent");
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
it("should handle empty options gracefully", () => {
|
|
221
|
-
const program: ProgramNode = programNode({
|
|
222
|
-
name: camelCase("testProgram"),
|
|
223
|
-
publicKey: "11111111111111111111111111111111",
|
|
224
|
-
instructions: [
|
|
225
|
-
instructionNode({
|
|
226
|
-
name: camelCase("transfer"),
|
|
227
|
-
accounts: [],
|
|
228
|
-
}),
|
|
229
|
-
],
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
const root = rootNode(program);
|
|
233
|
-
const visitor = renameVisitor({});
|
|
234
|
-
|
|
235
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
236
|
-
const instructions = updatedRoot.program.instructions;
|
|
237
|
-
|
|
238
|
-
expect(instructions[0].name.toString()).toBe("transfer");
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
describe("renameVisitor (program-specific format)", () => {
|
|
243
|
-
it("should rename instructions in specific programs", () => {
|
|
244
|
-
const quarryMineProgram: ProgramNode = programNode({
|
|
245
|
-
name: camelCase("quarryMine"),
|
|
246
|
-
publicKey: "11111111111111111111111111111111",
|
|
247
|
-
instructions: [
|
|
248
|
-
instructionNode({
|
|
249
|
-
name: camelCase("claimRewards"),
|
|
250
|
-
accounts: [],
|
|
251
|
-
}),
|
|
252
|
-
instructionNode({
|
|
253
|
-
name: camelCase("stake"),
|
|
254
|
-
accounts: [],
|
|
255
|
-
}),
|
|
256
|
-
],
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
const tokenProgram: ProgramNode = programNode({
|
|
260
|
-
name: camelCase("token"),
|
|
261
|
-
publicKey: "22222222222222222222222222222222",
|
|
262
|
-
instructions: [
|
|
263
|
-
instructionNode({
|
|
264
|
-
name: camelCase("transfer"),
|
|
265
|
-
accounts: [],
|
|
266
|
-
}),
|
|
267
|
-
instructionNode({
|
|
268
|
-
name: camelCase("mint"),
|
|
269
|
-
accounts: [],
|
|
270
|
-
}),
|
|
271
|
-
],
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
// Create a root with multiple programs
|
|
275
|
-
const root = rootNode(quarryMineProgram, [tokenProgram]);
|
|
276
|
-
|
|
277
|
-
const visitor = renameVisitor({
|
|
278
|
-
quarryMine: {
|
|
279
|
-
instructions: {
|
|
280
|
-
claimRewards: "claimRewardsMine",
|
|
281
|
-
},
|
|
282
|
-
},
|
|
283
|
-
token: {
|
|
284
|
-
instructions: {
|
|
285
|
-
transfer: "transferTokens",
|
|
286
|
-
mint: "mintNft",
|
|
287
|
-
},
|
|
288
|
-
},
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
292
|
-
const quarryInstructions = updatedRoot.program.instructions;
|
|
293
|
-
const tokenInstructions =
|
|
294
|
-
updatedRoot.additionalPrograms?.[0]?.instructions ?? [];
|
|
295
|
-
|
|
296
|
-
expect(quarryInstructions[0].name.toString()).toBe("claimRewardsMine");
|
|
297
|
-
expect(quarryInstructions[1].name.toString()).toBe("stake"); // Unchanged
|
|
298
|
-
expect(tokenInstructions[0].name.toString()).toBe("transferTokens");
|
|
299
|
-
expect(tokenInstructions[1].name.toString()).toBe("mintNft");
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
it("should rename events and defined types in specific programs", () => {
|
|
303
|
-
const program: ProgramNode = programNode({
|
|
304
|
-
name: camelCase("myProgram"),
|
|
305
|
-
publicKey: "11111111111111111111111111111111",
|
|
306
|
-
definedTypes: [
|
|
307
|
-
definedTypeNode({
|
|
308
|
-
name: camelCase("tokenMintedEvent"),
|
|
309
|
-
type: numberTypeNode("u64"),
|
|
310
|
-
}),
|
|
311
|
-
definedTypeNode({
|
|
312
|
-
name: camelCase("counter"),
|
|
313
|
-
type: numberTypeNode("u64"),
|
|
314
|
-
}),
|
|
315
|
-
],
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
const root = rootNode(program);
|
|
319
|
-
|
|
320
|
-
const visitor = renameVisitor({
|
|
321
|
-
myProgram: {
|
|
322
|
-
events: {
|
|
323
|
-
tokenMintedEvent: "nftMintedEvent",
|
|
324
|
-
},
|
|
325
|
-
definedTypes: {
|
|
326
|
-
counter: "counterAccount",
|
|
327
|
-
},
|
|
328
|
-
},
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
332
|
-
const types = updatedRoot.program.definedTypes;
|
|
333
|
-
|
|
334
|
-
expect(types[0].name.toString()).toBe("nftMintedEvent");
|
|
335
|
-
expect(types[1].name.toString()).toBe("counterAccount");
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
it("should work with Quarry-style program names (camelCase)", () => {
|
|
339
|
-
const quarryMergeMineProgram: ProgramNode = programNode({
|
|
340
|
-
name: camelCase("quarryMergeMine"),
|
|
341
|
-
publicKey: "QMMD16kjauP5knBwxNUJRZ1Z5o3deBuFrqVjBVmmqto",
|
|
342
|
-
instructions: [
|
|
343
|
-
instructionNode({
|
|
344
|
-
name: camelCase("claimRewards"),
|
|
345
|
-
accounts: [],
|
|
346
|
-
}),
|
|
347
|
-
instructionNode({
|
|
348
|
-
name: camelCase("stake"),
|
|
349
|
-
accounts: [],
|
|
350
|
-
}),
|
|
351
|
-
],
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
const root = rootNode(quarryMergeMineProgram);
|
|
355
|
-
|
|
356
|
-
const visitor = renameVisitor({
|
|
357
|
-
quarryMergeMine: {
|
|
358
|
-
instructions: {
|
|
359
|
-
claimRewards: "claimRewardsMergeMine",
|
|
360
|
-
},
|
|
361
|
-
},
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
365
|
-
const instructions = updatedRoot.program.instructions;
|
|
366
|
-
|
|
367
|
-
expect(instructions[0].name.toString()).toBe("claimRewardsMergeMine");
|
|
368
|
-
expect(instructions[1].name.toString()).toBe("stake"); // Unchanged
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it("should handle mixed program configurations", () => {
|
|
372
|
-
const program: ProgramNode = programNode({
|
|
373
|
-
name: camelCase("testProgram"),
|
|
374
|
-
publicKey: "11111111111111111111111111111111",
|
|
375
|
-
instructions: [
|
|
376
|
-
instructionNode({
|
|
377
|
-
name: camelCase("transfer"),
|
|
378
|
-
accounts: [],
|
|
379
|
-
}),
|
|
380
|
-
],
|
|
381
|
-
definedTypes: [
|
|
382
|
-
definedTypeNode({
|
|
383
|
-
name: camelCase("transferEvent"),
|
|
384
|
-
type: numberTypeNode("u64"),
|
|
385
|
-
}),
|
|
386
|
-
],
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
const root = rootNode(program);
|
|
390
|
-
|
|
391
|
-
const visitor = renameVisitor({
|
|
392
|
-
testProgram: {
|
|
393
|
-
instructions: {
|
|
394
|
-
transfer: "sendTokens",
|
|
395
|
-
},
|
|
396
|
-
events: {
|
|
397
|
-
transferEvent: "tokenSentEvent",
|
|
398
|
-
},
|
|
399
|
-
},
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
const updatedRoot = visit(root, visitor) as RootNode;
|
|
403
|
-
const instructions = updatedRoot.program.instructions;
|
|
404
|
-
const types = updatedRoot.program.definedTypes;
|
|
405
|
-
|
|
406
|
-
expect(instructions[0].name.toString()).toBe("sendTokens");
|
|
407
|
-
expect(types[0].name.toString()).toBe("tokenSentEvent");
|
|
408
|
-
});
|
|
409
|
-
});
|