support for extends

This commit is contained in:
Jill 2021-11-03 09:27:08 +03:00
parent 943ec11520
commit 1cf53d2eb0
1 changed files with 45 additions and 5 deletions

View File

@ -6,6 +6,8 @@ const chalk = require('chalk');
const declarationsPath = path.resolve(__dirname, './isaac-typescript-definitions/typings/');
const outPath = path.resolve(__dirname, './out/');
let classes = {};
function transpile(parsed, indent, prefix) {
let global = false;
indent = indent || 0;
@ -16,7 +18,18 @@ function transpile(parsed, indent, prefix) {
case 'namespace':
global = true;
case 'interface':
return `---@class ${d.name}\n${global ? '' : '__class_'}${d.name} = {}${n}${d.contents.map(c => transpile(c, indent, `${global ? '' : '__class_'}${d.name}`)).join(n)}\n`;
let contents = d.contents;
if (d.extends) {
if (classes[d.extends]) {
contents.push(...classes[d.extends]);
} else {
throw new Error(`Class not found in class storage: ${d.extends}`)
}
}
classes[d.name] = contents;
return `---@class ${d.name}\n${global ? '' : '__class_'}${d.name} = {}${n}${contents.map(c => transpile(c, indent, `${global ? '' : '__class_'}${d.name}`)).join(n)}\n`;
case 'function':
return `${d.arguments.map(p => `---@param ${p.name.replace('?', '')} ${p.type}${n}`).join('')}---@return ${d.returns}${n}function ${prefix ? prefix + ':' : ''}${d.name}(${d.arguments.map(a => a.name.replace('?', '')).join(', ')}) end`;
case 'const':
@ -47,10 +60,10 @@ function parse(code) {
// look for interfaces
let interfaceFreeCode = code;
const interface = /interface (\w+) ?{([^}]*)}/g;
const interface = /interface (\w+)(?: extends (\w+))? ?{([^}]*)}/g;
while ((match = interface.exec(code)) !== null) {
interfaceFreeCode = interfaceFreeCode.replace(match[0], '');
elements.push(['interface', {name: match[1], contents: parse(match[2])}]);
elements.push(['interface', {name: match[1], extends: match[2], contents: parse(match[3])}]);
}
code = interfaceFreeCode;
@ -113,12 +126,17 @@ function parse(code) {
}
}
let errored = [];
let errorReason = {};
const files = await fs.readdir(declarationsPath);
let i = 0;
for (const f of files) {
i++;
const filePath = path.resolve(declarationsPath, f);
const stat = await fs.lstat(filePath);
if (stat.isFile() && f) {
if (stat.isFile()) {
const file = await fs.readFile(filePath, 'utf8');
let startParse = Date.now();
@ -131,7 +149,29 @@ function parse(code) {
continue;
}
const transpiled = parsed.map(p => transpile(p)).join('\n').trim();
let transpiled;
try {
transpiled = parsed.map(p => transpile(p)).join('\n').trim();
errored = errored.filter(e => e !== f);
} catch(err) {
errorReason[f] = err;
if (errored.length > files.length - i) {
console.error(`${chalk.redBright('!')} failed resolving extends!`);
console.error(`${chalk.redBright('!')} abandoned ${chalk.magentaBright(errored.length)} files:`);
let longest = errored.reduce((a, b) => Math.max(a.length || a || 0, b.length));
errored.forEach(e => console.error(` - ${chalk.cyanBright(e)}${' '.repeat(longest - e.length + 2)}${chalk.gray(' -> ' + errorReason[e].message.replace('Class not found in class storage: ', ''))}`));
break;
}
console.error(`${chalk.yellowBright('!')} ${err}`);
console.error(`${chalk.yellowBright('!')} pushing to end of queue`);
files.push(f);
errored.push(f);
timeTotal += Date.now() - startParse;
continue;
}
console.log(` transpiled w/ final length of ${chalk.magentaBright(transpiled.length + ' chars')}`);
timeTotal += Date.now() - startParse;