Discussion: JS formatting - standardize on prettier?
Currently, we advocate using the airbnb eslint config (https://github.com/18F/frontend/issues/98) for linting our JS code.
What about standardizing on using prettier instead? Here's an introduction to it: http://jlongster.com/A-Prettier-Formatter.
It looks like it has good editor support (so that you can enable format on save, for example).
From the prettier README:
[prettier] goes way beyond eslint and other projects built on it. Unlike eslint, there aren't a million configuration options and rules. But more importantly: everything is fixable. This works because prettier never "checks" anything; it takes JavaScript as input and outputs the formatted JavaScript as output.
In technical terms: prettier parses your JavaScript into an AST and pretty-prints the AST, completely ignoring any of the original formatting. Say hello to completely consistent syntax!
Looks like there are a few tools that could/would be helpful for ensuring files adhere to prettier, which we'd run in CI:
@jseppi: can you outline the differences and possibly pros/cons for adopting prettier over the airbnb linter?
Note: take all this with a large grain of salt - I've not used prettier on any real project yet. I did make a quick experimental branch over in CALC: https://github.com/18F/calc/pull/1493
After doing a bit more research, I don't think it's really an either/or situation, especially with React projects. I am a big fan of Airbnb's eslint config with React projects because of the consistency it enforces in React components, down to the order of methods and ensuring that all props have a PropType specified.
I think the big benefit to adopting prettier would be even more consistency across developers in each project, and potentially over the organization as a whole.
For non-React projects, prettier might be all that's needed to enforce really good consistency and well-formatted code.
It seems like we've hard a hard time getting even the Airbnb eslint config adopted however, so maybe adding something else isn't a very realistic goal.
This is great. We know that there are different tools for different jobs, so this sounds like a case where we can think about suggesting one linter over another in certain cases. However if we want to update the guide with information like this, it would be helpful to have some data points about the advantages one linter has over another in different use cases (for example, React vs non-React).
Is there a reason prettier can't just work with an eslint config? I like the idea of a formatter, but would prefer to stick with a lint config we've used and seems popular.
I think it doesn't really work with an eslint config because it goes further with formatting than eslint can describe. I think the post from the original release of prettier does a good job of explaining this difference - http://jlongster.com/A-Prettier-Formatter. Here are some particularly salient paragraphs from it:
There's an extremely important piece missing from existing styling tools: the maximum line length. Sure, you can tell eslint to warn you when you have a line that's too long, but that's an after-thought (eslint never knows how to fix it). The maximum line length is a critical piece the formatter needs for laying out and wrapping code. ... This clearly shows that the maximum line length has a direct impact on the style of code we desire. The fact that current style tools ignore this means they can't really help with the situations that are actually the most troublesome. Individuals on teams will all format these differently according to their own rules and we lose the consistency we sought after. .. Prettier bans all custom styling by parsing it away and re-printing the parsed AST with its own rules that take the maximum line width into account, wrapping code when necessary.
The creator also gave an 8-minute lightning talk on prettier at ReactConf 2017 that I think is worth watching.
I'm excited to try this out!
Seems like prettier has been picking up some juice internally. Maybe we say that the guild recommends prettier or eslint+airbnb-config?
I admit that I do kinda like having one recommendation, because one big point of recommending a specific style is so that it is easy to go between projects without having to learn a bespoke style or even think about style at all. Maybe that's not a big issue for others though.
Yeah, I agree that having a consistent style across all our projects would be good to have.
Regardling prettier, one thing @cmc333333 pointed out is that it's not really a replacement for a linter because it only addresses the formatting-related concerns of code, but it doesn't deal with semantics at all, e.g. ensuring that you don't have any unused variables or imports. So at minimum, prettier should be used in addition to a linter, but then that means using two tools instead of just one... blargh.
Maybe not though: https://prettier.io/docs/en/eslint.html