Join GitHub today
Add support for flow typecast comments #5280
This attempts to fix the bug described in #719. Previously, Prettier was removing parentheses in expressions like:
turning them into:
which no longer satisfies Flow's syntax. With this change, we detect these typecast expressions and attempt to keep the parentheses intact in such cases.
This is my first time contributing to Prettier, so I'm probably making some egregious mistakes!
We need to be careful with that whitespace trimming though, because newlines do not seem to be supported. A goal of Prettier is to never break your code. Here's a case where it could break: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVxhgIIGMAuArgIYxj4CeADgKZjEB2Dc+x+AlnAwFyoBuxAE70woMGG5gGhALYAjGsJRgAvGACs6TGAByLEZVr0mLNpx78hYOaJCpJAfmnzFyLGs3aAKoIphBNDJwfOwMAObkABZ0DDQIMKF0ClBwAVF0uHDwDGCAyARgFHCEAOQwZGE0+AbUdIqCqQCE6ALCuKoiADzWQA
/* @flow */ // Actual type annotation: var a /* : number */ = 5 // Not a type annotation: var b /* : ?number */ = 5 // Try removing the newline before the colon – you'll get a type error! var c = a < b