@cosmology/ast
Version:
Cosmos TypeScript AST generation
65 lines (64 loc) • 3.17 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.toProtoMethod = exports.toProtoMsgMethod = void 0;
const t = __importStar(require("@babel/types"));
const utils_1 = require("../../../utils");
const types_1 = require("../../types");
const amino_1 = require("../../amino");
const toProtoMsgMethod = (context, name, proto) => {
const varName = 'message';
const ReturnType = types_1.SymbolNames.ProtoMsg(name);
const TypeName = types_1.SymbolNames.Msg(name);
const typeUrl = (0, amino_1.getTypeUrl)(context.ref.proto, proto);
if (!typeUrl)
return;
const body = [];
// body
body.push(t.returnStatement(t.objectExpression([
t.objectProperty(t.identifier('typeUrl'), t.stringLiteral(typeUrl)),
t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(TypeName), t.identifier('encode')), [
t.identifier(varName)
]), t.identifier('finish')), []))
])));
return (0, utils_1.objectMethod)('method', t.identifier('toProtoMsg'), [
(0, utils_1.identifier)(varName, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName))))
], t.blockStatement(body), false, false, false, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ReturnType))));
};
exports.toProtoMsgMethod = toProtoMsgMethod;
const toProtoMethod = (context, name, proto) => {
const varName = 'message';
const ReturnType = 'Uint8Array';
const TypeName = types_1.SymbolNames.Msg(name);
const body = [];
// body
body.push(t.returnStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(TypeName), t.identifier('encode')), [
t.identifier('message')
]), t.identifier('finish')), [])));
return (0, utils_1.objectMethod)('method', t.identifier('toProto'), [
(0, utils_1.identifier)(varName, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName))))
], t.blockStatement(body), false, false, false, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ReturnType))));
};
exports.toProtoMethod = toProtoMethod;
;