escodegen icon indicating copy to clipboard operation
escodegen copied to clipboard

Comment(s) in parenthesis statement being removed

Open lucivpav opened this issue 5 years ago • 3 comments

Problem with escodegen.attachComments (and maybe also with esprima.parse). See https://github.com/estools/escodegen/blob/master/test/comment.js or #427 PR for reproduction.

Input:

function foo(a, b, c) {
    return (
        (a >= b && a <= c)

        // lorem
        // ipsum
        || a === 42 || a === 666
    );
}

Output:

function foo(a, b, c) {
    return a >= b && a <= c || a === 42 || a === 666;
}

Suggested output:

function foo(a, b, c) {
    // lorem
    // ipsum
    return a >= b && a <= c || a === 42 || a === 666;
}

lucivpav avatar Oct 07 '20 12:10 lucivpav

the more problematic scenario is when having an arrow function returning an object expression in this scenario the generated code is actually invalid

() => (
// comment
{
  a: 1, b: 2
})

output:

() => // comment
{
    a: 1,
    b: 2
});

suggested output:

() => ( // comment
{
    a: 1,
    b: 2
)});

Meir017 avatar Oct 13 '20 07:10 Meir017

@Meir017 looks like this belongs to the related issue #365. Nice catch!

lucivpav avatar Oct 13 '20 07:10 lucivpav

@lucivpav we also need to support this syntax: var a = b => ({}.hasOwnProperty.call(b, "c")); which actually translates to a CallExpression

Itazulay avatar Jan 21 '21 08:01 Itazulay