@cosmology/ast
Version:
Cosmos TypeScript AST generation
124 lines (123 loc) • 5.87 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.aminoInterface = void 0;
const t = __importStar(require("@babel/types"));
const utils_1 = require("../utils");
const utils_2 = require("../utils");
const types_1 = require("../../types");
const proto_1 = require("../../proto");
const _1 = require(".");
exports.aminoInterface = {
defaultType(args) {
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation((0, types_1.getTSAminoType)(args.context, args.field.type, args.field.options)));
},
string(args) {
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation(t.tsStringKeyword()));
},
long(args) {
// longs become strings...
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation(t.tSStringKeyword()));
},
height(args) {
args.context.addUtil('AminoHeight');
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('AminoHeight'))));
},
duration(args) {
const durationFormat = args.context.pluginValue('prototypes.typingsFormat.duration');
switch (durationFormat) {
case 'string':
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation(t.tsStringKeyword()));
case 'duration':
default:
return exports.aminoInterface.type(args);
}
},
timestamp(args) {
return exports.aminoInterface.string(args);
},
enum(args) {
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation(t.tSNumberKeyword()));
},
pubkey(args) {
args.context.addUtil('Pubkey');
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Pubkey'))));
},
enumArray(args) {
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation((0, utils_1.arrayTypeNDim)(t.tSNumberKeyword(), 1)));
},
type({ context, field, currentProtoPath, isOptional }) {
const parentField = field;
const Type = context.getTypeFromCurrentPath(field, currentProtoPath);
const oneOfs = (0, proto_1.getOneOfs)(Type);
const properties = (0, utils_2.protoFieldsToArray)(Type).map(field => {
const isOneOf = oneOfs.includes(field.name);
const isOptional = (0, proto_1.getFieldOptionality)(context, field, isOneOf);
// TODO how to handle isOptional from parent to child...
if (parentField.import)
currentProtoPath = parentField.import;
return (0, _1.renderAminoField)({
context,
field,
currentProtoPath,
isOptional // TODO how to handle nested optionality
});
});
//
return t.tsPropertySignature(t.identifier(context.aminoCaseField(field)), t.tsTypeAnnotation(t.tsTypeLiteral(properties)));
},
typeArray({ context, field, currentProtoPath, isOptional }) {
const parentField = field;
const Type = context.getTypeFromCurrentPath(field, currentProtoPath);
// TODO how to handle isOptional from parent to child...
const oneOfs = (0, proto_1.getOneOfs)(Type);
const properties = (0, utils_2.protoFieldsToArray)(Type).map(field => {
const isOneOf = oneOfs.includes(field.name);
const isOptional = (0, proto_1.getFieldOptionality)(context, field, isOneOf);
if (parentField.import)
currentProtoPath = parentField.import;
return (0, _1.renderAminoField)({
context,
field,
currentProtoPath,
isOptional // TODO how to handle nested optionality
});
});
//
return t.tsPropertySignature(t.identifier(context.aminoCaseField(field)), t.tsTypeAnnotation((0, utils_1.arrayTypeNDim)(t.tsTypeLiteral(properties), 1)));
},
array(args) {
// TODO write test case
// return t.tsPropertySignature(
// t.identifier(options.aminoCasingFn(field.name)),
// t.tsTypeAnnotation(
// arrayTypeNDim(t.tsTypeLiteral(
// properties
// ), 1)
// )
// );
return t.tsPropertySignature(t.identifier(args.context.aminoCaseField(args.field)), t.tsTypeAnnotation((0, utils_1.arrayTypeNDim)((0, types_1.getTSAminoType)(args.context, args.field.type, args.field.options), 1)));
}
};
;