experimental comment transpilation support
VERY experimental, VERY janky
This commit is contained in:
parent
3d1ea46f50
commit
3dbadcf775
34
index.js
34
index.js
|
@ -1,6 +1,5 @@
|
||||||
const fs = require('fs/promises');
|
const fs = require('fs/promises');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { inspect } = require('util');
|
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
|
|
||||||
const declarationsPath = path.resolve(__dirname, './isaac-typescript-definitions/typings/');
|
const declarationsPath = path.resolve(__dirname, './isaac-typescript-definitions/typings/');
|
||||||
|
@ -69,6 +68,9 @@ function transpile(parsed, indent, prefix) {
|
||||||
const type = parsed[0];
|
const type = parsed[0];
|
||||||
const d = parsed[1];
|
const d = parsed[1];
|
||||||
let n = '\n' + ' '.repeat(indent); // newline
|
let n = '\n' + ' '.repeat(indent); // newline
|
||||||
|
|
||||||
|
const comments = d.comment ? d.comment.split('\n').map(l => l.trim()).map(l => l.startsWith('*') ? l.replace('*', '') : l).filter(l => l.trim().length !== 0 && !l.trim().startsWith('@')).map(l => '---' + l.trim()).join('\n') + '\n' : '';
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'namespace':
|
case 'namespace':
|
||||||
global = true;
|
global = true;
|
||||||
|
@ -84,14 +86,14 @@ function transpile(parsed, indent, prefix) {
|
||||||
}
|
}
|
||||||
|
|
||||||
classes[d.name] = contents;
|
classes[d.name] = contents;
|
||||||
return `---@class ${d.name}${d.extends ? ' : ' + d.extends : ''}${n}${contents.filter(e => e[0] === 'const').map(c => `---@field public ${c[1].name} ${transpileType(c[1].type)}${n}`).join('')}${global ? '' : '__class_'}${d.name} = {}${n}${contents.filter(e => e[0] !== 'const').map(c => transpile(c, indent, `${global ? '' : '__class_'}${d.name}`)).join(n)}\n`;
|
return `${comments}---@class ${d.name}${d.extends ? ' : ' + d.extends : ''}${n}${contents.filter(e => e[0] === 'const').map(c => `---@field public ${c[1].name} ${transpileType(c[1].type)}${n}`).join('')}${global ? '' : '__class_'}${d.name} = {}${n}${contents.filter(e => e[0] !== 'const').map(c => transpile(c, indent, `${global ? '' : '__class_'}${d.name}`)).join(n)}\n`;
|
||||||
case 'function':
|
case 'function':
|
||||||
if (d.arguments[0] && d.arguments[0].type === 'void') d.arguments = d.arguments.slice(1);
|
if (d.arguments[0] && d.arguments[0].type === 'void') d.arguments = d.arguments.slice(1);
|
||||||
return `${d.arguments.map(p => `---@param ${p.name.replace('?', '')} ${transpileType(p.type)}${n}`).join('')}---@return ${transpileType(d.returns)}${n}function ${prefix ? prefix + ':' : ''}${d.name}(${d.arguments.map(a => a.name.replace('?', '')).join(', ')}) end`;
|
return `${comments}${d.arguments.map(p => `---@param ${p.name.replace('?', '')} ${transpileType(p.type)}${n}`).join('')}---@return ${transpileType(d.returns)}${n}function ${prefix ? prefix + ':' : ''}${d.name}(${d.arguments.map(a => a.name.replace('?', '')).join(', ')}) end`;
|
||||||
case 'const':
|
case 'const':
|
||||||
return `---@type ${transpileType(d.type)}${n}${prefix ? prefix + '.' : ''}${d.name} = nil`;
|
return `${comments}---@type ${transpileType(d.type)}${n}${prefix ? prefix + '.' : ''}${d.name} = nil`;
|
||||||
case 'enum':
|
case 'enum':
|
||||||
return `${prefix ? prefix + '.' : ''}${d.name} = {${n} ${d.contents.map(c => `${c.name} = ${c.value}`).join(`,${n} `)}${n}}`;
|
return `${comments}${prefix ? prefix + '.' : ''}${d.name} = {${n} ${d.contents.map(c => `${c.name} = ${c.value}`).join(`,${n} `)}${n}}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
|
@ -109,7 +111,7 @@ let parsedElements = 0;
|
||||||
/**
|
/**
|
||||||
* @param {string} code
|
* @param {string} code
|
||||||
*/
|
*/
|
||||||
function parse(code) {
|
function parse(code, originalCode) {
|
||||||
// console.log(code);
|
// console.log(code);
|
||||||
|
|
||||||
// we're left with code containing "declare" statements
|
// we're left with code containing "declare" statements
|
||||||
|
@ -117,6 +119,7 @@ function parse(code) {
|
||||||
// were gonna end up with an ast of sorts anyways as a result, which we can then use instead of declares
|
// were gonna end up with an ast of sorts anyways as a result, which we can then use instead of declares
|
||||||
|
|
||||||
// sanitize stuff
|
// sanitize stuff
|
||||||
|
if (!originalCode) originalCode = code;
|
||||||
code = removeComments(code);
|
code = removeComments(code);
|
||||||
|
|
||||||
let match;
|
let match;
|
||||||
|
@ -128,7 +131,7 @@ function parse(code) {
|
||||||
while ((match = interface.exec(code)) !== null) {
|
while ((match = interface.exec(code)) !== null) {
|
||||||
parsedElements++;
|
parsedElements++;
|
||||||
interfaceFreeCode = interfaceFreeCode.replace(match[0], '');
|
interfaceFreeCode = interfaceFreeCode.replace(match[0], '');
|
||||||
elements.push(['interface', {name: match[1], extends: match[2], contents: parse(match[3])}]);
|
elements.push(['interface', {name: match[1], extends: match[2], contents: parse(match[3], originalCode)}]);
|
||||||
}
|
}
|
||||||
code = interfaceFreeCode;
|
code = interfaceFreeCode;
|
||||||
|
|
||||||
|
@ -138,7 +141,7 @@ function parse(code) {
|
||||||
while ((match = namespace.exec(code)) !== null) {
|
while ((match = namespace.exec(code)) !== null) {
|
||||||
parsedElements++;
|
parsedElements++;
|
||||||
namespaceFreeCode = namespaceFreeCode.replace(match[0], '');
|
namespaceFreeCode = namespaceFreeCode.replace(match[0], '');
|
||||||
elements.push(['namespace', {name: match[1], contents: parse(match[2])}]);
|
elements.push(['namespace', {name: match[1], contents: parse(match[2], originalCode)}]);
|
||||||
}
|
}
|
||||||
code = namespaceFreeCode;
|
code = namespaceFreeCode;
|
||||||
|
|
||||||
|
@ -152,7 +155,19 @@ function parse(code) {
|
||||||
const split = s.split(':').map(s => s.trim());
|
const split = s.split(':').map(s => s.trim());
|
||||||
return {type: split[1], name: split[0]}
|
return {type: split[1], name: split[0]}
|
||||||
}).filter(c => c.type && c.name);
|
}).filter(c => c.type && c.name);
|
||||||
elements.push(['function', {name: match[2], arguments: arguments, returns: match[5]}]);
|
|
||||||
|
const origIndex = originalCode.indexOf(match[0]);
|
||||||
|
const comment = /\/\*(.*?)\*\//gs;
|
||||||
|
let comments = [];
|
||||||
|
let comm;
|
||||||
|
while ((comm = comment.exec(originalCode.slice(0, origIndex))) !== null) {
|
||||||
|
comments.push([comm[1], comm.index + comm[0].length]);
|
||||||
|
}
|
||||||
|
comments = comments.filter(c => Math.abs(origIndex - c[1]) < 10);
|
||||||
|
const c = comments.pop();
|
||||||
|
//if (c) c[0] += 'isaac-lua-definitions debug: comment distance = ' + (origIndex - c[1]).toString();
|
||||||
|
|
||||||
|
elements.push(['function', {name: match[2], arguments: arguments, returns: match[5], comment: (c || [null])[0]}]);
|
||||||
}
|
}
|
||||||
code = functionFreeCode;
|
code = functionFreeCode;
|
||||||
|
|
||||||
|
@ -220,6 +235,7 @@ async function recursiveReaddir(rpath, p) {
|
||||||
const filesAmt = files.length;
|
const filesAmt = files.length;
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for (const f of files) {
|
for (const f of files) {
|
||||||
|
//if (f !== 'Isaac.d.ts') continue;
|
||||||
i++;
|
i++;
|
||||||
const filePath = path.resolve(declarationsPath, f);
|
const filePath = path.resolve(declarationsPath, f);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue