@cosmology/ast
Version:
Cosmos TypeScript AST generation
56 lines (55 loc) • 3.19 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.createInstantRpcClass = exports.createInstantRpcInterface = void 0;
const t = __importStar(require("@babel/types"));
const utils_1 = require("../../../utils");
const createInstantRpcInterface = (name, extendInterfaces) => {
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(name), null, extendInterfaces.map((item) => t.tsExpressionWithTypeArguments(t.identifier(`${item.importedVarName}.${item.interfaceName}`))), t.tsInterfaceBody([])));
};
exports.createInstantRpcInterface = createInstantRpcInterface;
const createInstantRpcClass = (context, name, instantMapping) => {
const useTelescopeGeneratedType = context.pluginValue("prototypes.typingsFormat.useTelescopeGeneratedType");
if (useTelescopeGeneratedType) {
context.addUtil("TxRpc");
}
else {
context.addUtil("Rpc");
}
return t.exportNamedDeclaration(t.classDeclaration(t.identifier(name), null, t.classBody([
(0, utils_1.classProperty)(t.identifier("rpc"), null, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(useTelescopeGeneratedType ? "TxRpc" : "Rpc"))), null, false, false),
// Constructor
t.classMethod("method", t.identifier("init"), [
(0, utils_1.identifier)("rpc", t.tsTypeAnnotation(t.tsTypeReference(t.identifier(useTelescopeGeneratedType ? "TxRpc" : "Rpc")))),
], t.blockStatement([
t.expressionStatement(t.assignmentExpression("=", t.memberExpression(t.thisExpression(), t.identifier("rpc")), t.identifier("rpc"))),
...Object.keys(instantMapping).map((methodAlias) => {
const mapping = instantMapping[methodAlias];
return t.expressionStatement(t.assignmentExpression("=", t.memberExpression(t.thisExpression(), t.identifier(methodAlias)), t.memberExpression(t.callExpression(t.memberExpression(t.identifier(mapping.importedVarName), t.identifier("createClientImpl")), [t.identifier("rpc")]), t.identifier(mapping.methodName))));
}),
]), false),
]), []));
};
exports.createInstantRpcClass = createInstantRpcClass;
;