escodegen
escodegen copied to clipboard
Added newlines may break return statements
This script should produce functionally-identical JS as output:
const js = `
var example = function() {
return /*@ngInject*/ function($scope) {
return $scope;
};
};
`;
const ast = esprima.parse(js, {range: true, comment: true, tokens: true});
escodegen.attachComments(ast, ast.comments, ast.tokens);
console.log(escodegen.generate(ast, {comment: true}));
However, the actual output is:
var example = function () {
return /*@ngInject*/
function ($scope) {
return $scope;
};
};
The inserted newline between the return
statement and the function literal causes the return
to be interpreted as return;
by automatic semicolon insertion, so the function ends up entirely broken.
+1 to this. Is there any way to keep comments without breaking returns in some cases?
I have a same issue.
Reproduce code:
var esprima = require('esprima');
var escodegen = require('escodegen');
var code = `(function(){
return /* comment */ function (){}
})()`
var ast = esprima.parse(code, {
range: true,
tokens: true,
comment: true
});
expected:
(function () {
return /* comment */ function () {
};
}());
actual:
(function () {
return /* comment */
function () {
};
}());
- https://runkit.com/azu/582039af9561080014e2a239