2017-01-10 08:45:11 +03:00
|
|
|
# Prettier
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
Prettier is an opinionated JavaScript formatter. It removes all
|
|
|
|
original styling and ensures that all outputted JavaScript conforms to
|
|
|
|
a consistent style.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
*Warning*: This is a **beta**, but should solidify fairly quickly.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
This goes way beyond [eslint](http://eslint.org/) and other projects
|
|
|
|
[built on it](https://github.com/feross/standard). 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.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
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!
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
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.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
For example, take the following code:
|
2016-12-23 21:38:10 +03:00
|
|
|
|
|
|
|
```js
|
2017-01-10 08:45:11 +03:00
|
|
|
foo(arg1, arg2, arg3);
|
2016-12-23 21:38:10 +03:00
|
|
|
```
|
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
That looks like the right way to format it. However, we've all run
|
|
|
|
into this situation:
|
2016-12-23 21:38:10 +03:00
|
|
|
|
|
|
|
```js
|
2017-01-10 08:45:11 +03:00
|
|
|
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
|
2016-12-23 21:40:43 +03:00
|
|
|
```
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
Suddenly our previous format for calling function breaks down because
|
|
|
|
this is too long. What you would probably do is this instead:
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
```
|
|
|
|
foo(
|
|
|
|
reallyLongArg(),
|
|
|
|
omgSoManyParameters(),
|
|
|
|
IShouldRefactorThis(),
|
|
|
|
isThereSeriouslyAnotherOne()
|
|
|
|
);
|
|
|
|
```
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
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.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
Even if we disregard line widths, it's too easy to sneak in various
|
|
|
|
styles of code in all other linters. The most strict linter I know
|
|
|
|
happily lets all these styles happen:
|
2016-12-23 21:38:10 +03:00
|
|
|
|
|
|
|
```js
|
2017-01-10 08:45:11 +03:00
|
|
|
foo({ num: 3 },
|
|
|
|
1, 2)
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
foo(
|
|
|
|
{ num: 3 },
|
|
|
|
1, 2)
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
foo(
|
|
|
|
{ num: 3 },
|
|
|
|
1,
|
|
|
|
2
|
|
|
|
)
|
|
|
|
```
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
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.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
## Technical Details
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
This printer is a fork of
|
|
|
|
[recast](https://github.com/benjamn/recast)'s printer with it's
|
|
|
|
algorithm replaced by the one described by Wadler in "[A prettier
|
|
|
|
printer](http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf)".
|
|
|
|
There still may be leftover code from recast that needs to be cleaned
|
|
|
|
up.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
The basic idea is that the printer takes an AST and returns an
|
|
|
|
intermediate representation of the output, and the printer uses that
|
|
|
|
to generate a string. The advantage is that the printer can "measure"
|
|
|
|
the IR and see if the output is going to fit on a line, and break if
|
|
|
|
not.
|
2016-11-29 20:26:03 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
This means that most of the logic of printing an AST involves
|
|
|
|
generating an abstract representation of the output involving certain
|
|
|
|
commands. For example, `concat(["(", line, arg, line ")"])` would
|
|
|
|
represent a concatentation of opening parens, an argument, and closing
|
|
|
|
parens. But if that doesn't fit on one line, the printer can break
|
|
|
|
where `line` is specified.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
More (rough) details can be found in [commands.md](commands.md).
|
|
|
|
Better docs will come soon.
|
2016-12-23 21:38:10 +03:00
|
|
|
|
|
|
|
## Editors
|
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
Currently atom and emacs support is provided. Atom users can simply
|
|
|
|
install the `prettier-atom` package and use ctrl+alt+f to format a
|
|
|
|
file (or format on save if turned on). Emacs users should see [this
|
|
|
|
folder](https://github.com/jlongster/prettier/tree/master/editors/emacs).
|
2016-11-29 20:26:03 +03:00
|
|
|
|
2017-01-10 08:45:11 +03:00
|
|
|
## Contributing
|
2016-11-29 20:26:03 +03:00
|
|
|
|