transpile return comments

This commit is contained in:
Jill 2022-01-14 13:08:39 +03:00
parent 3f4af8f9a9
commit 002cff99c8
1 changed files with 11 additions and 3 deletions

View File

@ -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;