estools / escodegen

ECMAScript code generator
BSD 2-Clause "Simplified" License
2.65k stars 335 forks source link

Reproduce issue #426 #427

Open lucivpav opened 3 years ago

lucivpav commented 3 years ago

Reproduces #426

Meir017 commented 3 years ago

@lucivpav I just ran into this issue too. I found a solution for this - https://github.com/estools/escodegen/blob/ab53cd5489fd15c3624386465d1f7a0544cda6c8/escodegen.js#L946-L949

this should be

var strExpr = expr.toString();
if (strExpr[0] === '{' || strExpr[strExpr.length - 1] === '}') {
  expr = ['(', expr, ')'];
}

I'm pretty sure the last character cannot be part of the comment, in that case the comment would not be part of the node

lucivpav commented 3 years ago

I wonder if this fix works for the affected parenthesized expressions in general, or just in function bodies 🤔

Meir017 commented 3 years ago

@lucivpav a better solution is to check the node types:

if (node.type === Syntax.ArrowFunctionExpression && node.body.type === Syntax.ObjectExpression) {
  expr = ['(', expr, ')'];
}
Itazulay commented 3 years ago

@lucivpav This PR does not cover this scenario: var a = b => ({}.hasOwnProperty.call(b, "c")); This is a CallExpression and not an ObjectExpression. Maybe it should be simply: if (node.type === Syntax.ArrowFunctionExpression) { expr = ['(', expr, ')']; }

lucivpav commented 3 years ago

@Itazulay my new PR is implementing it properly IMO https://github.com/estools/escodegen/pull/437/files#diff-f422139858ace4feaa93c12eac40aff386df95122af5cf2fc1874bf946ef8578R294