@cosmology/ast
Version:
Cosmos TypeScript AST generation
124 lines (123 loc) • 6.04 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInterfaceToAminoHelper = exports.createInterfaceToAmino = exports.getInterfaceToAminoName = void 0;
const t = __importStar(require("@babel/types"));
const utils_1 = require("@cosmology/utils");
const utils_2 = require("../../../utils");
const firstUpper = (s) => s = s.charAt(0).toUpperCase() + s.slice(1);
const getInterfaceToAminoName = (str) => {
return firstUpper((0, utils_1.slugify)(str) + '_ToAmino');
};
exports.getInterfaceToAminoName = getInterfaceToAminoName;
const createInterfaceToAmino = (context, ref, interfaceName) => {
if (interfaceName === 'cosmos.crypto.PubKey') {
// return a helper!
context.addUtil('decodePubkey');
context.addUtil('Pubkey');
const functionName = (0, exports.getInterfaceToAminoName)(interfaceName);
return makeFunctionWrapper(context, functionName, t.returnStatement(t.callExpression(t.identifier('decodePubkey'), [
t.identifier('content')
])), t.tsTypeAnnotation(t.tsUnionType([t.tsTypeReference(t.identifier("Pubkey")), t.tsNullKeyword()])));
}
const typeMap = context.store.getTypeUrlMap(ref);
const typeRefs = typeMap[interfaceName];
return (0, exports.createInterfaceToAminoHelper)(context, (0, exports.getInterfaceToAminoName)(interfaceName), typeRefs);
};
exports.createInterfaceToAmino = createInterfaceToAmino;
const makeFunctionWrapper = (context, functionName, stmt, returnType) => {
return t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(functionName), (0, utils_2.arrowFunctionExpression)([
(0, utils_2.identifier)('content', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Any')))),
...(context.options.interfaces.enabled &&
context.options.interfaces.useUseInterfacesParams
? [
t.assignmentPattern((0, utils_2.identifier)("useInterfaces", t.tsTypeAnnotation(t.tsBooleanKeyword())), t.identifier((context.pluginValue("interfaces.useByDefault") ?? true).toString())),
]
: []),
], t.blockStatement([
stmt
]), returnType))
]));
};
const createInterfaceToAminoHelper = (context, functionName, typeRefs) => {
// MARKED AS NOT DRY
const allTypes = typeRefs?.reduce((m, typeRef) => {
return [...m, ...typeRef.types];
}, []) ?? [];
const switchCases = allTypes.map(type => {
if (!type.aminoType)
return;
return t.switchCase(t.stringLiteral(type.typeUrl), [
// return
t.returnStatement(t.objectExpression([
t.objectProperty(t.identifier('type'), t.stringLiteral(type.aminoType)),
t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier(type.importAs), t.identifier('toAmino')), [
t.callExpression(t.memberExpression(t.identifier(type.importAs), t.identifier('decode')), [
t.memberExpression(t.identifier('content'), t.identifier('value')),
t.identifier('undefined'),
...(context.options.interfaces.enabled &&
context.options.interfaces.useUseInterfacesParams
? [
t.identifier('useInterfaces')
]
: []),
]),
...(context.options.interfaces.enabled &&
context.options.interfaces.useUseInterfacesParams
? [
t.identifier('useInterfaces')
]
: []),
]))
]))
]);
}).filter(Boolean);
let ast;
if (!switchCases.length) {
ast = t.returnStatement(t.callExpression(t.memberExpression(t.identifier('Any'), t.identifier('toAmino')), [
t.identifier('content'),
...(context.options.interfaces.enabled && context.options.interfaces.useUseInterfacesParams ? [
t.identifier('useInterfaces')
] : []),
]));
}
else {
ast = t.switchStatement(t.memberExpression(t.identifier('content'), t.identifier('typeUrl')), [
...switchCases,
// default
t.switchCase(null, [
t.returnStatement(t.callExpression(t.memberExpression(t.identifier('Any'), t.identifier('toAmino')), [
t.identifier('content'),
...(context.options.interfaces.enabled && context.options.interfaces.useUseInterfacesParams ? [
t.identifier('useInterfaces')
] : []),
]))
])
]);
}
return makeFunctionWrapper(context, functionName, ast);
};
exports.createInterfaceToAminoHelper = createInterfaceToAminoHelper;
;