@cosmology/ast
Version:
Cosmos TypeScript AST generation
151 lines (150 loc) • 6.53 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.createEnumAminoType = exports.createAminoTypeType = exports.createAminoType = void 0;
const t = __importStar(require("@babel/types"));
const utils_1 = require("../../../utils");
const types_1 = require("../types");
const types_2 = require("../../types");
const amino_1 = require("../../amino");
const getAminoField = (context, field) => {
let ast = null;
ast = (0, types_2.getFieldAminoTypeReference)(context, field);
if (field.rule === 'repeated') {
ast = t.tsArrayType(ast);
}
if (field.keyType) {
ast = t.tsUnionType([
t.tsTypeLiteral([
t.tsIndexSignature([
(0, utils_1.identifier)('key', t.tsTypeAnnotation((0, types_2.getTSAminoType)(context, field.keyType)))
], t.tsTypeAnnotation(ast))
])
]);
}
return ast;
};
const createAminoType = (context, name, proto) => {
const oneOfs = (0, types_1.getOneOfs)(proto);
const AminoName = types_2.SymbolNames.Amino(name);
// scalar amino types!
let declaration;
if (proto.type === 'Type') {
switch (proto.name) {
case 'Duration':
case 'google.protobuf.Duration':
case 'Timestamp':
case 'google.protobuf.Timestamp':
declaration = t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(AminoName), null, t.tsStringKeyword()));
break;
default:
}
}
// declaration
if (!declaration) {
declaration = t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(AminoName), null, [], t.tsInterfaceBody(Object.keys(proto.fields).reduce((m, fieldName) => {
const isOneOf = oneOfs.includes(fieldName);
const field = proto.fields[fieldName];
let isOptional = (0, types_1.getFieldOptionalityForAmino)(context, field, isOneOf);
const orig = field.options?.['(telescope:orig)'] ?? fieldName;
// this (useOriginalCase) is always true, right?
// let fieldNameWithCase = options.useOriginalCase ? orig : fieldName;
let fieldNameWithCase = orig;
// should we actually just edit/add comments
// to make this more "native" for any google.protobuf.Any?
// let's see...
if (name === 'Any' &&
context.ref.proto.package === 'google.protobuf' &&
// options.type === 'Amino' &&
orig === 'type_url') {
// type_url => type
fieldNameWithCase = 'type';
isOptional = false;
}
let aminoField = getAminoField(context, field);
if (name === 'Any' &&
context.ref.proto.package === 'google.protobuf' &&
// options.type === 'Amino' &&
orig === 'value') {
aminoField = t.tsAnyKeyword();
isOptional = false;
}
const propSig = (0, utils_1.tsPropertySignature)(t.identifier(fieldNameWithCase), t.tsTypeAnnotation(aminoField), isOptional);
const comments = [];
if (field.comment) {
comments.push((0, utils_1.makeCommentBlock)(field.comment));
}
if (field.options?.deprecated) {
comments.push((0, utils_1.makeCommentBlock)('@deprecated'));
}
if (comments.length) {
propSig.leadingComments = comments;
}
m.push(propSig);
return m;
}, []))));
}
const comments = [];
if (proto.comment) {
comments.push((0, utils_1.makeCommentBlock)(proto.comment));
}
if (proto.options?.deprecated) {
comments.push((0, utils_1.makeCommentBlock)('@deprecated'));
}
if (comments.length) {
declaration.leadingComments = comments;
}
return declaration;
};
exports.createAminoType = createAminoType;
const createAminoTypeType = (context, name, proto) => {
const AminoName = types_2.SymbolNames.Amino(name);
const AminoTypeName = types_2.SymbolNames.AminoMsg(name);
const aminoName = (0, amino_1.getAminoTypeName)(context, context.ref.proto, proto);
const typ = aminoName ? t.tsLiteralType(t.stringLiteral(aminoName)) : t.tsTypeReference(t.identifier('string'));
let typeAnnotation = t.tsTypeAnnotation(typ);
if (name === 'Any' &&
context.ref.proto.package === 'google.protobuf') {
// replace type with plain string for this one case
typeAnnotation = t.tsTypeAnnotation(t.tsStringKeyword());
}
// scalar amino types!
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(AminoTypeName), null, [], t.tsInterfaceBody([
(0, utils_1.tsPropertySignature)(t.identifier('type'), typeAnnotation, false),
(0, utils_1.tsPropertySignature)(t.identifier('value'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(AminoName))), false)
])));
};
exports.createAminoTypeType = createAminoTypeType;
const createEnumAminoType = (context, name, proto) => {
return t.exportNamedDeclaration(t.variableDeclaration('const', [
t.variableDeclarator(t.identifier(types_2.SymbolNames.Amino(name)), t.identifier(name))
]));
// return createProtoEnum(
// context,
// name + 'Amino',
// proto
// );
};
exports.createEnumAminoType = createEnumAminoType;
;