conjure-typescript
Advanced tools
Comparing version 5.0.0 to 5.1.0
@@ -96,30 +96,54 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var sourceFile, namespaceDefinition; | ||
var sourceFile, namespaceDefinition, variableStatement; | ||
return __generator(this, function (_a) { | ||
sourceFile = simpleAst.createSourceFile(definition.typeName); | ||
namespaceDefinition = sourceFile.addNamespace({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
typeAliases: definition.values.map(function (enumValue) { return ({ | ||
if (definition.values.length > 0) { | ||
namespaceDefinition = sourceFile.addNamespace({ | ||
isExported: true, | ||
name: enumValue.value, | ||
type: utils_1.doubleQuote(enumValue.value), | ||
docs: utils_1.addDeprecatedToDocs(enumValue), | ||
}); }), | ||
bodyText: function (writer) { | ||
definition.values.forEach(function (_a) { | ||
var value = _a.value; | ||
var quotedValue = utils_1.doubleQuote(value); | ||
writer.writeLine("export const " + value + " = " + quotedValue + " as " + quotedValue + ";"); | ||
}); | ||
}, | ||
}); | ||
if (definition.docs != null) { | ||
namespaceDefinition.addJsDoc(definition.docs); | ||
name: definition.typeName.name, | ||
typeAliases: definition.values.map(function (enumValue) { return ({ | ||
isExported: true, | ||
name: enumValue.value, | ||
type: utils_1.doubleQuote(enumValue.value), | ||
docs: utils_1.addDeprecatedToDocs(enumValue), | ||
}); }), | ||
bodyText: function (writer) { | ||
definition.values.forEach(function (_a) { | ||
var value = _a.value; | ||
var quotedValue = utils_1.doubleQuote(value); | ||
writer.writeLine("export const " + value + " = " + quotedValue + " as " + quotedValue + ";"); | ||
}); | ||
}, | ||
}); | ||
if (definition.docs != null) { | ||
namespaceDefinition.addJsDoc(definition.docs); | ||
} | ||
sourceFile.addTypeAlias({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
type: "keyof typeof " + definition.typeName.name, | ||
}); | ||
} | ||
sourceFile.addTypeAlias({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
type: "keyof typeof " + definition.typeName.name, | ||
}); | ||
else { | ||
variableStatement = sourceFile.addVariableStatement({ | ||
declarationKind: ts_simple_ast_1.VariableDeclarationKind.Const, | ||
declarations: [ | ||
{ | ||
initializer: "{}", | ||
name: definition.typeName.name, | ||
}, | ||
], | ||
isExported: true, | ||
}); | ||
if (definition.docs != null) { | ||
variableStatement.addJsDoc(definition.docs); | ||
} | ||
sourceFile.addTypeAlias({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
// We use void instead of never because void can't be assigned to anything else | ||
// (while never is assignable to anything) | ||
type: "void", | ||
}); | ||
} | ||
sourceFile.formatText(); | ||
@@ -126,0 +150,0 @@ return [2 /*return*/, sourceFile.save()]; |
{ | ||
"name": "conjure-typescript", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "A conjure generator for Typescript", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -69,28 +69,53 @@ /** | ||
const namespaceDefinition = sourceFile.addNamespace({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
typeAliases: definition.values.map(enumValue => ({ | ||
if (definition.values.length > 0) { | ||
const namespaceDefinition = sourceFile.addNamespace({ | ||
isExported: true, | ||
name: enumValue.value, | ||
type: doubleQuote(enumValue.value), | ||
docs: addDeprecatedToDocs(enumValue), | ||
})), | ||
bodyText: writer => { | ||
definition.values.forEach(({ value }) => { | ||
const quotedValue = doubleQuote(value); | ||
writer.writeLine(`export const ${value} = ${quotedValue} as ${quotedValue};`); | ||
}); | ||
}, | ||
}); | ||
if (definition.docs != null) { | ||
namespaceDefinition.addJsDoc(definition.docs); | ||
name: definition.typeName.name, | ||
typeAliases: definition.values.map(enumValue => ({ | ||
isExported: true, | ||
name: enumValue.value, | ||
type: doubleQuote(enumValue.value), | ||
docs: addDeprecatedToDocs(enumValue), | ||
})), | ||
bodyText: writer => { | ||
definition.values.forEach(({ value }) => { | ||
const quotedValue = doubleQuote(value); | ||
writer.writeLine(`export const ${value} = ${quotedValue} as ${quotedValue};`); | ||
}); | ||
}, | ||
}); | ||
if (definition.docs != null) { | ||
namespaceDefinition.addJsDoc(definition.docs); | ||
} | ||
sourceFile.addTypeAlias({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
type: `keyof typeof ${definition.typeName.name}`, | ||
}); | ||
} else { | ||
// We need to special case empty enums for two reasons: | ||
// 1) `keyof typeof MyEnum` results in an erorr | ||
// 2) Typescript won't generate `const MyEnum = {}` and will instead just skip it from the compiled code. | ||
const variableStatement = sourceFile.addVariableStatement({ | ||
declarationKind: VariableDeclarationKind.Const, | ||
declarations: [ | ||
{ | ||
initializer: "{}", | ||
name: definition.typeName.name, | ||
}, | ||
], | ||
isExported: true, | ||
}); | ||
if (definition.docs != null) { | ||
variableStatement.addJsDoc(definition.docs); | ||
} | ||
sourceFile.addTypeAlias({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
// We use void instead of never because void can't be assigned to anything else | ||
// (while never is assignable to anything) | ||
type: "void", | ||
}); | ||
} | ||
sourceFile.addTypeAlias({ | ||
isExported: true, | ||
name: definition.typeName.name, | ||
type: `keyof typeof ${definition.typeName.name}`, | ||
}); | ||
sourceFile.formatText(); | ||
@@ -97,0 +122,0 @@ return sourceFile.save(); |
Sorry, the diff of this file is not supported yet
243317
4223