On Wed, Feb 27, 2013 at 7:02 PM, Yusuke SUZUKI <
utata...@gmail.com> wrote:
>> If you can't wait, it should be rather easy to attach the comments via
>> a post-processing step. I remember someone did that, I can't recall
>> who/where.
>
>
> escodegen.attachComments do this :)
Thanks, Yusuke :) Now I remember everything!
Here's a simple example program (run with Node.js):
var esprima = require('esprima');
var escodegen = require('escodegen');
var code = 'function foo() { // Answer\n return 42 }';
var syntax = esprima.parse(code, { tokens: true, range: true,
comment: true });
syntax = escodegen.attachComments(syntax, syntax.comments, syntax.tokens);
console.log(JSON.stringify(syntax.body, null, 2));
You would see that ReturnStatement node will have "// Answer" in its
leadingComments.