Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken parens on return statement #365

Open
retrohacker opened this issue Mar 19, 2018 · 5 comments
Open

Broken parens on return statement #365

retrohacker opened this issue Mar 19, 2018 · 5 comments

Comments

@retrohacker
Copy link

retrohacker commented Mar 19, 2018

Repro

escodegen strips parenthesis from return statements, changing the behavior of the code. This breaks ast serialization/deserialization for react here.

var acorn = require('acorn');
var escodegen = require('escodegen');

var file = `
console.log((function () {
    return (
        // do a thing
        'hello world'
    );
})());
`;

var tokens = [];
var comments = [];

var ast = acorn.parse(file,
    {
        onToken: tokens,
        onComment: comments,
        ranges: true,
    });
escodegen.attachComments(ast, comments, tokens);
console.log(escodegen.generate(ast, { comment: true }));

Output

original

console.log((function () {
    return (
        // do a thing
        'hello world'
    );
})());
hello world

generated

console.log(function () {
    return // do a thing
    'hello world';
}());
undefined
@retrohacker
Copy link
Author

This only happens when comments are included inside the parentheses.

micschro added a commit to micschro/escodegen that referenced this issue Apr 10, 2018
Fix broken parens on return statements with leading comments
micschro added a commit to yWorks/escodegen that referenced this issue Feb 1, 2019
@micschro
Copy link

Same problem for throw statements.

micschro added a commit to yWorks/escodegen that referenced this issue Feb 26, 2019
micschro added a commit to yWorks/escodegen that referenced this issue Feb 26, 2019
@lucivpav
Copy link

lucivpav commented Oct 7, 2020

This issue is still present in v2.0.0.

@lucivpav
Copy link

lucivpav commented Oct 13, 2020

The problem is more general. An expression that is within parentheses and contains a comment in the beginning, compiles incorrectly.
Example:

let variable = (
  // ahoj
  3+3
);

compiles into:

let variable = // ahoj
3+3;

This does not happen in all cases, such as it does not happen if the expression is within an if condition.

Note: the compiled code still works correctly, so this may not be a good example of the problem, but I think the parentheses should still be generated.

@lucivpav lucivpav mentioned this issue Oct 19, 2020
@lucivpav lucivpav mentioned this issue Dec 9, 2020
@lucivpav lucivpav mentioned this issue Dec 21, 2020
@lucivpav
Copy link

lucivpav commented Jan 4, 2021

The issue is about wrong code generation when using comments in return, throw and arrow function expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants