Browse Source

Fix unused args variable on sync instruction functions on JS renderer again (#46)

Loris Leiva 1 year ago
parent
commit
bcd5eaedf6

+ 5 - 0
.changeset/mighty-hotels-join.md

@@ -0,0 +1,5 @@
+---
+"@kinobi-so/renderers-js": patch
+---
+
+Fix unused args variable on sync instruction functions on JS renderer again

+ 2 - 1
packages/renderers-js/src/utils/async.ts

@@ -91,7 +91,8 @@ export function getInstructionDependencies(
     }
 
     if (isNode(input.defaultValue, 'resolverValueNode')) {
-        if (!useAsync || asyncResolvers.includes(input.defaultValue.name)) {
+        const isSynchronousResolver = !asyncResolvers.includes(input.defaultValue.name);
+        if (useAsync || isSynchronousResolver) {
             return input.defaultValue.dependsOn ?? [];
         }
     }

+ 41 - 1
packages/renderers-js/test/instructionsPage.test.ts

@@ -76,7 +76,7 @@ test('it renders extra arguments that default on each other', async () => {
     ]);
 });
 
-test('it only renders the args variable on the async function if the sync function does not need it', async () => {
+test('it renders the args variable on the async function only if the extra argument has an async default value', async () => {
     // Given the following instruction with an async resolver and an extra argument.
     const node = programNode({
         instructions: [
@@ -108,6 +108,46 @@ test('it only renders the args variable on the async function if the sync functi
     await codeDoesNotContain(syncFunction, ['// Original args.', 'const args = { ...input }']);
 });
 
+test('it only renders the args variable on the async function if the extra argument is used in an async default value', async () => {
+    // Given the following instruction with an async resolver depending on
+    // an extra argument such that the instruction has no data arguments.
+    const node = programNode({
+        instructions: [
+            instructionNode({
+                accounts: [
+                    instructionAccountNode({
+                        defaultValue: resolverValueNode('myAsyncResolver', { dependsOn: [argumentValueNode('bar')] }),
+                        isSigner: false,
+                        isWritable: false,
+                        name: 'foo',
+                    }),
+                ],
+                extraArguments: [
+                    instructionArgumentNode({
+                        name: 'bar',
+                        type: numberTypeNode('u64'),
+                    }),
+                ],
+                name: 'create',
+            }),
+        ],
+        name: 'myProgram',
+        publicKey: '1111',
+    });
+
+    // When we render it.
+    const renderMap = visit(node, getRenderMapVisitor({ asyncResolvers: ['myAsyncResolver'] }));
+
+    // And split the async and sync functions.
+    const [asyncFunction, syncFunction] = renderMap
+        .get('instructions/create.ts')
+        .split(/export\s+function\s+getCreateInstruction/);
+
+    // Then we expect only the async function to contain the args variable.
+    await codeContains(asyncFunction, ['// Original args.', 'const args = { ...input }']);
+    await codeDoesNotContain(syncFunction, ['// Original args.', 'const args = { ...input }']);
+});
+
 test('it renders instruction accounts with linked PDAs as default value', async () => {
     // Given the following program with a PDA node and an instruction account using it as default value.
     const node = programNode({