Prettier 1.0
jlongster.com
jlongster.com
Highlights of the 1.0 announcement:
- production-ready (and used, eg; React, Oculus, Cloudflare, etc)
- --no-semi option
- --use-tabs option
- parens that help indicate precedence
- many formatting improvements
There are a ton of edge cases in this work that really matter (read through the announcement for a few). Speaking as a contributor, this is some of the hardest stuff I've ever done (beats building a JS superset, http://lightscript.org). I've been super impressed by the prettier team.
For example this: http://whitneyland.com/2017/04/first-in-the-world-to-display...
Not sure exactly what you mean, but here's an example with JSON: https://prettier.github.io/prettier/#%7B%22content%22%3A%22x...
Prettier doesn't support JSON files per se (and doesn't currently plan to [0]), so you need to provide a leading `x = {...` or similar to ensure the { is parsed as an object instead of a block.
If you have a constant like this, except really long and ugly, there's quite a bit that can be done to make the code more readable.
Maybe I'll add an issue and see if anyone else cares about it.
var simpleJson = '{ "DevelopersNeedUXToo": [ { "Color": "Red", "Name": "Lee" } ] }'
Prettier has so far resisted configuration wherever possible; it has 8 options, compared with eslint's 260+.
I'm hoping to see similar efforts on project file/folder structure at some point. How often do two node projects created by different engineering teams end up with the same folder/file structure? There's nearly no strong community convention, unlike many other languages/frameworks. Rails, for example, famously touts that any engineer can walk onto another Rails project and have a reasonable suspicion of where to find a particular piece of code.
1: https://github.com/airbnb/javascript 2: https://twitter.com/hshoff/status/784205839347232768
The point about quotes comes up often. The important part for us is consistency at Airbnb. We use eslint to enforce and maintain the consistency. Would it be more consistent to treat strings the same everywhere? For some projects sure. We diverge from the community a bit in the sense that we treat JSX as not JS and we aim to be consistent about that. If your project treats JSX and JS the same then yes, we've certainly seen folks do this. As always, please do what works best for your team or project [1]. Forks encouraged.
I will say an added benefit of including a style guide in a project for large teams that may get lost jumping straight into prettier, has been helping our engineers become stronger javascript programmers in the process of learning our style guide. That's largely anecdotal evidence from my time at Airbnb and of course your mileage my vary.
Personally, I support the Prettier mission 100%. Easier consistency is huge. Removing barriers to contributing to open source projects is huge. PR reviews focused on what the code is doing vs painful style nits is huge. It's been great to see this come from the community and I look forward to seeing where the community takes it. Congrats to all of the contributors on the 1.0!
1. Announced 2 months ago
2. Already adopted by, it seems, every major JS project. (https://twitter.com/jlongster/status/852532847252566017)
3. 7000 stars, 100,000 npm downloads/mo
Now some people will say it is a better mousetrap, but even now I can't tell you what the difference is between it and all the other linters. I can tell you that, based on the popularity, I would just default to this one and assume all this famous projects and people have worked it out.
How did it get it so popular? What made people want to try it out? Just word of mouth?
Even with ESLint having autofix capabilities, Prettier seems far more comprehensive in that regard. It also isn't doing exactly the same things as linters. They check for programming errors as well, not just stylistic issues. With those linters, you also need to decide how to configure the rules (though picking a preset can make that easier).
I have always loved that Python has an "official" style guide (https://www.python.org/dev/peps/pep-0008/), and gofmt (https://golang.org/cmd/gofmt/) is one of the reasons I want to try out Go, so Prettier is a great development in my opinion. I am optimistic that the community will keep on adopting it, helping make our code more readable and allowing us to focus on actually building libraries, tools, applications, etc. rather than arguing over where we should and shouldn't have spaces in our code.
Edit: my TL;DR answer would be that it's a virtually zero configuration way to guarantee stylistic consistency.
Whereas this just formats everything for you, set it up on file save and you're done. No more discussions on style and whatnot, your codebase is suddenly uniform.
Go has that with `go fmt` and it's very liberating.
All that, being a great tool, and some Twitter/Medium promotion = success.
I believe there was an idea of adding it as a pre-commit hook, a second one as a manual step and another one as a post-commit amend.
Have we reached a consensus on that? Not up to date what they went with, what seems to be the best way to do this?
Formatting source code feels like a complete waste of developer time and energy, especially when they don't agree with how the team has decided to format the code. I almost wish source code files could be just serialized ASTs and that a VCS could understand them making merges etc. even easier.
It could take any sort of style in, and produce any sort of style out. Things like K&R blocks vs Allman-style blocks, tabs vs spaces, line wrapping, cuddled/not-cuddled else, etc.
http://perltidy.sourceforge.net/stylekey.html
Edit: this looks pretty close to the same thing for JavaScript. Missing some style options, and not command line driven, but the general idea seems close: https://github.com/benjamn/recast/blob/master/README.md
It might be useful to re-format a completely messed up piece of code coming from the external world, but on the team I always prefer styleguides in the mood of "recommended, never enforced", which "prettier" is exact opposite of. That is, try to write like everyone else on the team does, but avoid re-formatting the code written by someone else, if there's no real need to do so. Obvious, strictly formal things like spaces instead of tabs, spaces outside of brackets (not inside of them), etc — this is one thing. But aligning pieces code to match some meta-pattern, or using longer/shorter lines than usual for some specific, "artistic" purpose — come on, this is not such a big deal. Consistency is overrated.
Edit: If I had to program in JS I would definitely use this coming from Go.
Prettier has succeeded largely due to a better algorithm, which actually came from the haskell community, via Wadler's paper A Prettier Printer [1] (with an implementation available at [2]).
[0] js-beautify had its first commit in 2007: https://github.com/beautify-web/js-beautify/commit/e1ddc28b2...
[1] https://homepages.inf.ed.ac.uk/wadler/papers/prettier/pretti... [PDF]
I know, many people can't stand significant white space... The idea sounded weird for me too but after trying it for awhile I was hooked.
Using a tool like this you all share the same rhythm. No ones feelings get hurt, onboarding is simpler, code is easier to read.
Second, there's what I'd call the "Mark Zuckerberg clothing" reason. When asked why he always wears the same style clothes, he said, "I really want to clear my life to make it so that I have to make as few decisions as possible about anything except how to best serve this community." Similarly, code formatting doesn't deliver value to customers/users. The more you can ignore it, the more you can focus on other things. When I first started using code formatters, they irked me greatly. But once you get used to them as a part of your process and start to feel comfortable reading the code they spit out, they transition to being liberating. The formatting fades into the background and all you see is the logic in the code. And I didn't realize just how much time I was spending getting indentation levels correct and aligning things until I entirely stopped doing those things. Now I write code that looks really ugly until I hit save and then it looks exactly the way that the rest of my code looks.
Also: my code formatting has always been a bit unusual, in that I never put spaces in. Compared to what most people do, this is easier to type, because it doesn't require pressing the space bar as much. So code formatters are good for me, because I can keep not pressing space, and now nobody need ever know.
But, guess what: this means code formatters are good for you too. Because now you don't have to press space either.
I understand if the way prettier formats your code is not the way you would like, and is more of a hindrance than helpful. But I hope the above gif shows why it is freeing for a lot of people.
Also, note that prettier does keep the original blank lines in your code. That's where most of the rhythm comes from, and we allows authors to group code however they want. The rest of it is basically choosing whether to insert a space here or there or not.
I respect your work, antirez, and I hope you at least try it once! It's totally fine if it's not useful for you though.
[1] https://github.com/feross/standard/blob/master/RULES.md#semi...
If we were to implement prettier, and chose to use 2-space indents, for a simple example, how would a 4-space developer fare when they pulled down the Prettified code? Would they just set up their own config locally based on their own standards?
If you have a pair of particularly persnickety peers, you could theoretically:
1) run prettier with the company-wide config as a precommit hook (https://github.com/prettier/prettier#pre-commit-hook-for-cha...)
2) instruct your colleagues to have their editor run prettier with their own preferences on save.
This way, your colleagues can simply open a file locally and save it to see things their way – and the code will be back to company-normal when they commit. They'll still have to see that unbearable bracket-spacing during code review, of course.
Prettier doesn't have many configuration options (intentionally!) so they'd probably opt to use prettier-eslint or similar, which allows more configuration (at the cost of speed).
Hopefully, after trying this, they would come to the conclusion that life is better just using the defaults, two-space-indents be damned. Bikesheds are great places for compromise.
How are you doing it today?
See it as views. Code could be stored in a minified (spaces and new lines are not necessarily saved), and then every developer in the team can view the code in any style he chooses. His code would then be committed using the underlying "minified" format, so that the next developer can pick up that code and view it in his own code style.
Does that even make sense? What could be the gotchas of such a thing?
I sort of deal with something like that even just personally. On my work machine I have nice screen real estate, but when I'm on the go on my 11" air I have prettier set up with a more generous character limit so I can see more on screen. If you combined this set up with something like the pre-commit hooks that OITT have suggested, I could then be looking at my own code slightly differently in two machines but the end result would be the same!
Personally, I use TSLint with the tslint.autoFixOnSave VSCode setting, and couldn't be happier.
Every lint ruleset I've ever used still leaves a TON of formatting discretion to the author – notably about where to wrap. Prettier will format code differently depending on how much space there is left to work with on the line – like a human would do, but always consistent.
Unlike ESLint ... everything is fixable. This works because Prettier never "checks" anything; it ... parses your JavaScript into an AST (Abstract Syntax Tree) and pretty-prints the AST, completely ignoring any of the original formatting.
There's an extremely important piece missing from existing styling tools: the maximum line length ... (ESLint never knows how to fix it).
Going through all code at once would destroy all relevant vcs history and git blame. Maybe before each substantial edit on a file first run it through prettier and then make your real changes in a separate commit?
I write vastly different JS as Prettier formats (single quotes, no semicolons, etc.), but after I hit save everything just looks 'normalised'
I'd like to hear how people are using it on teams. With some effort I got it working on save in emacs, but asking other people to please install more tooling is not always easy.
I do have some authority over package.json though. How would you make it just work for everyone (using atom, vim, etc...)
There are integrations for most editors (Atoms, Emacs, Vim, Neovim, VSCode, VS, Sublime Text, JetBrains):
Edit: Oh here we go: https://github.com/prettier/prettier-atom
> 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.