UNPKG

bitwig-types-generator

Version:
132 lines 4.63 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Utils = void 0; const cheerio_1 = __importDefault(require("cheerio")); const domhandler_1 = require("domhandler"); class Utils { static fixTypes(types) { return types .replace(/\<\? extends T \>/g, "<T>") .replace(/Class\<\?\>/g, "Class") .replace(/List\<\? extends (\w+) \>/, "List<$1>") .replace(/Function\< (\w+), (\w+) \>/g, "Func<$1,$2>") .replace(/ function/g, " fn") .replace(/Consumer\< (\w+) \>/g, "Consumer<$1>") .replace(/Supplier\< (\w+) \>/g, "Supplier<$1>"); } static parseMemitem(memitem) { const match = memitem.match(/^memitem:(.+)$/); return match ? match[1] : ""; } static parseDeclaration(dec) { dec = Utils.fixTypes(dec); let match; let type; let value; if ((match = dec.match(/^public class (.+)$/))) { type = "class"; value = match[1]; } else if ((match = dec.match(/^public abstract class (.+)$/))) { type = "class"; value = match[1]; } else if ((match = dec.match(/^public interface (.+)$/))) { type = "interface"; value = match[1]; } else if ((match = dec.match(/^public @interface (.+)$/))) { type = "interface"; value = match[1]; } else if ((match = dec.match(/^public enum (.+)$/))) { type = "enum"; value = match[1]; } else if ((match = dec.match(/^enum (.+)$/))) { type = "enum"; value = match[1]; } else { throw new Error(`unknown declaration: ${dec}`); } const name = value.match(/^\w+/)[0]; return { type, value, name }; } static parseNode(el, container) { let str = ""; for (const c of el.childNodes) { if ((0, domhandler_1.isTag)(c)) { let val = (0, cheerio_1.default)(c).text(); switch (c.tagName) { case "a": if (c.attribs.href.startsWith("http")) { str += `{@link ${c.attribs.href} ${val}}`; } else { val = val .replace("#", ".") .replace("::", ".") .replace(/\(.+$/g, ""); if (/^[a-z]\w+$/.test(val)) { str += `{@link ${container}.${val}}`; } else { str += `{@link ${val}}`; } } break; case "code": case "b": str += val; break; case "br": break; } } else if ((0, domhandler_1.isText)(c)) { str += c.nodeValue; } } return str; } static parseDescription(doc, container) { const lines = doc .find("p") .toArray() .map((el) => Utils.parseNode(el, container)); for (const dl of doc.find("dl").toArray()) { (0, cheerio_1.default)(dl).removeClass("section"); const val = Utils.parseNode((0, cheerio_1.default)("dd", dl)[0], container); switch (dl.attribs.class) { case "since": case "deprecated": case "see": case "return": case "version": lines.push(`@${dl.attribs.class} ${val}`); break; case "exception": case "params": /** * @Todo */ break; default: throw new Error(`Unkown section description: ${dl.attribs.class}`); } } /** * @Todo * better comment lines */ if (!lines.length) return ""; return `/**\n${lines.map((line) => `* ${line}`).join("\n")}\n**/\n`; } } exports.Utils = Utils; //# sourceMappingURL=Utils.js.map