diff --git a/index.js b/index.js index c281732..a87322d 100644 --- a/index.js +++ b/index.js @@ -177,7 +177,19 @@ function parse(code, originalCode) { while ((match = enums.exec(code)) !== null) { parsedElements++; enumFreeCode = enumFreeCode.replace(match[0], ''); - elements.push(['enum', {name: match[1], contents: match[2].split(',').filter(c => c.split('=').length === 2).map(c => {return {name: c.split('=')[0].trim(), value: c.split('=')[1].trim()}})}]); + + 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(['enum', {name: match[1], contents: match[2].split(',').filter(c => c.split('=').length === 2).map(c => {return {name: c.split('=')[0].trim(), value: c.split('=')[1].trim()}}), comment: (c || [null])[0]}]); } code = enumFreeCode; @@ -187,7 +199,19 @@ function parse(code, originalCode) { while ((match = consts.exec(code)) !== null) { parsedElements++; constFreeCode = constFreeCode.replace(match[0], ''); - elements.push(['const', {name: match[1], type: match[2]}]); + + 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(['const', {name: match[1], type: match[2], comment: (c || [null])[0]}]); } code = constFreeCode;