-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
parseTypeArguments.ts
66 lines (52 loc) · 1.71 KB
/
parseTypeArguments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { TargetEnum } from '../types/enums/TargetEnum';
import type { IRawAbiTypeComponent } from '../types/interfaces/IRawAbiType';
import type { IType } from '../types/interfaces/IType';
import { findType } from './findType';
/*
Recursively parses the given `typeArguments` node
*/
export function parseTypeArguments(params: {
types: IType[];
target: TargetEnum;
typeArguments: IRawAbiTypeComponent[];
parentTypeId?: number;
}): string {
const { types, typeArguments, parentTypeId, target } = params;
const attributeKey: 'inputLabel' | 'outputLabel' = `${target}Label`;
const buffer: string[] = [];
let parentType: IType | undefined;
let parentLabel: string | undefined;
if (parentTypeId !== undefined) {
parentType = findType({ types, typeId: parentTypeId });
parentLabel = parentType.attributes[attributeKey];
}
// loop through all `typeArgument` items
typeArguments.forEach((typeArgument) => {
let currentLabel: string;
const currentTypeId = typeArgument.type;
try {
const currentType = findType({ types, typeId: currentTypeId });
currentLabel = currentType.attributes[attributeKey];
} catch (_err) {
// used for functions without output
currentLabel = 'void';
}
if (typeArgument.typeArguments) {
// recursively process nested `typeArguments`
const nestedParsed = parseTypeArguments({
types,
target,
parentTypeId: typeArgument.type,
typeArguments: typeArgument.typeArguments,
});
buffer.push(nestedParsed);
} else {
buffer.push(`${currentLabel}`);
}
});
let output = buffer.join(', ');
if (parentLabel) {
output = `${parentLabel}<${output}>`;
}
return output;
}