@cosmology/ast
Version:
Cosmos TypeScript AST generation
83 lines (82 loc) • 6.86 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.createObjectWithMethods = void 0;
const t = __importStar(require("@babel/types"));
const from_partial_1 = require("./proto/from-partial");
const decode_1 = require("./proto/decode");
const encode_1 = require("./proto/encode");
const from_json_1 = require("./proto/from-json");
const to_json_1 = require("./proto/to-json");
const from_amino_1 = require("./proto/from-amino");
const to_amino_1 = require("./proto/to-amino");
const to_sdk_1 = require("./proto/to-sdk");
const from_sdk_1 = require("./proto/from-sdk");
const proto_1 = require("./proto");
const is_1 = require("./proto/is");
const utils_1 = require("../utils");
const createObjectWithMethods = (context, name, proto) => {
const isVNext = context.pluginValue('env') === 'v-next';
const methodsAndProps = [
(context.pluginValue('prototypes.addTypeUrlToObjects') || context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry')) && (0, proto_1.createTypeUrlProperty)(context, proto),
(context.pluginValue('prototypes.addAminoTypeToObjects') || context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry')) && (0, proto_1.createAminoTypeProperty)(context, proto),
context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry') && (0, is_1.isMethod)({ context, name, proto }),
context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry') && context.pluginValue('useSDKTypes') && (0, is_1.isMethod)({ context, name, proto, methodName: "isSDK", getFieldName: utils_1.getSdkFieldName }),
context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry') && context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') && (0, is_1.isMethod)({ context, name, proto, methodName: "isAmino", getFieldName: utils_1.getAminoFieldName }),
context.pluginValue('prototypes.methods.encode') && (0, encode_1.encodeMethod)(context, name, proto),
context.pluginValue('prototypes.methods.decode') && (0, decode_1.decodeMethod)(context, name, proto),
context.pluginValue('prototypes.methods.fromJSON') && (0, from_json_1.fromJSONMethod)(context, name, proto),
context.pluginValue('prototypes.methods.toJSON') && (0, to_json_1.toJSONMethod)(context, name, proto),
context.pluginValue('prototypes.methods.fromPartial') && (0, from_partial_1.fromPartialMethod)(context, name, proto),
context.pluginValue('prototypes.methods.fromSDK') && (0, from_sdk_1.fromSDKMethod)(context, name, proto),
context.pluginValue('prototypes.methods.fromSDKJSON') && (0, proto_1.fromSDKJSONMethod)(context, name, proto),
context.pluginValue('prototypes.methods.toSDK') && (0, to_sdk_1.toSDKMethod)(context, name, proto),
(context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.fromAmino')) && (0, from_amino_1.fromAminoJSONMethod)(context, name, proto),
(context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toAmino')) && (0, to_amino_1.toAminoJSONMethod)(context, name, proto),
!context.pluginValue('aminoEncoding.disableMsgTypes') && (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.fromAmino')) && (0, from_amino_1.fromAminoMsgMethod)(context, name, proto),
!context.pluginValue('aminoEncoding.disableMsgTypes') && (context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toAmino')) && (0, to_amino_1.toAminoMsgMethod)(context, name, proto),
(context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.fromProto')) && (0, proto_1.fromProtoMsgMethod)(context, name, proto),
(context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toProto')) && (0, proto_1.toProtoMethod)(context, name, proto),
(context.pluginValue('aminoEncoding.enabled') && !context.pluginValue('aminoEncoding.useLegacyInlineEncoding') || context.pluginValue('prototypes.methods.toProto')) && (0, proto_1.toProtoMsgMethod)(context, name, proto),
context.pluginValue('interfaces.enabled') && context.pluginValue('interfaces.useGlobalDecoderRegistry') && context.pluginValue('helperFunctions.enabled') && context.pluginValue('helperFunctions.useGlobalDecoderRegistry') && (0, proto_1.registerTypeUrlMethod)({ context, name, proto }),
].filter(Boolean);
const declaration = t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(name), t.objectExpression(methodsAndProps))
]));
if (isVNext) {
const commentBlock = new utils_1.CommentBlockBuilder()
.addLine(proto.comment)
.addLine(`@name ${name}`)
.addLine(`@package ${context.ref.proto.package}`)
.addLine(`@see proto type: ${context.ref.proto.package}.${proto.originalName ?? proto.name}`)
.addLine(proto.options?.deprecated ? '@deprecated' : null)
.build();
if (commentBlock) {
declaration.leadingComments = [commentBlock];
}
}
return declaration;
};
exports.createObjectWithMethods = createObjectWithMethods;