You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We were taking code like the following:
(/**/{foo} = 1)
and wrapping the comment in parens, which is invalid syntax:
(/**/)({foo} = 1)
The symptom appears when using the default parser, but not esprima;
and only when you call the printer directly on the AST node for the
expression, not the whole program.
The bug here was that in needsParens, we check to see if `node` is
an AssignmentExpression with ObjectPattern, and if so we say that
we do need parens... but we were making that check at the very top
of the function, before we'd checked that `node` was actually the
thing we wanted to be operating on at all.
If in fact the path currently points to a comment -- as it can
when needsParens gets called by the printer invoked by the
`print(commentPath)` in printLeadingComment or printTrailingComment
-- then the comment doesn't count as a node, and so `getNode` will
actually return the parent. If that parent meets the
AssignmentExpression / ObjectPattern condition, then we would
buggily decide that the comment needed parens too.
Fix it by checking that `node` is the actual top of the stack,
i.e. `this.getValue()`, before doing anything with `node`.
Also add a regression test.
Fixes: benjamn#575
Using recast 0.17.3:
Expected output: something like
(/**/{foo} = 1)
or{foo} = 1
Actual output:
(/**/)({foo} = 1)
The output is invalid code because the initial parentheses are only surrounding a comment and nothing else.
The text was updated successfully, but these errors were encountered: