Emit needed parens for all kinds of Flow types #1127
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.
When printing a Flow type expression, we were mostly neglecting to print parentheses where they're needed. A few specific cases were covered, but many others were not.
I ran into one of those cases in practice:
A & (B | C)
was getting printed asA & B | C
, which means something different. After looking into it, I decided to go about just fixing this whole class of issues. Turns out not to need all that much code!This includes a thorough set of test cases, which I wrote up before writing the fix and helped me figure out what the fix should look like. If we run this PR's test cases without the fixes it makes in
lib/
(but with #1089, which enhances the test output and also fixes unrelated bugs that some of these tests would hit), the Mocha results summary is:Each of those numbered lines (with no ✔) is a case where the code before this PR would drop a pair of parentheses that shouldn't be dropped. For all but three of them (18, 19, 20), the resulting output wouldn't parse the same way. So the other 25 test cases represent situations where the existing code gets a wrong result.