UNPKG

@cosmology/ast

Version:
104 lines (103 loc) 5.8 kB
"use strict"; 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.createHelperObject = exports.addEncodedMethod = exports.addJsonMethod = exports.addToJSONMethod = exports.addFromPartialMethod = exports.addFromJSONMethod = exports.addMsgMethod = void 0; const t = __importStar(require("@babel/types")); const utils_1 = require("../../utils"); const utils_2 = require("@cosmology/utils"); const addMsgMethod = ({ methodName, typeUrl, TypeName, methodToCall }) => { return t.objectMethod('method', t.identifier(methodName), [ (0, utils_1.identifier)('value', t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName)))) ], t.blockStatement([ t.returnStatement(t.objectExpression([ t.objectProperty(t.identifier('typeUrl'), t.stringLiteral(typeUrl)), t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier(TypeName), t.identifier(methodToCall)), [t.identifier('value')])) ])) ])); }; exports.addMsgMethod = addMsgMethod; const addFromJSONMethod = ({ methodName, typeUrl, TypeName }) => { return t.objectMethod('method', t.identifier(methodName), [ (0, utils_1.identifier)('value', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('any')))) ], t.blockStatement([ t.returnStatement(t.objectExpression([ t.objectProperty(t.identifier('typeUrl'), t.stringLiteral(typeUrl)), t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier(TypeName), t.identifier('fromJSON')), [t.identifier('value')])) ])) ])); }; exports.addFromJSONMethod = addFromJSONMethod; const addFromPartialMethod = ({ methodName, typeUrl, TypeName }) => { return (0, exports.addMsgMethod)({ methodName, typeUrl, TypeName, methodToCall: 'fromPartial' }); }; exports.addFromPartialMethod = addFromPartialMethod; const addToJSONMethod = ({ methodName, typeUrl, TypeName }) => { return (0, exports.addMsgMethod)({ methodName, typeUrl, TypeName, methodToCall: 'toJSON' }); }; exports.addToJSONMethod = addToJSONMethod; const addJsonMethod = ({ methodName, typeUrl, TypeName }) => { return t.objectMethod('method', t.identifier(methodName), [ (0, utils_1.identifier)('value', t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName)))) ], t.blockStatement([ t.returnStatement(t.objectExpression([ t.objectProperty(t.identifier('typeUrl'), t.stringLiteral(typeUrl)), t.objectProperty(t.identifier('value'), t.identifier('value'), false, true) ])) ])); }; exports.addJsonMethod = addJsonMethod; const addEncodedMethod = ({ methodName, typeUrl, TypeName }) => { return t.objectMethod('method', t.identifier(methodName), [ (0, utils_1.identifier)('value', t.tsTypeAnnotation(t.tsTypeReference(t.identifier(TypeName)))) ], t.blockStatement([ 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('value') ]), t.identifier('finish')), [])) ])) ])); }; exports.addEncodedMethod = addEncodedMethod; const createHelperObject = ({ context, // for future plugins name, mutations }) => { mutations = mutations.map(mutation => { return { ...mutation, methodName: (0, utils_2.camel)(mutation.methodName) }; }); const methods = [ context.pluginValue('prototypes.methods.encode') && (t.objectProperty(t.identifier('encoded'), t.objectExpression(mutations.map(mutation => (0, exports.addEncodedMethod)(mutation))))), t.objectProperty(t.identifier('withTypeUrl'), t.objectExpression(mutations.map(mutation => (0, exports.addJsonMethod)(mutation)))), context.pluginValue('prototypes.methods.toJSON') && (t.objectProperty(t.identifier('toJSON'), t.objectExpression(mutations.map(mutation => (0, exports.addToJSONMethod)(mutation))))), context.pluginValue('prototypes.methods.fromJSON') && (t.objectProperty(t.identifier('fromJSON'), t.objectExpression(mutations.map(mutation => (0, exports.addFromJSONMethod)(mutation))))), context.pluginValue('prototypes.methods.fromPartial') && (t.objectProperty(t.identifier('fromPartial'), t.objectExpression(mutations.map(mutation => (0, exports.addFromPartialMethod)(mutation))))) ].filter(Boolean); return t.exportNamedDeclaration(t.variableDeclaration('const', [ t.variableDeclarator(t.identifier(name), t.objectExpression(methods)) ])); }; exports.createHelperObject = createHelperObject;