From 002cff99c8717fd42c6883329bfe73e51e2f0a99 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Fri, 14 Jan 2022 13:08:39 +0300 Subject: [PATCH] transpile return comments --- index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index d541197..d62023d 100644 --- a/index.js +++ b/index.js @@ -104,7 +104,7 @@ function transpile(parsed, indent, prefix, gl) { 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}`, global)).join(n)}\n`; case 'function': if (d.arguments[0] && d.arguments[0].type === 'void') d.arguments = d.arguments.slice(1); - return `${comments}${d.arguments.map(p => `---@param ${p.name.replace('?', '')} ${transpileType(p.type)}${p.comment ? ' @' + p.comment : ''}${n}`).join('')}---@return ${transpileType(d.returns)}${n}function ${prefix ? prefix + (gl ? '.' : ':') : ''}${d.name}(${d.arguments.map(a => a.name.replace('?', '')).join(', ')}) end`; + return `${comments}${d.arguments.map(p => `---@param ${p.name.replace('?', '')} ${transpileType(p.type)}${p.comment ? ' @' + p.comment : ''}${n}`).join('')}---@return ${transpileType(d.returns)}${d.returnsComment ? ' @' + d.returnsComment : ''}${n}function ${prefix ? prefix + (gl ? '.' : ':') : ''}${d.name}(${d.arguments.map(a => a.name.replace('?', '')).join(', ')}) end`; case 'const': return `${comments}---@type ${transpileType(d.type)}${n}${prefix ? prefix + '.' : ''}${d.name} = nil`; case 'enum': @@ -184,6 +184,8 @@ function parse(code, originalCode) { const c = comments.pop(); //if (c) c[0] += 'isaac-lua-definitions debug: comment distance = ' + (origIndex - c[1]).toString(); + let returnsComment; + // add parameter comments if (c) { c[0].split('\n').map(l => l.trim()).filter(l => l.startsWith('* @param ')).forEach(p => { @@ -193,10 +195,16 @@ function parse(code, originalCode) { let comment = p.split(' ').slice(1).join(' '); arguments[indx].comment = comment; } - }); + }); + + // add returns comments, if found + let returns = c[0].split('\n').map(l => l.trim()).find(l => l.startsWith('* @returns ')); + if (returns) { + returnsComment = returns.slice('* @returns '.length); + } } - elements.push(['function', {name: match[2], arguments: arguments, returns: match[5], comment: (c || [null])[0]}]); + elements.push(['function', {name: match[2], arguments: arguments, returns: match[5], returnsComment: returnsComment, comment: (c || [null])[0]}]); } code = functionFreeCode;