forked from FOSS/BangleApps
Types: improve require function
parent
2311fe91ec
commit
0526ed86ea
|
@ -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) {
|
||||||
add(
|
if (key === "require") {
|
||||||
`${indent ? "" : "declare "}function ${key}(${args}): ${returnType};`
|
add(
|
||||||
);
|
`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 {
|
||||||
|
add(
|
||||||
|
`${indent ? "" : "declare "}function ${key}(${args}): ${returnType};`,
|
||||||
|
f
|
||||||
|
);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
add(`${key}: (${args}) => ${returnType};`);
|
add(`${key}: (${args}) => ${returnType};`, f);
|
||||||
}
|
}
|
||||||
|
} else if (hasProperties) {
|
||||||
|
add(`${indent ? "" : "declare "}const ${key}: ${getType(type)} & {`, f);
|
||||||
|
file.indent += 2;
|
||||||
|
topLevel = false;
|
||||||
|
for (const key in obj) {
|
||||||
|
get(key, obj[key], true);
|
||||||
|
}
|
||||||
|
topLevel = true;
|
||||||
|
file.indent -= 2;
|
||||||
|
add("};", f);
|
||||||
|
} else if (topLevel) {
|
||||||
|
add(`${indent ? "" : "declare "}const ${key}: ${getType(type)};`, f);
|
||||||
} else {
|
} else {
|
||||||
if (hasProperties) {
|
add(`${key}: ${getType(type)}`, f);
|
||||||
add(`${indent ? "" : "declare "}const ${key}: ${getType(type)} & {`);
|
|
||||||
indent += 2;
|
|
||||||
topLevel = false;
|
|
||||||
for (const key in obj) {
|
|
||||||
get(key, obj[key], true);
|
|
||||||
}
|
|
||||||
topLevel = true;
|
|
||||||
indent -= 2;
|
|
||||||
add("};");
|
|
||||||
} else if (topLevel) {
|
|
||||||
add(`${indent ? "" : "declare "}const ${key}: ${getType(type)};`);
|
|
||||||
} else {
|
|
||||||
add(`${key}: ${getType(type)}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) => {
|
||||||
add("/* Note: This file was automatically generated. */\n");
|
const json = JSON.parse(fs.readFileSync("./espruino.json"));
|
||||||
for (const key in json) {
|
add("/* Note: This file was automatically generated. */\n");
|
||||||
get(key, json[key], true);
|
for (const key in json) {
|
||||||
}
|
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
Loading…
Reference in New Issue