-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix missing parens in reprinted output #1068
Conversation
Oh hmm, nope it's not quite right yet. I found a test that breaks things. |
I'm pretty confident that my fix is correct, but I'm seeing a problem in the typescript test. |
Oops, didn't mean to send that yet. The problem is that |
It seems like I could just tweak the logic to ensure that neither of those is dependent on |
By the way this is only an issue because I've removed the |
Though actually that line only handled the case when |
1afa93b
to
2956890
Compare
So I think I've fixed those problems. I also renamed |
As it sits now this PR has one failing test that I'm not sure what to do about. The test case boils down to this: assert.strictEqual(
new Printer().print(b.functionExpression(/*...*/)).code,
"function a(b, c = 1, ...d) {}",
); which results in the following error:
I definitely broke this by allowing the root node of an expression AST to need parens, and I read the result as malicious compliance. For |
Ah ok, I'm putting it back exactly the way it was. All the tests pass again now. It looks a little different because it is still necessary to check That ensures that both the formerly broken case and |
054daa1
to
8d327d6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I strongly recommend reviewing this with the "Hide whitespace" option on as a large block was reindented.
if (!parent) { | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We delay this check so that we can reprint (function () {} ())
, which otherwise will always have its outermost parens stripped since it has no parent.
if (node.extra && node.extra.parenthesized) { | ||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with short circuiting to false
as we did previously is that a node may still really need parens (that is, non-decoratively). I opted to make this code responsible for decorative parens which as far as I can tell works fine, though it isn't apparent to me yet if there is some reason the code was written the way it was. The test suite seems to indicate that there is not, or at least is no longer.
@@ -5,6 +5,7 @@ import "./identity"; | |||
import "./jsx"; | |||
import "./lines"; | |||
import "./mapping"; | |||
import "./parens-extra"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d'oh
|
||
expr.body.properties = []; | ||
|
||
assert.strictEqual(printer.print(ast).code, "() => ({})"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't yet been able to verify that this test fails if the behavior regresses.
|
||
function check(expr: string) { | ||
const ast = parse(expr); | ||
|
||
const reprinted = printer.print(ast).code; | ||
assert.strictEqual(reprinted, expr); | ||
assert.strictEqual(expr, reprinted); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected is first
8d327d6
to
eb94a84
Compare
@coderaiser Sorry about that. The test cases for recast are really rather inadequate it seems. I'm looking into these issues. |
@coderaiser I'm sorry, at the moment I'm going to need some help from you to be able to understand how to investigate what's happening in your codebase. I've figured out how to isolate a specific test and hit breakpoints, but I'm confused. If I put a breakpoint at the beginning of the recast parse function (in |
Oh oops maybe I just focused the wrong test. |
I'm looking at the extra parens on JSX return first. The JSX node itself is Unfortunately it's pretty tricky to see what the correct fix may be.... |
Out of curiousity @coderaiser how do you feel about transitioning from recast to prettier if prettier were able to support transformations? |
@conartist6 thanks for so fast reply! Actually this additional parens not a very big deal since all the code transformed by 🐊Putout then processed by ESLint with
I will definitely try it, right now |
Fixes #1067
I am not sure the test is completely adequate, but I can verify that the bug is fixed in the minimal repro.