UNPKG

@cosmology/ast

Version:
119 lines (118 loc) 4.28 kB
import * as t from "@babel/types"; export const TypeLong = { propTypes: { bigint: "bigint", long: "Long", }, propIdentifiers: { bigint: t.identifier("bigint"), long: t.identifier("Long"), }, types: { bigint: "BigInt", long: "Long", }, identifiers: { bigint: t.identifier("BigInt"), long: t.identifier("Long"), }, fromStringArray: { bigint: t.callExpression(t.identifier("BigInt"), [t.identifier("e")]), long: t.callExpression(t.memberExpression(t.identifier("Long"), t.identifier("fromString")), [t.identifier("e")]), }, toStringArray: { bigint: t.callExpression(t.memberExpression(t.identifier("e"), t.identifier("toString")), []), long: t.identifier("e"), }, uzeroExpressions: { bigint: t.callExpression(t.identifier("BigInt"), [t.numericLiteral(0)]), long: t.memberExpression(t.identifier("Long"), t.identifier("UZERO")), }, zeroExpressions: { bigint: t.callExpression(t.identifier("BigInt"), [t.numericLiteral(0)]), long: t.memberExpression(t.identifier("Long"), t.identifier("ZERO")), }, fromValueExpressions: { bigint: t.identifier("BigInt"), long: t.memberExpression(t.identifier("Long"), t.identifier("fromValue")), }, fromNumberExpressions: { bigint: t.identifier("BigInt"), long: t.memberExpression(t.identifier("Long"), t.identifier("fromNumber")), }, fromStringExpressions: { bigint: t.identifier("BigInt"), long: t.memberExpression(t.identifier("Long"), t.identifier("fromString")), }, addUtil: (ctx) => { if (!ctx) { return; } const longLib = ctx.pluginValue("prototypes.typingsFormat.num64"); switch (longLib) { case "long": ctx.addUtil("Long"); break; } }, getPropType: (ctx) => { return TypeLong.getNode(ctx, TypeLong.propTypes); }, getType: (ctx) => { return TypeLong.getNode(ctx, TypeLong.types); }, getFromStringArray: (ctx) => { return TypeLong.getNode(ctx, TypeLong.fromStringArray); }, getToStringArray: (ctx) => { return TypeLong.getNode(ctx, TypeLong.toStringArray); }, getPropIdentifier: (ctx) => { return TypeLong.getNode(ctx, TypeLong.propIdentifiers); }, getIdentifier: (ctx) => { return TypeLong.getNode(ctx, TypeLong.identifiers); }, getUZero: (ctx) => { return TypeLong.getNode(ctx, TypeLong.uzeroExpressions); }, getZero: (ctx) => { return TypeLong.getNode(ctx, TypeLong.zeroExpressions); }, getFromValue: (ctx) => { return TypeLong.getNode(ctx, TypeLong.fromValueExpressions); }, getFromValueWithArgs: (ctx, arg) => { const longLib = ctx.pluginValue("prototypes.typingsFormat.num64"); const args = []; switch (longLib) { case "bigint": args.push(t.callExpression(t.memberExpression(arg, t.identifier("toString")), [])); break; case "long": args.push(arg); default: } return t.callExpression(TypeLong.getFromValue(ctx), args); }, getFromNumber: (ctx) => { return TypeLong.getNode(ctx, TypeLong.fromNumberExpressions); }, getFromString: (ctx) => { return TypeLong.getNode(ctx, TypeLong.fromStringExpressions); }, getNode: (ctx, mappings) => { const longLib = ctx.pluginValue("prototypes.typingsFormat.num64"); return mappings[longLib] ?? mappings["long"]; }, getLongNotZero: (prop, ctx) => { const longLib = ctx.pluginValue("prototypes.typingsFormat.num64"); switch (longLib) { case "bigint": return t.binaryExpression("!==", t.memberExpression(t.identifier("message"), t.identifier(prop)), t.callExpression(t.identifier("BigInt"), [t.numericLiteral(0)])); case "long": default: return t.unaryExpression("!", t.callExpression(t.memberExpression(t.memberExpression(t.identifier("message"), t.identifier(prop)), t.identifier("isZero")), [])); } }, };