escodegen icon indicating copy to clipboard operation
escodegen copied to clipboard

Reproduce issue #426

Open lucivpav opened this issue 5 years ago • 5 comments

Reproduces #426

lucivpav avatar Oct 07 '20 13:10 lucivpav

@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

Meir017 avatar Oct 12 '20 10:10 Meir017

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

lucivpav avatar Oct 14 '20 08:10 lucivpav

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

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

Meir017 avatar Oct 14 '20 08:10 Meir017

@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, ')']; }

Itazulay avatar Jan 21 '21 08:01 Itazulay

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

lucivpav avatar Jan 31 '21 10:01 lucivpav