Last active
February 6, 2023 17:54
-
-
Save vkz/c87186074d613cddbcf4 to your computer and use it in GitHub Desktop.
Preserve comments with Esprima + Escodegen combination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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})); |
Thanks! I was wondering what was going on and this snippet saved me some debug time 👍
Great !! Super thx
Many thanks, I was scratching my head for a while!
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
Thanks.