Skip to content

Instantly share code, notes, and snippets.

@vkz
Last active February 6, 2023 17:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkz/c87186074d613cddbcf4 to your computer and use it in GitHub Desktop.
Save vkz/c87186074d613cddbcf4 to your computer and use it in GitHub Desktop.
Preserve comments with Esprima + Escodegen combination
// escodegen has a fucked up API for attaching comments
// https://github.com/estools/escodegen/issues/10
var esprima = require("esprima");
var es = require("escodegen");
var ast = esprima.parse(
'//comment here\nvar answer = 42;',
{range: true, tokens: true, comment: true});
// attaching comments is a separate step
ast = es.attachComments(ast, ast.comments, ast.tokens);
console.log(es.generate(ast, {comment: true}));
@GauthamBanasandra
Copy link

Thanks.

@QuotableWater7
Copy link

Thanks! I was wondering what was going on and this snippet saved me some debug time 👍

@aurelhann
Copy link

Great !! Super thx

@mflorence99
Copy link

Many thanks, I was scratching my head for a while!

@Ardweaden
Copy link

Thanks, saved me a lot of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment