ArrowFunctionExpression returning ObjectPattern prints without parens when a return type is removed
conartist6 opened this issue · comments
I've created a minimal repro for this behavior here: https://github.com/conartist6/recast-arrow-object-pattern-repro
The input is:
(): Type => ({ prop: true });
and the output is invalid, as it is missing parens:
() => { prop: true };
I made the initial PR to add support for node.extra.parenthesized
as a way to declare parens, but I'm thinking I missed a case. I notice a few things.
First: fastPath.needsParens
will always return false
because node.extra.parenthesized
.
Second: I implemented the logic to check node.extra.parenthesized
and print parens in genericPrint
. But the ObjectPattern
node is not printed with genericPrint
. It has a reprinter:
Lines 107 to 126 in cb7ff9a
Third: the patcher seems to rely only on fastPath.needsParens
:
Lines 234 to 236 in cb7ff9a
So I guess I understand how it's broken now. How should it work?
I clearly made a faulty assumption when I made the initial PR, that extra.parenthesized
and ParenthesizedExpression
could be handled exactly the same way, but they're not quite the same thing. One has its own node, and one does not.
While it is true that needsParens
is always false for a ParenthesizedExpression
because that node prints itself as a set of parens, I think for the implementation to be correct it cannot possibly return false
for a node with extra.parenthesized
. A node which has that flag does always need parens, and it's presence also must not override the following logic, which dictates whether a node needs parens for any other reason. What I've actually done is short-circuited the logic that would say that an ObjectPattern
which is the body
of an ArrowFunctionExpression
must ALWAYS get parens. Oops!!!!!