@cosmology/ast
Version:
Cosmos TypeScript AST generation
115 lines (114 loc) • 5.39 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.createInterfaceDecoderHelper = exports.createInterfaceDecoder = exports.getInterfaceDecoderName = void 0;
const t = __importStar(require("@babel/types"));
const utils_1 = require("@cosmology/utils");
const utils_2 = require("../../../utils");
const binary_coder_expression_1 = require("../../../utils/binary-coder-expression");
const getMapFromTypeUrlMap = (urlMap, name) => {
return (urlMap?.[name]?.reduce((m, v) => {
v.types.forEach((type) => {
m[type.importAs] = type.typeUrl;
});
return m;
}, {}) ?? {});
};
const firstUpper = (s) => (s = s.charAt(0).toUpperCase() + s.slice(1));
const getInterfaceDecoderName = (str) => {
return firstUpper((0, utils_1.slugify)(str) + '_InterfaceDecoder');
};
exports.getInterfaceDecoderName = getInterfaceDecoderName;
const createInterfaceDecoder = (context, ref, interfaceName) => {
const typeMap = context.store.getTypeUrlMap(ref);
const typeRefs = typeMap[interfaceName];
return (0, exports.createInterfaceDecoderHelper)(context, (0, exports.getInterfaceDecoderName)(interfaceName), typeRefs);
};
exports.createInterfaceDecoder = createInterfaceDecoder;
const createInterfaceDecoderHelper = (context, functionName, typeRefs) => {
let useUseInterfacesParams = context.pluginValue("interfaces.useUseInterfacesParams");
binary_coder_expression_1.BinaryCoder.addUtil(context);
// MARKED AS NOT DRY
const allTypes = typeRefs?.reduce((m, typeRef) => {
return [...m, ...typeRef.types];
}, []) ?? [];
const returnTypes = allTypes.map((type) => type.importAs);
const decodeMessages = allTypes.map((type) => type.typeUrl);
const switches = returnTypes.map((returnType, i) => {
let params = [
t.memberExpression(t.identifier('data'), t.identifier('value')),
];
if (useUseInterfacesParams) {
params.push(t.identifier('undefined'));
params.push(t.booleanLiteral(true));
}
return t.switchCase(t.stringLiteral(decodeMessages[i]), [
t.returnStatement(t.callExpression(t.memberExpression(t.identifier(returnType), t.identifier('decode')), params))
]);
});
let decodeParams = [
t.identifier('reader'),
t.callExpression(t.memberExpression(t.identifier('reader'), t.identifier('uint32') // NOTE is it always uint32?
), [])
];
if (useUseInterfacesParams) {
decodeParams.push(t.booleanLiteral(true));
}
return t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(functionName), (0, utils_2.arrowFunctionExpression)([
(0, utils_2.identifier)('input', t.tsTypeAnnotation(t.tsUnionType([
binary_coder_expression_1.BinaryCoder.getReaderTypeRef(context),
t.tsTypeReference(t.identifier('Uint8Array'))
])), false)
],
// body
t.blockStatement([
/// READER
t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('reader'), t.conditionalExpression(t.binaryExpression('instanceof', t.identifier('input'), binary_coder_expression_1.BinaryCoder.getReaderMemberExp(context)), t.identifier('input'), t.newExpression(binary_coder_expression_1.BinaryCoder.getReaderMemberExp(context), [
t.identifier('input')
])))
]),
// DATA
t.variableDeclaration('const', [
t.variableDeclarator(t.identifier('data'), t.callExpression(t.memberExpression(t.identifier('Any'), t.identifier('decode')), decodeParams))
]),
// SWITCH
t.switchStatement(t.memberExpression(t.identifier('data'), t.identifier('typeUrl')), [
...switches,
/////
t.switchCase(null, [t.returnStatement(t.identifier('data'))])
])
]),
// return type
t.tsTypeAnnotation(
// do we need to use interfaces.useUnionTypes here ?
t.tsUnionType([
...returnTypes.map((type) => t.tsTypeReference(t.identifier(type))),
t.tsTypeReference(t.identifier('Any'))
]))))
]));
};
exports.createInterfaceDecoderHelper = createInterfaceDecoderHelper;
;