Fix parens around comment at object-destructuring assignment #1129
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #575.
We were taking code like the following:
and wrapping the comment in parens, which is invalid syntax:
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
isan 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 thething 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
willactually 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 withnode
.Also add a regression test.