tsondb 0.20.10 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -27,6 +27,15 @@ export type TypeScriptRendererOptions = {
|
|
|
27
27
|
*/
|
|
28
28
|
inferTranslationParameters?: {
|
|
29
29
|
format: "mf2";
|
|
30
|
+
/**
|
|
31
|
+
* The default type to use for translation parameters when the type cannot be inferred from the message string.
|
|
32
|
+
* @default "string"
|
|
33
|
+
*/
|
|
34
|
+
defaultTranslationParameterType?: "string" | "StringableTranslationParameter";
|
|
30
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* The generated type to use for Date types. The data is always stored as a string, but Date can be helpful if you need to differenciate between a string and a date in code. Defaults to `string`.
|
|
38
|
+
*/
|
|
39
|
+
dateType: "string" | "Date";
|
|
31
40
|
};
|
|
32
41
|
export declare const render: (options: Partial<TypeScriptRendererOptions> | undefined, declarations: readonly Decl[]) => string;
|
|
@@ -22,7 +22,9 @@ const defaultOptions = {
|
|
|
22
22
|
preserveFiles: false,
|
|
23
23
|
generateHelpers: {},
|
|
24
24
|
addIdentifierToEntities: false,
|
|
25
|
+
dateType: "string",
|
|
25
26
|
};
|
|
27
|
+
const defaultTranslationParameterType = "string";
|
|
26
28
|
const renderDocumentation = (comment, isDeprecated) => syntax `${comment === undefined
|
|
27
29
|
? ""
|
|
28
30
|
: syntax `/**
|
|
@@ -55,7 +57,7 @@ const renderObjectType = (options, type) => {
|
|
|
55
57
|
.map(([name, config]) => syntax `${renderDocumentation(config.comment, config.isDeprecated)}${name}${config.isRequired ? "" : "?"}: ${renderType(options, config.type)}`), Object.values(type.properties).some(prop => prop.comment !== undefined) ? EOL + EOL : EOL));
|
|
56
58
|
};
|
|
57
59
|
const renderBooleanType = (_options, _type) => syntax `boolean`;
|
|
58
|
-
const renderDateType = (
|
|
60
|
+
const renderDateType = (options, _type) => options.dateType === "Date" ? syntax `Date` : syntax `string`;
|
|
59
61
|
const renderFloatType = (_options, _type) => syntax `number`;
|
|
60
62
|
const renderIntegerType = (_options, _type) => syntax `number`;
|
|
61
63
|
const renderStringType = (_options, _type) => syntax `string`;
|
|
@@ -75,7 +77,7 @@ const renderEnumType = (options, type) => combineSyntaxes(Object.entries(type.va
|
|
|
75
77
|
? ""
|
|
76
78
|
: syntax `${EOL}${caseName}: ${renderType(options, caseDef.type)}`}`)}${EOL}}`)));
|
|
77
79
|
const renderChildEntitiesType = () => syntax `never`;
|
|
78
|
-
const mapTypeNameToType = (typeName) => {
|
|
80
|
+
const mapTypeNameToType = (options, typeName) => {
|
|
79
81
|
switch (typeName) {
|
|
80
82
|
case "string":
|
|
81
83
|
return "string";
|
|
@@ -88,7 +90,8 @@ const mapTypeNameToType = (typeName) => {
|
|
|
88
90
|
return "Date";
|
|
89
91
|
case null:
|
|
90
92
|
default:
|
|
91
|
-
return
|
|
93
|
+
return (options.inferTranslationParameters?.defaultTranslationParameterType ??
|
|
94
|
+
defaultTranslationParameterType);
|
|
92
95
|
}
|
|
93
96
|
};
|
|
94
97
|
const renderTranslationParameterBrand = (options, params) => {
|
|
@@ -102,7 +105,7 @@ const renderTranslationParameterBrand = (options, params) => {
|
|
|
102
105
|
return syntax ` & { __params: { ${entries.map(([name, type]) => `"${name}": ${type}`).join("; ")} } }`;
|
|
103
106
|
};
|
|
104
107
|
const renderTranslationObjectType = (options, type) => {
|
|
105
|
-
return wrapAsObject(options, combineSyntaxes(Object.entries(type.properties).map(([name, config]) => syntax `"${name.replace('"', '\\"')}"${type.allKeysAreRequired ? "" : "?"}: ${renderType(options, getTypeOfKey(config, type))}${renderTranslationParameterBrand(options, mapParameterTypeNames(extractParameterTypeNamesFromMessage(name), mapTypeNameToType))}`), EOL));
|
|
108
|
+
return wrapAsObject(options, combineSyntaxes(Object.entries(type.properties).map(([name, config]) => syntax `"${name.replace('"', '\\"')}"${type.allKeysAreRequired ? "" : "?"}: ${renderType(options, getTypeOfKey(config, type))}${renderTranslationParameterBrand(options, mapParameterTypeNames(extractParameterTypeNamesFromMessage(name), typeName => mapTypeNameToType(options, typeName)))}`), EOL));
|
|
106
109
|
};
|
|
107
110
|
const renderType = (options, type) => {
|
|
108
111
|
switch (type.kind) {
|
|
@@ -187,7 +190,10 @@ const renderEntityMapType = (options, declarations) => renderMapHelperType(optio
|
|
|
187
190
|
const renderChildEntityMapType = (options, declarations) => renderMapHelperType(options, declarations, "childEntityMap", "ChildEntityMap", decl => isEntityDecl(decl) && isEntityDeclWithParentReference(decl), decl => `${decl.name}: [${decl.name}, "${decl.parentReferenceKey}", ${decl.name}["${decl.parentReferenceKey}"]]`);
|
|
188
191
|
const renderEnumMapType = (options, declarations) => renderMapHelperType(options, declarations, "enumMap", "EnumMap", isEnumDecl);
|
|
189
192
|
const renderTypeAliasMapType = (options, declarations) => renderMapHelperType(options, declarations, "typeAliasMap", "TypeAliasMap", isTypeAliasDecl);
|
|
190
|
-
const renderStringableTranslationParameterType = (options) => options.inferTranslationParameters?.format === "mf2"
|
|
193
|
+
const renderStringableTranslationParameterType = (options) => options.inferTranslationParameters?.format === "mf2" &&
|
|
194
|
+
(options.inferTranslationParameters.defaultTranslationParameterType === undefined ||
|
|
195
|
+
options.inferTranslationParameters.defaultTranslationParameterType ===
|
|
196
|
+
"StringableTranslationParameter")
|
|
191
197
|
? "export type StringableTranslationParameter = {" +
|
|
192
198
|
EOL +
|
|
193
199
|
prefixLines(getIndentation(options.indentation, 1), "toString(): string") +
|