Skip to content

Commit e3e4099

Browse files
authored
Filter out type variant on imports (#53)
* Add missing type prefix * Add changeset * Fix lint * Revert prefix * Filter out type variant on imports * Update changeset description
1 parent e129b87 commit e3e4099

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

.changeset/shy-points-march.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+
Filter out type variant on imports

packages/renderers-js/src/ImportMap.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,18 @@ export class ImportMap {
135135
return a.localeCompare(b);
136136
})
137137
.map(([module, imports]) => {
138-
const joinedImports = [...imports].sort().join(', ');
138+
const joinedImports = [...imports]
139+
.sort()
140+
.filter(i => {
141+
// import of a type can either be '<Type>' or 'type <Type>', so
142+
// we filter out 'type <Type>' variation if there is a '<Type>'
143+
const name = i.split(' ');
144+
if (name.length > 1) {
145+
return !imports.has(name[1]);
146+
}
147+
return true;
148+
})
149+
.join(', ');
139150
return `import { ${joinedImports} } from '${module}';`;
140151
})
141152
.join('\n');

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
import { accountNode, pdaLinkNode, pdaNode, programNode } from '@kinobi-so/nodes';
1+
import {
2+
accountNode,
3+
booleanTypeNode,
4+
definedTypeLinkNode,
5+
definedTypeNode,
6+
enumEmptyVariantTypeNode,
7+
enumTypeNode,
8+
enumValueNode,
9+
fieldDiscriminatorNode,
10+
pdaLinkNode,
11+
pdaNode,
12+
programNode,
13+
publicKeyTypeNode,
14+
structFieldTypeNode,
15+
structTypeNode,
16+
} from '@kinobi-so/nodes';
217
import { visit } from '@kinobi-so/visitors-core';
318
import { test } from 'vitest';
419

@@ -24,3 +39,44 @@ test('it renders PDA helpers for PDA with no seeds', async () => {
2439
'await findBarPda({ programAddress })',
2540
]);
2641
});
42+
43+
test('it renders an account with a defined type link as discriminator', async () => {
44+
// Given the following program with 1 account with a discriminator.
45+
const node = programNode({
46+
accounts: [
47+
accountNode({
48+
data: structTypeNode([
49+
structFieldTypeNode({
50+
defaultValue: enumValueNode('key', 'Asset'),
51+
defaultValueStrategy: 'omitted',
52+
name: 'key',
53+
type: definedTypeLinkNode('Key'),
54+
}),
55+
structFieldTypeNode({
56+
name: 'mutable',
57+
type: booleanTypeNode(),
58+
}),
59+
structFieldTypeNode({
60+
name: 'owner',
61+
type: publicKeyTypeNode(),
62+
}),
63+
]),
64+
discriminators: [fieldDiscriminatorNode('key', 0)],
65+
name: 'asset',
66+
}),
67+
],
68+
definedTypes: [
69+
definedTypeNode({
70+
name: 'key',
71+
type: enumTypeNode([enumEmptyVariantTypeNode('Uninitialized'), enumEmptyVariantTypeNode('Asset')]),
72+
}),
73+
],
74+
name: 'splToken',
75+
publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
76+
});
77+
78+
const renderMap = visit(node, getRenderMapVisitor());
79+
80+
// Then we expect the following import list with a reference to the disciminator type.
81+
await renderMapContains(renderMap, 'accounts/asset.ts', ['import { Key, getKeyDecoder, getKeyEncoder }']);
82+
});

0 commit comments

Comments
 (0)