@cosmology/ast
Version:
Cosmos TypeScript AST generation
549 lines (546 loc) • 26.8 kB
JavaScript
import * as t from "@babel/types";
import { getTagNumber } from "../types";
import { getKeyTypeEntryName } from "..";
import { getDefaultTSTypeFromProtoType } from "../../types";
import { TypeLong } from "../../../utils";
const notUndefined = (prop) => {
return t.binaryExpression("!==", t.memberExpression(t.identifier("message"), t.identifier(prop)), t.identifier("undefined"));
};
const ifNotUndefined = (prop, stmt) => {
return t.ifStatement(notUndefined(prop), t.blockStatement([stmt]));
};
const notEmptyString = (prop) => {
return t.binaryExpression("!==", t.memberExpression(t.identifier("message"), t.identifier(prop)), t.stringLiteral(""));
};
const nullCheckAndCondition = (inputProp, inputExpression) => {
return t.logicalExpression("&&", t.memberExpression(t.identifier("message"), t.identifier(inputProp)), inputExpression);
};
const lengthNotZero = (prop) => {
return t.binaryExpression("!==", t.memberExpression(t.memberExpression(t.identifier("message"), t.identifier(prop)), t.identifier("length")), t.numericLiteral(0));
};
const ifTrue = (prop) => {
return t.binaryExpression("===", t.memberExpression(t.identifier("message"), t.identifier(prop)), t.booleanLiteral(true));
};
const notZero = (prop) => {
return t.binaryExpression("!==", t.memberExpression(t.identifier("message"), t.identifier(prop)), t.numericLiteral(0));
};
// TODO research, shouldn't we AND these two tests?
const wrapOptional = (prop, test, isOptional, args) => {
if (isOptional ||
args?.context?.options?.prototypes?.allowEncodeDefaultScalars) {
return notUndefined(prop);
}
return test;
};
const scalarType = (num, prop, type, args) => {
let valueExpression = t.memberExpression(t.identifier("message"), t.identifier(prop));
switch (type) {
case "int64":
case "sint64":
case "uint64":
case "fixed64":
case "sfixed64":
TypeLong.addUtil(args.context);
break;
}
return t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier(type)), [valueExpression])),
]);
};
const customType = (num, prop, type, customType, args) => {
const goPackage = args.context.ref.proto.options?.["go_package"];
if (goPackage == "github.com/cosmos/cosmos-sdk/types" &&
customType == "Dec") {
customType = `${goPackage}.Dec`;
}
switch (customType) {
case "github.com/cosmos/cosmos-sdk/types.Dec":
case "cosmossdk.io/math.LegacyDec":
default:
args.context.addUtil("Decimal");
return t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier(type)), [
t.memberExpression(t.callExpression(t.memberExpression(t.identifier("Decimal"), t.identifier("fromUserInput")), [
t.memberExpression(t.identifier("message"), t.identifier(prop)),
t.numericLiteral(18),
]), t.identifier("atomics")),
])),
]);
}
};
export const encode = {
string(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.string(num, prop, args.isOptional, args);
},
double(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.double(num, prop, args.isOptional, args);
},
float(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.float(num, prop, args.isOptional, args);
},
int32(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.int32(num, prop, args.isOptional, args);
},
sint32(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.sint32(num, prop, args.isOptional, args);
},
uint32(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.uint32(num, prop, args.isOptional, args);
},
fixed32(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.fixed32(num, prop, args.isOptional, args);
},
sfixed32(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.sfixed32(num, prop, args.isOptional, args);
},
int64(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.int64(num, prop, args.isOptional, args);
},
sint64(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.sint64(num, prop, args.isOptional, args);
},
uint64(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.uint64(num, prop, args.isOptional, args);
},
fixed64(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.fixed64(num, prop, args.isOptional, args);
},
sfixed64(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.sfixed64(num, prop, args.isOptional, args);
},
bool(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.bool(num, prop, args.isOptional, args);
},
type(args) {
const prop = args.field.name;
const name = args.context.getTypeName(args.field);
const num = getTagNumber(args.field);
let isAnyType = false;
if (!args.context.options.aminoEncoding.useLegacyInlineEncoding &&
args.context.options.interfaces.enabled &&
args.field.type === "google.protobuf.Any" &&
args.field.options["(cosmos_proto.accepts_interface)"]) {
isAnyType = true;
}
const isGlobalRegistry = args.context.options.interfaces?.enabled &&
args.context.options.interfaces?.useGlobalDecoderRegistry;
if (isGlobalRegistry) {
args.context.addUtil("GlobalDecoderRegistry");
}
return types.type(num, prop, name, isAnyType, isGlobalRegistry);
},
enum(args) {
const num = getTagNumber(args.field);
return types.enum(args.context, num, args.field, args.isOptional, args.isOneOf);
},
bytes(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.bytes(num, prop, args.isOptional);
},
timestamp(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
const timestampFormat = args.context.pluginValue("prototypes.typingsFormat.timestamp");
switch (timestampFormat) {
case "timestamp":
return types.timestamp(num, prop);
case "date":
default:
args.context.addUtil("toTimestamp");
return types.timestampDate(num, prop);
}
},
duration(args) {
const prop = args.field.name;
const num = getTagNumber(args.field);
const durationFormat = args.context.pluginValue("prototypes.typingsFormat.duration");
switch (durationFormat) {
case "string":
args.context.addUtil("toDuration");
return types.duration(num, prop);
case "duration":
default:
return encode.type(args);
}
},
forkDelimArray(args, expr) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.forkDelimArray(num, prop, expr);
},
array(args, expr) {
const prop = args.field.name;
const num = getTagNumber(args.field);
return types.array(num, prop, expr);
},
typeArray(args) {
const prop = args.field.name;
const name = args.context.getTypeName(args.field);
const num = getTagNumber(args.field);
let isAnyType = false;
if (!args.context.options.aminoEncoding.useLegacyInlineEncoding &&
args.context.options.interfaces.enabled &&
args.field.type === "google.protobuf.Any" &&
args.field.options["(cosmos_proto.accepts_interface)"]) {
isAnyType = true;
}
const isGlobalRegistry = args.context.options.interfaces?.enabled &&
args.context.options.interfaces?.useGlobalDecoderRegistry;
return types.typeArray(num, prop, name, isAnyType, isGlobalRegistry);
},
keyHash(args) {
const prop = args.field.name;
const name = args.typeName;
const num = getTagNumber(args.field);
return types.keyHash(num, prop, name);
},
};
export const types = {
/*
if (message.sender && message.sender !== "") {
writer.uint32(10).string(message.sender);
}
*/
string(num, prop, isOptional, args) {
const useCosmosSDKDec = args.context.pluginValue("prototypes.typingsFormat.customTypes.useCosmosSDKDec");
const goPackage = args.context.ref.proto.options?.["go_package"];
const isCosmosSDKDec = (goPackage == "github.com/cosmos/cosmos-sdk/types" &&
args.field.options?.["(gogoproto.customtype)"] == "Dec") ||
args.field.options?.["(gogoproto.customtype)"] ==
"github.com/cosmos/cosmos-sdk/types.Dec" ||
args.field.options?.["(gogoproto.customtype)"] ==
"cosmossdk.io/math.LegacyDec";
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notEmptyString(prop))
: notEmptyString(prop), isOptional, args), useCosmosSDKDec && isCosmosSDKDec
? customType(num, prop, "string", args.field.options?.["(gogoproto.customtype)"], args)
: scalarType(num, prop, "string"));
},
/*
if (message.doubleValue && message.doubleValue !== 0) {
writer.uint32(41).double(message.doubleValue);
}
*/
double(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notZero(prop))
: notZero(prop), isOptional, args), scalarType(num, prop, "double"));
},
/*
if (message.floatValue && message.floatValue !== 0) {
writer.uint32(41).float(message.floatValue);
}
*/
float(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notZero(prop))
: notZero(prop), isOptional, args), scalarType(num, prop, "float"));
},
// if (message.int32Value && message.int32Value !== 0) {
// writer.uint32(24).int32(message.int32Value);
// }
int32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notZero(prop))
: notZero(prop), isOptional, args), scalarType(num, prop, "int32"));
},
// if (message.sint32Value && message.sint32Value !== 0) {
// writer.uint32(24).sint32(message.sint32Value);
// }
sint32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notZero(prop))
: notZero(prop), isOptional, args), scalarType(num, prop, "sint32"));
},
// if (message.int32Value && message.int32Value !== 0) {
// writer.uint32(24).uint32(message.int32Value);
// }
uint32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notZero(prop))
: notZero(prop), isOptional, args), scalarType(num, prop, "uint32"));
},
fixed32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notZero(prop))
: notZero(prop), isOptional, args), scalarType(num, prop, "fixed32"));
},
sfixed32(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, notZero(prop))
: notZero(prop), isOptional, args), scalarType(num, prop, "sfixed32"));
},
// if (message.int64Value && !message.int64Value.isZero()) {
// writer.uint32(24).int64(message.int64Value);
// }
int64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, TypeLong.getLongNotZero(prop, args.context))
: TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, "int64", args));
},
// if (message.sint64Value && !message.sint64Value.isZero()) {
// writer.uint32(24).sint64(message.sint64Value);
// }
sint64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, TypeLong.getLongNotZero(prop, args.context))
: TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, "sint64", args));
},
// if (message.int64Value && !message.int64Value.isZero()) {
// writer.uint32(24).uint64(message.int64Value);
// }
uint64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, TypeLong.getLongNotZero(prop, args.context))
: TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, "uint64", args));
},
fixed64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, TypeLong.getLongNotZero(prop, args.context))
: TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, "fixed64", args));
},
sfixed64(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, args.context.pluginValue("prototypes.enforceNullCheck")
? nullCheckAndCondition(prop, TypeLong.getLongNotZero(prop, args.context))
: TypeLong.getLongNotZero(prop, args.context), isOptional, args), scalarType(num, prop, "sfixed64", args));
},
// if (message.disableMacros === true) {
// writer.uint32(32).bool(message.disableMacros);
// }
bool(num, prop, isOptional, args) {
return t.ifStatement(wrapOptional(prop, ifTrue(prop), isOptional, args), scalarType(num, prop, "bool"));
},
type(num, prop, name, isAnyType, isGlobalRegistry) {
let messageProp = t.memberExpression(t.identifier("message"), t.identifier(prop));
if (isAnyType) {
if (isGlobalRegistry) {
messageProp = t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("wrapAny")), [messageProp]);
}
else {
messageProp = t.tsAsExpression(messageProp, t.tsTypeReference(t.identifier("Any")));
}
}
return t.ifStatement(notUndefined(prop), t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(name), t.identifier("encode")), [
messageProp,
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("fork")), []),
]), t.identifier("ldelim")), [])),
]));
},
// if (message.singleField !== 0) {
// writer.uint32(24).int32(message.singleField);
// }
enum(context, num, field, isOptional, isOneOf) {
const prop = field.name;
return t.ifStatement(wrapOptional(prop, t.binaryExpression("!==", t.memberExpression(t.identifier("message"), t.identifier(field.name)), getDefaultTSTypeFromProtoType(context, field, isOneOf)), isOptional), scalarType(num, prop, "int32"));
},
/*
if (message.queryData.length !== 0) {
writer.uint32(18).bytes(message.queryData);
}
*/
bytes(num, prop, isOptional) {
return t.ifStatement(wrapOptional(prop, lengthNotZero(prop), isOptional), scalarType(num, prop, "bytes"));
},
// if (message.periodReset !== undefined) {
// Timestamp.encode(toTimestamp(message.periodReset), writer.uint32(18).fork()).ldelim();
// }
timestamp(num, prop) {
return ifNotUndefined(prop, t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("Timestamp"), t.identifier("encode")), [
t.memberExpression(t.identifier("message"), t.identifier(prop)),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("fork")), []),
]), t.identifier("ldelim")), [])));
},
timestampDate(num, prop) {
return ifNotUndefined(prop, t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("Timestamp"), t.identifier("encode")), [
t.callExpression(t.identifier("toTimestamp"), [
t.memberExpression(t.identifier("message"), t.identifier(prop)),
]),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("fork")), []),
]), t.identifier("ldelim")), [])));
},
// if (message.period !== undefined) {
// Duration.encode(toDuration(message.period), writer.uint32(18).fork()).ldelim();
// }
duration(num, prop) {
return t.ifStatement(notUndefined(prop), t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("Duration"), t.identifier("encode")), [
t.callExpression(t.identifier("toDuration"), [
t.memberExpression(t.identifier("message"), t.identifier(prop)),
]),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("fork")), []),
]), t.identifier("ldelim")), [])),
]));
},
/*
writer.uint32(10).fork();
for (const v of message.codeIds) {
writer.uint64(v);
}
writer.ldelim();
*/
forkDelimArray(num, prop, expr) {
return [
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("fork")), [])),
t.forOfStatement(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("v"), null),
]), t.memberExpression(t.identifier("message"), t.identifier(prop)), t.blockStatement([expr])),
t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("ldelim")), [])),
];
},
array(num, prop, expr) {
return [
t.forOfStatement(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("v"), null),
]), t.memberExpression(t.identifier("message"), t.identifier(prop)), t.blockStatement([expr])),
];
},
typeArray(num, prop, name, isAnyType, isGlobalRegistry) {
// "v!" just means it's NOT NULLABLE
let nestedProp = t.tsNonNullExpression(t.identifier("v"));
if (isAnyType) {
if (isGlobalRegistry) {
nestedProp = t.callExpression(t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("wrapAny")), [nestedProp]);
}
else {
nestedProp = t.tsAsExpression(nestedProp, t.tsTypeReference(t.identifier("Any")));
}
}
return [
t.forOfStatement(t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("v"), null),
]), t.memberExpression(t.identifier("message"), t.identifier(prop)), t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(name), t.identifier("encode")), [
nestedProp,
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("fork")), []),
]), t.identifier("ldelim")), [])),
])),
];
},
// Object.entries(message.labels).forEach(([key, value]) => {
// LogEntry_LabelsEntry.encode({
// key: (key as any),
// value
// }, writer.uint32(106).fork()).ldelim();
// });
keyHash(num, prop, name) {
return t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("entries")), [
t.memberExpression(t.identifier("message"), t.identifier(prop)),
]), t.identifier("forEach")), [
t.arrowFunctionExpression([
t.arrayPattern([
t.identifier("key"),
t.identifier("value"),
]),
], t.blockStatement([
t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(getKeyTypeEntryName(name, prop)), t.identifier("encode")), [
t.objectExpression([
t.objectProperty(t.identifier("key"), t.tsAsExpression(t.identifier("key"), t.tsAnyKeyword())),
t.objectProperty(t.identifier("value"), t.identifier("value"), false, true),
]),
t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [
t.numericLiteral(num),
]), t.identifier("fork")), []),
]), t.identifier("ldelim")), [])),
])),
]));
},
};
export const arrayTypes = {
double() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("double")), [t.identifier("v")]));
},
bool() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("bool")), [t.identifier("v")]));
},
float() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("float")), [t.identifier("v")]));
},
int32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("int32")), [t.identifier("v")]));
},
sint32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("sint32")), [t.identifier("v")]));
},
uint32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.identifier("v")]));
},
fixed32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("fixed32")), [t.identifier("v")]));
},
sfixed32() {
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("sfixed32")), [t.identifier("v")]));
},
long(type, args) {
let valueExpression = t.identifier("v");
return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier(type)), [valueExpression]));
},
int64(args) {
return arrayTypes.long("int64", args);
},
sint64(args) {
return arrayTypes.long("sint64", args);
},
uint64(args) {
return arrayTypes.long("uint64", args);
},
fixed64(args) {
return arrayTypes.long("fixed64", args);
},
sfixed64(args) {
return arrayTypes.long("sfixed64", args);
},
string(args) {
const useCosmosSDKDec = args.context.pluginValue("prototypes.typingsFormat.customTypes.useCosmosSDKDec");
const goPackage = args.context.ref.proto.options?.["go_package"];
const isCosmosSDKDec = (goPackage == "github.com/cosmos/cosmos-sdk/types" &&
args.field.options?.["(gogoproto.customtype)"] == "Dec") ||
args.field.options?.["(gogoproto.customtype)"] ==
"github.com/cosmos/cosmos-sdk/types.Dec" ||
args.field.options?.["(gogoproto.customtype)"] ==
"cosmossdk.io/math.LegacyDec";
const num = getTagNumber(args.field);
let valueExpression = t.tsNonNullExpression(t.identifier("v"));
if (useCosmosSDKDec && isCosmosSDKDec) {
args.context.addUtil("Decimal");
valueExpression = t.memberExpression(t.callExpression(t.memberExpression(t.identifier("Decimal"), t.identifier("fromUserInput")), [valueExpression, t.numericLiteral(18)]), t.identifier("atomics"));
}
return t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("string")), [valueExpression]));
},
bytes(args) {
const num = getTagNumber(args.field);
return t.expressionStatement(t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier("writer"), t.identifier("uint32")), [t.numericLiteral(num)]), t.identifier("bytes")), [t.tsNonNullExpression(t.identifier("v"))]));
},
enum() {
return arrayTypes.int32();
},
};