All files / src/commands/contract maps.ts

100% Statements 13/13
75% Branches 9/12
100% Functions 5/5
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 585687x 1x 1x   1x   16x 115x             16x     16x                               2x   16x 115x                     16x               16x    
import type {ABI} from '@wharfkit/antelope'
import * as ts from 'typescript'
import {findAbiType} from './finders'
 
export function generateTableMap(abi: ABI.Def): ts.VariableStatement {
    // Map over tables to create the object properties
    const tableProperties = abi.tables.map((table) =>
        ts.factory.createPropertyAssignment(
            JSON.stringify(table.name),
            ts.factory.createIdentifier(findAbiType(table.type, abi, 'Types.')?.type || table.type)
        )
    )
 
    // Create the TableMap structure
    const tableMap = ts.factory.createObjectLiteralExpression(tableProperties, true)
 
    // Declare the variable
    return ts.factory.createVariableStatement(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        ts.factory.createVariableDeclarationList(
            [
                ts.factory.createVariableDeclaration(
                    ts.factory.createIdentifier('TableMap'),
                    undefined,
                    undefined,
                    tableMap
                ),
            ],
            ts.NodeFlags.Const
        )
    )
}
 
export function generateTableTypesInterface(abi: ABI.Def): ts.InterfaceDeclaration {
    // Generate interface members
    const members = abi.tables.map((table) =>
        ts.factory.createPropertySignature(
            undefined,
            JSON.stringify(table.name),
            undefined,
            ts.factory.createTypeReferenceNode(
                findAbiType(table.type, abi, 'Types.')?.type || table.type
            )
        )
    )
 
    // Create the interface declaration
    const interfaceDeclaration = ts.factory.createInterfaceDeclaration(
        [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
        'TableTypes',
        undefined,
        undefined,
        members
    )
 
    return interfaceDeclaration
}