@cosmology/ast
Advanced tools
Comparing version 1.5.1 to 1.5.2
@@ -54,3 +54,3 @@ "use strict"; | ||
]))), | ||
(0, utils_1.identifier)("memo", t.tsTypeAnnotation(t.tsStringKeyword())), | ||
(0, utils_1.identifier)("memo", t.tsTypeAnnotation(t.tsStringKeyword()), true), | ||
]; | ||
@@ -57,0 +57,0 @@ return (0, utils_1.tsMethodSignature)(t.identifier(name), null, methodArgs, (0, rpc_1.returnReponseType)(responseType), trailingComments, leadingComments); |
@@ -164,2 +164,11 @@ "use strict"; | ||
} | ||
setEnumValues(pkg, name, values) { | ||
this.store.setEnumValues(pkg, name, values); | ||
} | ||
isEnumValueExisting(pkg, name, value) { | ||
return this.store.isEnumValueExisting(pkg, name, value); | ||
} | ||
getExistingSmallestValue(pkg, name, value) { | ||
return this.store.getExistingSmallestValue(pkg, name, value); | ||
} | ||
getToEnum(field) { | ||
@@ -166,0 +175,0 @@ const name = (0, proto_1.getEnumToJsonName)((0, proto_1.getFieldsTypeName)(field)); |
@@ -30,17 +30,3 @@ "use strict"; | ||
const utils_1 = require("../../utils"); | ||
const getEnumValues = (proto) => { | ||
const enums = Object.keys(proto.values).map(key => { | ||
const e = { | ||
name: key, | ||
comment: null, | ||
value: null | ||
}; | ||
e.value = proto.values[key]; | ||
if (proto.comments[key]) { | ||
e.comment = proto.comments[key]; | ||
} | ||
return e; | ||
}); | ||
return enums; | ||
}; | ||
const utils_2 = require("@cosmology/utils"); | ||
const ensureOneSpace = (str) => { | ||
@@ -70,3 +56,3 @@ if (/^[\s\n\t]+/.test(str)) | ||
const createProtoEnum = (context, name, proto) => { | ||
const enums = getEnumValues(proto); | ||
const enums = (0, utils_2.getEnumValues)(proto); | ||
const values = enums.map(e => { | ||
@@ -93,3 +79,3 @@ return (0, utils_1.tsEnumMember)(t.identifier(e.name), t.numericLiteral(e.value), e.comment ? [{ | ||
const createProtoEnumFromJSON = (context, name, proto) => { | ||
const enums = getEnumValues(proto); | ||
const enums = (0, utils_2.getEnumValues)(proto); | ||
const switches = enums.reduce((m, e) => { | ||
@@ -121,3 +107,3 @@ m.push(t.switchCase(t.numericLiteral(e.value), [])); | ||
const createProtoEnumToJSON = (context, name, proto) => { | ||
const enums = getEnumValues(proto); | ||
const enums = (0, utils_2.getEnumValues)(proto); | ||
const switches = enums.map(e => { | ||
@@ -124,0 +110,0 @@ return t.switchCase(t.memberExpression(t.identifier(name), t.identifier(e.name)), [ |
@@ -369,7 +369,4 @@ "use strict"; | ||
if (field.parsedType?.type === 'Enum') { | ||
// @ts-ignore | ||
if (context.ref.proto?.syntax === 'proto2') { | ||
return t.numericLiteral(1); | ||
} | ||
return t.numericLiteral(0); | ||
const enumDefault = getPreferredEnumDefault(context, field); | ||
return t.numericLiteral(enumDefault); | ||
} | ||
@@ -462,1 +459,25 @@ switch (field.type) { | ||
} | ||
function getPreferredEnumDefault(context, field) { | ||
const autoFixUndefinedEnumDefault = context.pluginValue('prototypes.typingsFormat.autoFixUndefinedEnumDefault'); | ||
// @ts-ignore | ||
if (context.ref.proto?.syntax === 'proto2') { | ||
if (autoFixUndefinedEnumDefault) { | ||
const typeName = (0, utils_1.getProtoFieldTypeName)(context, field); | ||
return context.getExistingSmallestValue(getPackage(field), typeName, 1); | ||
} | ||
else { | ||
return 1; | ||
} | ||
} | ||
if (autoFixUndefinedEnumDefault) { | ||
const typeName = (0, utils_1.getProtoFieldTypeName)(context, field); | ||
return context.getExistingSmallestValue(getPackage(field), typeName, 0); | ||
} | ||
else { | ||
return 0; | ||
} | ||
} | ||
function getPackage(field) { | ||
const pkgs = field.scope?.flat(); | ||
return pkgs ? pkgs[0] : ''; | ||
} |
@@ -28,3 +28,3 @@ import * as t from '@babel/types'; | ||
]))), | ||
identifier("memo", t.tsTypeAnnotation(t.tsStringKeyword())), | ||
identifier("memo", t.tsTypeAnnotation(t.tsStringKeyword()), true), | ||
]; | ||
@@ -31,0 +31,0 @@ return tsMethodSignature(t.identifier(name), null, methodArgs, returnReponseType(responseType), trailingComments, leadingComments); |
@@ -159,2 +159,11 @@ import { isRefExcluded, getObjectName } from '@cosmology/utils'; | ||
} | ||
setEnumValues(pkg, name, values) { | ||
this.store.setEnumValues(pkg, name, values); | ||
} | ||
isEnumValueExisting(pkg, name, value) { | ||
return this.store.isEnumValueExisting(pkg, name, value); | ||
} | ||
getExistingSmallestValue(pkg, name, value) { | ||
return this.store.getExistingSmallestValue(pkg, name, value); | ||
} | ||
getToEnum(field) { | ||
@@ -161,0 +170,0 @@ const name = getEnumToJsonName(getFieldsTypeName(field)); |
import * as t from '@babel/types'; | ||
import { getEnumFromJsonName, getEnumToJsonName } from './types'; | ||
import { identifier, tsEnumMember, functionDeclaration, makeCommentBlock, cleanComment } from '../../utils'; | ||
const getEnumValues = (proto) => { | ||
const enums = Object.keys(proto.values).map(key => { | ||
const e = { | ||
name: key, | ||
comment: null, | ||
value: null | ||
}; | ||
e.value = proto.values[key]; | ||
if (proto.comments[key]) { | ||
e.comment = proto.comments[key]; | ||
} | ||
return e; | ||
}); | ||
return enums; | ||
}; | ||
import { getEnumValues } from '@cosmology/utils'; | ||
const ensureOneSpace = (str) => { | ||
@@ -20,0 +6,0 @@ if (/^[\s\n\t]+/.test(str)) |
@@ -335,7 +335,4 @@ import * as t from '@babel/types'; | ||
if (field.parsedType?.type === 'Enum') { | ||
// @ts-ignore | ||
if (context.ref.proto?.syntax === 'proto2') { | ||
return t.numericLiteral(1); | ||
} | ||
return t.numericLiteral(0); | ||
const enumDefault = getPreferredEnumDefault(context, field); | ||
return t.numericLiteral(enumDefault); | ||
} | ||
@@ -426,1 +423,25 @@ switch (field.type) { | ||
} | ||
function getPreferredEnumDefault(context, field) { | ||
const autoFixUndefinedEnumDefault = context.pluginValue('prototypes.typingsFormat.autoFixUndefinedEnumDefault'); | ||
// @ts-ignore | ||
if (context.ref.proto?.syntax === 'proto2') { | ||
if (autoFixUndefinedEnumDefault) { | ||
const typeName = getProtoFieldTypeName(context, field); | ||
return context.getExistingSmallestValue(getPackage(field), typeName, 1); | ||
} | ||
else { | ||
return 1; | ||
} | ||
} | ||
if (autoFixUndefinedEnumDefault) { | ||
const typeName = getProtoFieldTypeName(context, field); | ||
return context.getExistingSmallestValue(getPackage(field), typeName, 0); | ||
} | ||
else { | ||
return 0; | ||
} | ||
} | ||
function getPackage(field) { | ||
const pkgs = field.scope?.flat(); | ||
return pkgs ? pkgs[0] : ''; | ||
} |
{ | ||
"name": "@cosmology/ast", | ||
"version": "1.5.1", | ||
"version": "1.5.2", | ||
"description": "Cosmos TypeScript AST generation", | ||
@@ -65,3 +65,3 @@ "author": "Dan Lynch <pyramation@gmail.com>", | ||
"devDependencies": { | ||
"@cosmology/proto-parser": "^1.5.0", | ||
"@cosmology/proto-parser": "^1.5.1", | ||
"@types/jest": "^29.5.0", | ||
@@ -88,8 +88,8 @@ "ast-stringify": "0.1.0", | ||
"@babel/types": "7.23.6", | ||
"@cosmology/types": "^1.5.0", | ||
"@cosmology/utils": "^1.5.0", | ||
"@cosmology/types": "^1.5.1", | ||
"@cosmology/utils": "^1.5.1", | ||
"case": "1.6.3", | ||
"dotty": "0.1.2" | ||
}, | ||
"gitHead": "ac7179dc6e553df3f40260c143ead2cc9da09c54" | ||
"gitHead": "a498e19917131aa20c9beafcb746246d95e00bc0" | ||
} |
@@ -74,3 +74,3 @@ import * as t from '@babel/types'; | ||
), | ||
identifier("memo", t.tsTypeAnnotation(t.tsStringKeyword())), | ||
identifier("memo", t.tsTypeAnnotation(t.tsStringKeyword()), true), | ||
]; | ||
@@ -77,0 +77,0 @@ |
@@ -211,2 +211,14 @@ import { TelescopeOptions, ProtoField, ProtoRef, TraversalSymbol, IParseContext, ImportUsage } from '@cosmology/types'; | ||
setEnumValues(pkg: string, name: string, values: number[]) { | ||
this.store.setEnumValues(pkg, name, values); | ||
} | ||
isEnumValueExisting(pkg: string, name: string, value: number) { | ||
return this.store.isEnumValueExisting(pkg, name, value); | ||
} | ||
getExistingSmallestValue(pkg: string, name: string, value: number) { | ||
return this.store.getExistingSmallestValue(pkg, name, value); | ||
} | ||
getToEnum(field: ProtoField) { | ||
@@ -213,0 +225,0 @@ const name = getEnumToJsonName(getFieldsTypeName(field)); |
@@ -6,19 +6,4 @@ import * as t from '@babel/types'; | ||
import { ProtoParseContext } from '../context'; | ||
import { getEnumValues } from '@cosmology/utils'; | ||
const getEnumValues = (proto: ProtoEnum) => { | ||
const enums = Object.keys(proto.values).map(key => { | ||
const e = { | ||
name: key, | ||
comment: null, | ||
value: null | ||
}; | ||
e.value = proto.values[key]; | ||
if (proto.comments[key]) { | ||
e.comment = proto.comments[key]; | ||
} | ||
return e; | ||
}); | ||
return enums; | ||
} | ||
const ensureOneSpace = (str) => { | ||
@@ -25,0 +10,0 @@ if (/^[\s\n\t]+/.test(str)) return str; |
@@ -429,7 +429,5 @@ import * as t from '@babel/types'; | ||
if (field.parsedType?.type === 'Enum') { | ||
// @ts-ignore | ||
if (context.ref.proto?.syntax === 'proto2') { | ||
return t.numericLiteral(1); | ||
} | ||
return t.numericLiteral(0); | ||
const enumDefault = getPreferredEnumDefault(context, field); | ||
return t.numericLiteral(enumDefault); | ||
} | ||
@@ -555,1 +553,26 @@ | ||
function getPreferredEnumDefault(context: ProtoParseContext, field: ProtoField) { | ||
const autoFixUndefinedEnumDefault = context.pluginValue('prototypes.typingsFormat.autoFixUndefinedEnumDefault') | ||
// @ts-ignore | ||
if (context.ref.proto?.syntax === 'proto2') { | ||
if(autoFixUndefinedEnumDefault){ | ||
const typeName = getProtoFieldTypeName(context, field); | ||
return context.getExistingSmallestValue(getPackage(field), typeName, 1) | ||
} else { | ||
return 1; | ||
} | ||
} | ||
if(autoFixUndefinedEnumDefault){ | ||
const typeName = getProtoFieldTypeName(context, field); | ||
return context.getExistingSmallestValue(getPackage(field), typeName, 0) | ||
} else { | ||
return 0; | ||
} | ||
} | ||
function getPackage(field: ProtoField){ | ||
const pkgs = field.scope?.flat(); | ||
return pkgs ? pkgs[0] : ''; | ||
} |
@@ -36,2 +36,5 @@ import { TelescopeOptions, ProtoField, ProtoRef, TraversalSymbol, IParseContext, ImportUsage } from '@cosmology/types'; | ||
constructor(ref: ProtoRef, store: IProtoStore, options: TelescopeOptions); | ||
setEnumValues(pkg: string, name: string, values: number[]): void; | ||
isEnumValueExisting(pkg: string, name: string, value: number): boolean; | ||
getExistingSmallestValue(pkg: string, name: string, value: number): number; | ||
getToEnum(field: ProtoField): string; | ||
@@ -38,0 +41,0 @@ getFromEnum(field: ProtoField): string; |
Sorry, the diff of this file is not supported yet
3319313
66849
Updated@cosmology/types@^1.5.1
Updated@cosmology/utils@^1.5.1