prettier/README.md

115 lines
3.8 KiB
Markdown
Raw Normal View History

# Prettier
Prettier is an opinionated JavaScript formatter. It removes all
original styling and ensures that all outputted JavaScript conforms to
a consistent style.
*Warning*: This is a **beta**, but should solidify fairly quickly.
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.
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!
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.
For example, take the following code:
```js
foo(arg1, arg2, arg3);
```
That looks like the right way to format it. However, we've all run
into this situation:
```js
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
2016-12-23 21:40:43 +03:00
```
Suddenly our previous format for calling function breaks down because
this is too long. What you would probably do is this instead:
```
foo(
reallyLongArg(),
omgSoManyParameters(),
IShouldRefactorThis(),
isThereSeriouslyAnotherOne()
);
```
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.
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:
```js
foo({ num: 3 },
1, 2)
foo(
{ num: 3 },
1, 2)
foo(
{ num: 3 },
1,
2
)
```
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.
## Technical Details
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.
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
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.
More (rough) details can be found in [commands.md](commands.md).
Better docs will come soon.
## Editors
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
## Contributing
2016-11-29 20:26:03 +03:00