UNPKG

getdocs2ts

Version:

This is a utility that transforms code documented with getdocs-style doc comments into TypeScript definition files

110 lines 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const types_1 = require("./types"); const gentype_1 = require("./gentype"); function functionDeclarationDef(env, item) { gentype_1.functionParamsDef(env, item.params || []); env.append(": "); gentype_1.functionReturnDef(env, item.returns); } function miscDef(env, type, name, isInlineProp, processItemProperties = true) { if (types_1.isFunction(type) && !type.optional) { const isConstructor = typeof type.id == "string" && /\.constructor$/.test(type.id); if (isConstructor) { env.append("constructor"); gentype_1.functionParamsDef(env, type.params || []); } else { if (!isInlineProp) env.append("function "); env.append(name); functionDeclarationDef(env, type); } } else if (isInlineProp) { env.append(name); if (type.type) { if (type.optional) { env.append("?: "); gentype_1.typeDef(env, gentype_1.unionWith(type, gentype_1.nullType)); } else { env.append(": "); gentype_1.typeDef(env, type); } } env.append(";"); } else { env.append("let " + name); if (type.type) { env.append(": "); gentype_1.typeDef(env, type.optional ? gentype_1.unionWith(type, gentype_1.nullType, gentype_1.undefinedType) : type); } } if (types_1.isObject(type) && processItemProperties) { for (let prop in type.properties) { miscDef(env, type.properties[prop], prop, true); } } } exports.miscDef = miscDef; function classDef(env, decl, name) { env.append(decl.type + " " + name + " "); if (decl.typeParams) { env.append("<"); for (let i in decl.typeParams) { if (i != "0") { env.append(", "); } gentype_1.typeDef(env, decl.typeParams[i]); } env.append("> "); } if (decl.extends) { env.append("extends "); gentype_1.typeDef(env, decl.extends); env.append(" "); } env.append("{"); const indented = env.indent(); if ("constructor" in decl && !(decl.constructor instanceof Function)) { indented.appendLine(""); miscDef(indented, decl.constructor, name, false); } if (decl.properties) { for (let prop in decl.properties) { indented.appendLine(""); miscDef(indented, decl.properties[prop], prop, true); } } if (decl.staticProperties) { for (let prop in decl.staticProperties) { indented.appendLine(""); indented.append("static "); miscDef(indented, decl.staticProperties[prop], prop, true); } } env.appendLine("}"); } exports.classDef = classDef; function itemDef(env, decl, name, exportDecl = false) { if (types_1.isClassOrInterfaceDeclaration(decl)) { const customCode = env.customCodeFor(name); if (typeof customCode == 'string') { env.append(customCode); } else { if (exportDecl) env.append("export "); classDef(env, decl, env.resolveTypeName(name)); } } else { if (exportDecl) env.append("export "); miscDef(env, decl, name, false, false); } } exports.itemDef = itemDef; //# sourceMappingURL=gendeclaration.js.map