escodegen icon indicating copy to clipboard operation
escodegen copied to clipboard

Added newlines may break return statements

Open 00dani opened this issue 9 years ago • 2 comments

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.

00dani avatar Sep 25 '15 06:09 00dani

+1 to this. Is there any way to keep comments without breaking returns in some cases?

sidhu663 avatar Apr 15 '16 23:04 sidhu663

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

azu avatar Nov 07 '16 08:11 azu