prettier с фиксом для поддержки скобок с новой строки
 
 
 
 
 
 
Go to file
James Long 612e3411cb Move atom package out and start new readme 2017-01-10 00:45:11 -05:00
bin Document more CLI flags in usage text 2017-01-09 23:22:58 -05:00
editors Move atom package out and start new readme 2017-01-10 00:45:11 -05:00
src Document more CLI flags in usage text 2017-01-09 23:22:58 -05:00
tests Add special case for comment at top of file; regenerate snapshots 2017-01-09 21:49:26 -05:00
tests_config Add option to include spaces inside object/array literals, default to false 2017-01-04 17:23:07 -05:00
.gitignore initial 2016-11-29 12:14:10 -05:00
README.md Move atom package out and start new readme 2017-01-10 00:45:11 -05:00
commands.md Move atom package out and start new readme 2017-01-10 00:45:11 -05:00
index.js Use babylon directly and convert recast's comment algorith to use our own API 2017-01-09 17:37:45 -05:00
package.json Fix up atom package 2017-01-09 23:44:12 -05:00

README.md

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 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!

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:

foo(arg1, arg2, arg3);

That looks like the right way to format it. However, we've all run into this situation:

foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());

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:

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's printer with it's algorithm replaced by the one described by Wadler in "A prettier printer". 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.

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. 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.

Contributing