1
0
Fork 0

Types: improve require function

master
qucchia 2022-07-20 20:35:57 +02:00
parent 2311fe91ec
commit 0526ed86ea
2 changed files with 4023 additions and 4060 deletions

View File

@ -2,11 +2,11 @@ const fetch = (...args) =>
import("node-fetch").then(({ default: fetch }) => fetch(...args)); import("node-fetch").then(({ default: fetch }) => fetch(...args));
const fs = require("fs"); const fs = require("fs");
const TAKEN_IDENTIFIERS = ["ArrayBufferView", "crypto", "File", "Storage"]; const TAKEN_IDENTIFIERS = ["ArrayBufferView", "File"];
function getType(type) { function getType(type) {
if (type.startsWith("+")) type = type.substring(1); if (type.startsWith("+")) type = type.substring(1);
if (TAKEN_IDENTIFIERS.includes(type)) return "Espruino" + type; if (TAKEN_IDENTIFIERS.includes(type)) type = "Espruino" + type;
switch (type) { switch (type) {
case undefined: case undefined:
case "?": case "?":
@ -26,17 +26,20 @@ function getType(type) {
let indent = 0; let indent = 0;
let topLevel = true; let topLevel = true;
let file = []; let file = { lines: [], indent: 0 };
let libraries = { lines: [], indent: 2 };
function add(text) { function add(text, f) {
if (!f) f = file;
if (text) if (text)
file = file.concat( f.lines.push(
text.split("\n").map((line) => " ".repeat(indent) + line.trimEnd()) ...text.split("\n").map((line) => " ".repeat(f.indent) + line.trimEnd())
); );
else file.push(""); else f.lines.push("");
} }
function get(key, obj, isGlobal) { function get(key, obj, isGlobal, f) {
if (!f) f = file;
if (key.startsWith("!")) return; if (key.startsWith("!")) return;
if (key === "prototype") return; if (key === "prototype") return;
if (key in global && isGlobal) return; if (key in global && isGlobal) return;
@ -74,7 +77,8 @@ function get(key, obj, isGlobal) {
.concat([`@url ${obj["!url"]}`]) .concat([`@url ${obj["!url"]}`])
.map((line) => " * " + line) .map((line) => " * " + line)
.join("\n") + .join("\n") +
"\n */" "\n */",
f
); );
const type = obj["!type"] || "?"; const type = obj["!type"] || "?";
@ -98,63 +102,87 @@ function get(key, obj, isGlobal) {
}) })
.join(", "); .join(", ");
if (hasProperties) { if (obj["!library"]) {
add(`${indent ? "" : "declare "}const ${key}: {`); add(`${key}: {`, libraries);
indent += 2; libraries.indent += 2;
topLevel = false;
for (const key in obj) {
get(key, obj[key], true, libraries);
}
topLevel = true;
libraries.indent -= 2;
add("};", libraries);
} else if (hasProperties) {
add(`${indent ? "" : "declare "}const ${key}: {`, f);
file.indent += 2;
topLevel = false; topLevel = false;
for (const key in obj) { for (const key in obj) {
get(key, obj[key], true); get(key, obj[key], true);
} }
topLevel = true; topLevel = true;
indent -= 2; file.indent -= 2;
add("};"); add("};", f);
} else if (topLevel) { } else if (topLevel) {
if (key === "require") {
add( add(
`${indent ? "" : "declare "}function ${key}(${args}): ${returnType};` `declare function require<T extends keyof Modules>(moduleName: T): Modules[T];`,
f
);
add(
`declare function require<T extends Exclude<string, keyof Modules>>(moduleName: T): any;`,
f
); );
} else { } else {
add(`${key}: (${args}) => ${returnType};`); add(
`${indent ? "" : "declare "}function ${key}(${args}): ${returnType};`,
f
);
} }
} else { } else {
if (hasProperties) { add(`${key}: (${args}) => ${returnType};`, f);
add(`${indent ? "" : "declare "}const ${key}: ${getType(type)} & {`); }
indent += 2; } else if (hasProperties) {
add(`${indent ? "" : "declare "}const ${key}: ${getType(type)} & {`, f);
file.indent += 2;
topLevel = false; topLevel = false;
for (const key in obj) { for (const key in obj) {
get(key, obj[key], true); get(key, obj[key], true);
} }
topLevel = true; topLevel = true;
indent -= 2; file.indent -= 2;
add("};"); add("};", f);
} else if (topLevel) { } else if (topLevel) {
add(`${indent ? "" : "declare "}const ${key}: ${getType(type)};`); add(`${indent ? "" : "declare "}const ${key}: ${getType(type)};`, f);
} else { } else {
add(`${key}: ${getType(type)}`); add(`${key}: ${getType(type)}`, f);
}
} }
if (obj.prototype) { if (obj.prototype) {
add(""); add("", f);
add(`type ${key} = {`); add(`type ${key} = {`, f);
indent += 2; file.indent += 2;
topLevel = false; topLevel = false;
for (const key in obj.prototype) { for (const key in obj.prototype) {
get(key, obj.prototype[key], true); get(key, obj.prototype[key], true);
} }
topLevel = true; topLevel = true;
indent -= 2; file.indent -= 2;
add("}"); add("}", f);
} }
add(""); add("", f);
} }
fetch("https://espruino.com/json/espruino.json") // fetch("https://espruino.com/json/espruino.json")
.then((response) => response.json()) // .then((response) => response.json())
.then((json) => { // .then((json) => {
const json = JSON.parse(fs.readFileSync("./espruino.json"));
add("/* Note: This file was automatically generated. */\n"); add("/* Note: This file was automatically generated. */\n");
for (const key in json) { for (const key in json) {
get(key, json[key], true); get(key, json[key], true);
} }
fs.writeFileSync("types/main.d.ts", file.join("\n")); add("type Modules = {");
}); add(libraries.lines.join("\n"));
add("}");
fs.writeFileSync("types/main.d.ts", file.lines.join("\n"));
// });

File diff suppressed because it is too large Load Diff