UNPKG

getdocs2ts

Version:

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

109 lines 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const StringBuilder = require("string-builder"); exports.baseTypes = { bool: { replaceBy: 'boolean' }, string: {}, number: {}, any: {}, Object: { replaceBy: 'object' }, this: {}, null: {}, undefined: {}, T: {} // TODO: handle type parameters dynamically! }; function mergeTypeInfo(a, b, typeName) { function checkConflict(x, y, name) { if (typeof x == 'string' && typeof y == 'string' && x != y) { throw new Error("conflicting '" + name + "' information for type '" + typeName + "'!"); } } checkConflict(a.replaceBy, b.replaceBy, 'replaceBy'); checkConflict(a.definedIn, b.definedIn, 'definedIn'); checkConflict(a.code, b.code, 'code'); return { replaceBy: a.replaceBy || b.replaceBy, definedIn: a.definedIn || b.definedIn, code: a.code || b.code }; } function mergeTypeInfos(a, b) { const res = {}; for (let typeName in a) { if (b[typeName]) { res[typeName] = mergeTypeInfo(a[typeName], b[typeName], typeName); } else { res[typeName] = a[typeName]; } } for (let typeName in b) { if (!a[typeName]) { res[typeName] = b[typeName]; } } return res; } exports.mergeTypeInfos = mergeTypeInfos; class GenEnv { constructor(currModuleName, imports, typeInfos, sb, indentation = "", firstInLine = true) { this.currModuleName = currModuleName; this.imports = imports; this.typeInfos = typeInfos; this.sb = sb; this.indentation = indentation; this.firstInLine = firstInLine; } indent() { return new GenEnv(this.currModuleName, this.imports, this.typeInfos, this.sb, this.indentation + " ", this.firstInLine); } customCodeFor(rawName) { const typeInfo = this.typeInfos[rawName]; return typeInfo && typeInfo.code; } resolveTypeName(rawName) { const typeInfo = this.typeInfos[rawName]; if (/^\"[^\"]*\"$/.test(rawName)) { return rawName; } if (typeInfo) { const name = typeof typeInfo.replaceBy == 'string' ? typeInfo.replaceBy : rawName; if (typeof typeInfo.definedIn == 'string' && typeInfo.definedIn != this.currModuleName) { const importsFromModule = this.imports[typeInfo.definedIn] || []; if (importsFromModule.indexOf(name) == -1) importsFromModule.push(name); this.imports[typeInfo.definedIn] = importsFromModule; } return name; } if (!GenEnv.warnedAbout[rawName]) { console.log("unknown type '" + rawName + "'!"); GenEnv.warnedAbout[rawName] = true; } return rawName; } append(str) { if (str == "") return; if (this.firstInLine) this.sb.append(this.indentation); this.firstInLine = false; this.sb.append(str); } appendLine(str) { if (str != "") { this.sb.appendLine(this.indentation + str); } else { this.sb.appendLine(str); } this.firstInLine = true; } } GenEnv.warnedAbout = {}; exports.GenEnv = GenEnv; function emptyEnvForTests(additionalTypes = {}) { return new GenEnv("test", {}, mergeTypeInfos(exports.baseTypes, additionalTypes), new StringBuilder()); } exports.emptyEnvForTests = emptyEnvForTests; //# sourceMappingURL=env.js.map