Try running prettier on the codebase #23
Closed
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.
DO NOT MERGE
This is a spike to see what impact running prettier on our code would have.
If this works, the idea would be that we would disable any ESLint formatting rules and replace them with the use of automatic formatting by prettier.
I ran prettier on each JavaScript file as
prettier —single-quote —write file.js
.Based on these results (and running it on a client's React Native codebase), I have the following observations so far:
We’d have to be OK with some changes to our current formatting. IMO, if the community adopts prettier’s opinionated formatting, then this is an acceptable tradeoff. I’m willing to give up being a special snowflake in the name of community consistency.
Prettier adds trailing semi-colons with no option to disable them. If we use an editor auto-fix integration, we won’t have to type them ourselves at least.
Prettier has an option to add or not add spaces inside array brackets and curly braces. Our current style is to add them for curly braces but not brackets. One of the things we’d have to live with, but there is an open issue to consider splitting these two.
Promise chains get formatted like pipelines. Not sure what I think about this, but can probably live with it (see
app/index.js
for an example).It incorrectly formats code using the Babel export-extensions-transform by adding incorrect curly braces. I’ve opened an issue for this.
Strings in JSX are always written with double-quotes regardless of the command-line option. This is apparently intentional. See General JSX Improvements prettier/prettier#73 (comment) and General JSX Improvements prettier/prettier#73 (comment) for some of the reasoning.
It deletes comments in some cases. See
app/templates/client/modules/app/reducer/index.js
for an example.It likes to squash multi-line object literals down to one line if they'll fit. This makes some code hard to read, especially in react-native stylesheet files.
It likes to squash Ramda
compose
pipelines down to one line if they'll fit. This also makes some code hard to read.It formats multi-line React components differently; I'm ok with this:
Ours:
Prettier:
Conclusion: Promising, but not yet ready for prime time. Worth keeping an eye on it and trying again in a month or two.