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})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, saved me a lot of time.