diff --git a/index.js b/index.js index 1637403..ae91152 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ const fs = require('fs/promises'); const path = require('path'); -const { inspect } = require('util'); const chalk = require('chalk'); const declarationsPath = path.resolve(__dirname, './isaac-typescript-definitions/typings/'); @@ -69,6 +68,9 @@ function transpile(parsed, indent, prefix) { const type = parsed[0]; const d = parsed[1]; 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) { case 'namespace': global = true; @@ -84,14 +86,14 @@ function transpile(parsed, indent, prefix) { } 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': 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': - 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': - 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 ''; @@ -109,7 +111,7 @@ let parsedElements = 0; /** * @param {string} code */ -function parse(code) { +function parse(code, originalCode) { // console.log(code); // 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 // sanitize stuff + if (!originalCode) originalCode = code; code = removeComments(code); let match; @@ -128,7 +131,7 @@ function parse(code) { while ((match = interface.exec(code)) !== null) { parsedElements++; 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; @@ -138,7 +141,7 @@ function parse(code) { while ((match = namespace.exec(code)) !== null) { parsedElements++; 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; @@ -152,7 +155,19 @@ function parse(code) { const split = s.split(':').map(s => s.trim()); return {type: split[1], name: split[0]} }).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; @@ -220,6 +235,7 @@ async function recursiveReaddir(rpath, p) { const filesAmt = files.length; let i = 0; for (const f of files) { + //if (f !== 'Isaac.d.ts') continue; i++; const filePath = path.resolve(declarationsPath, f);