Skip to content

Commit bcd5eae

Browse files
authored
Fix unused args variable on sync instruction functions on JS renderer again (#46)
1 parent b0c6fae commit bcd5eae

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

.changeset/mighty-hotels-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@kinobi-so/renderers-js": patch
3+
---
4+
5+
Fix unused args variable on sync instruction functions on JS renderer again

packages/renderers-js/src/utils/async.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export function getInstructionDependencies(
9191
}
9292

9393
if (isNode(input.defaultValue, 'resolverValueNode')) {
94-
if (!useAsync || asyncResolvers.includes(input.defaultValue.name)) {
94+
const isSynchronousResolver = !asyncResolvers.includes(input.defaultValue.name);
95+
if (useAsync || isSynchronousResolver) {
9596
return input.defaultValue.dependsOn ?? [];
9697
}
9798
}

packages/renderers-js/test/instructionsPage.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test('it renders extra arguments that default on each other', async () => {
7676
]);
7777
});
7878

79-
test('it only renders the args variable on the async function if the sync function does not need it', async () => {
79+
test('it renders the args variable on the async function only if the extra argument has an async default value', async () => {
8080
// Given the following instruction with an async resolver and an extra argument.
8181
const node = programNode({
8282
instructions: [
@@ -108,6 +108,46 @@ test('it only renders the args variable on the async function if the sync functi
108108
await codeDoesNotContain(syncFunction, ['// Original args.', 'const args = { ...input }']);
109109
});
110110

111+
test('it only renders the args variable on the async function if the extra argument is used in an async default value', async () => {
112+
// Given the following instruction with an async resolver depending on
113+
// an extra argument such that the instruction has no data arguments.
114+
const node = programNode({
115+
instructions: [
116+
instructionNode({
117+
accounts: [
118+
instructionAccountNode({
119+
defaultValue: resolverValueNode('myAsyncResolver', { dependsOn: [argumentValueNode('bar')] }),
120+
isSigner: false,
121+
isWritable: false,
122+
name: 'foo',
123+
}),
124+
],
125+
extraArguments: [
126+
instructionArgumentNode({
127+
name: 'bar',
128+
type: numberTypeNode('u64'),
129+
}),
130+
],
131+
name: 'create',
132+
}),
133+
],
134+
name: 'myProgram',
135+
publicKey: '1111',
136+
});
137+
138+
// When we render it.
139+
const renderMap = visit(node, getRenderMapVisitor({ asyncResolvers: ['myAsyncResolver'] }));
140+
141+
// And split the async and sync functions.
142+
const [asyncFunction, syncFunction] = renderMap
143+
.get('instructions/create.ts')
144+
.split(/export\s+function\s+getCreateInstruction/);
145+
146+
// Then we expect only the async function to contain the args variable.
147+
await codeContains(asyncFunction, ['// Original args.', 'const args = { ...input }']);
148+
await codeDoesNotContain(syncFunction, ['// Original args.', 'const args = { ...input }']);
149+
});
150+
111151
test('it renders instruction accounts with linked PDAs as default value', async () => {
112152
// Given the following program with a PDA node and an instruction account using it as default value.
113153
const node = programNode({

0 commit comments

Comments
 (0)