Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2017-11-07 14:39:07 +11:00
---
id: index
title: What is Prettier?
---
Prettier is an opinionated code formatter with support for:
- JavaScript (including experimental features)
2018-05-27 15:37:44 -03:00
- [JSX](https://facebook.github.io/jsx/)
2024-11-25 16:40:45 +01:00
- [Angular](https://angular.dev/)
- [Vue](https://vuejs.org/)
2018-05-27 15:37:44 -03:00
- [Flow](https://flow.org/)
- [TypeScript](https://www.typescriptlang.org/)
2024-08-26 09:26:16 +09:00
- CSS, [Less](https://lesscss.org/), and [SCSS](https://sass-lang.com)
- [HTML](https://en.wikipedia.org/wiki/HTML)
- [Ember/Handlebars](https://emberjs.com/)
2020-05-10 20:00:02 +09:00
- [JSON](https://json.org/)
2019-11-05 20:25:28 +09:00
- [GraphQL](https://graphql.org/)
- [Markdown](https://commonmark.org/), including [GFM](https://github.github.com/gfm/) and [MDX v1](https://mdxjs.com/)
2019-11-05 20:25:28 +09:00
- [YAML](https://yaml.org/)
- [Lightning Web Components (LWC)](https://developer.salesforce.com/developer-centers/lightning-web-components)
- [MJML](https://mjml.io/)
2017-11-07 14:39:07 +11:00
2019-11-08 19:49:32 +08:00
It removes all original styling[\*](#footnotes) and ensures that all outputted code conforms to a consistent style. (See this [blog post](https://jlongster.com/A-Prettier-Formatter))
2017-11-07 14:39:07 +11:00
Prettier takes your code and reprints it from scratch by taking the line length into account.
For example, take the following code:
<!-- prettier-ignore -->
2017-11-07 14:39:07 +11:00
```js
foo(arg1, arg2, arg3, arg4);
```
It fits in a single line so it’s going to stay as is. However, we've all run into this situation:
2017-11-07 14:39:07 +11:00
2017-11-22 19:27:40 +08:00
<!-- prettier-ignore -->
2017-11-07 14:39:07 +11:00
```js
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
```
Suddenly our previous format for calling function breaks down because this is too long. Prettier is going to do the painstaking work of reprinting it like that for you:
```js
foo(
reallyLongArg(),
omgSoManyParameters(),
IShouldRefactorThis(),
isThereSeriouslyAnotherOne(),
);
```
Prettier enforces a consistent code **style** (i.e. code formatting that won’t affect the AST) across your entire codebase because it disregards the original styling[\*](#footnotes) by parsing it away and re-printing the parsed AST with its own rules that take the maximum line length into account, wrapping code when necessary.
2017-11-07 14:39:07 +11:00
If you want to learn more, these two conference talks are great introductions:
[![A Prettier Printer by James Long on React Conf 2017](/images/youtube-cover/a-prettier-printer-by-james-long-on-react-conf-2017.png)](https://www.youtube.com/watch?v=hkfBvpEfWdA)
2017-11-07 14:39:07 +11:00
[![JavaScript Code Formatting by Christopher Chedeau on React London 2017](/images/youtube-cover/javascript-code-formatting-by-christopher-chedeau-on-react-london-2017.png)](https://www.youtube.com/watch?v=0Q4kUNx85_4)
2017-11-07 14:39:07 +11:00
#### Footnotes
2017-12-01 10:59:17 +08:00
\* _Well actually, some original styling is preserved when practical—see [empty lines](rationale.md#empty-lines) and [multi-line objects](rationale.md#multi-line-objects)._