* Improve ESLint setup
- Uses `eslint:recommended` + a handful more rules.
- Uses .eslintignore so that editors can understand which files to lint.
- Uses .eslintrc<strong>.js</strong> so more editors get syntax highlighting.
* Fix ESLint errors
About half of them were fixed by `eslint . --fix`.
* Allow `--write` to be used with `--list-different`
This makes it possible to simultaneously check for formatting errors and
fix them, which can be useful for CI services. For example, a CI service
could run:
prettier --list-different --write $FILES || git diff --exit-code
to show the formatting errors. Before this change, it would be necessary
to do:
prettier --list-different $FILES || (prettier --write $FILES; git diff --exit-code)
Here are some commands which are useful to verify that the outputs and
exit codes of `prettier --list-different` and `prettier --write` have
not changed:
./bin/prettier.js --list-different index.js src/*.js bin/*.js; echo $?
./bin/prettier.js --write index.js src/*.js bin/*.js; echo $?
* fixup! Allow `--write` to be used with `--list-different`
* Run AST comparison tests on Travis
It looks like some of these currently fail, so we should probably also
sort that out.
Inspired by https://github.com/prettier/prettier/issues/1552
* tests: Use specified parser when AST_COMPARE=1
This fixes some of the tests with AST_COMPARE=1
* Move cleanAST() into prettier.__debug
This makes it available for tests to use.
* AST_COMPARE=1 uses cleanAst() instead of removeEmptyStatements()
Ths fixes some of the tests with AST_COMPARE=1
* Export parse() from src/parser.js
This makes it available for tests to use.
* tests: Use specified parser more when AST_COMPARE=1
This is a continuation of commit 86437a66d326919897fe89891a25824870f5bb79
This fixes some of the tests with AST_COMPARE=1
* massageAST: remove leadingComments/trailingComments
This fixes some of the tests with AST_COMPARE=1
* massageAST: remove `extra`
This fixes some of the tests with AST_COMPARE=1
* tests_config/run_spec.js: Rename variables for clarity
* AST_COMPARE=1 tests compare unstringified objects
This makes the test error output shorter.
* fixup! Export parse() from src/parser.js
* Revert "Run AST comparison tests on Travis"
See https://github.com/prettier/prettier/pull/1553#issuecomment-300027747
This reverts commit 49873a956c532f23fd216551a35ae35c1a18407e.
* fixup! fixup! Export parse() from src/parser.js
* parser: Require babel-code-frame only when needed
This addresses:
* https://github.com/prettier/prettier/pull/1553#discussion_r115386253
* https://github.com/prettier/prettier/pull/1553#discussion_r115386250
* parser: Don't export now-unused parseWith* functions
Addresses https://github.com/prettier/prettier/pull/1553#discussion_r115386964
* Move cleanAST/massageAST into own file, don't export
This addresses:
* https://github.com/prettier/prettier/pull/1553#discussion_r115386993
* https://github.com/prettier/prettier/pull/1553#discussion_r115386611
* Don't destructure require() result (Node v4 compat.)
* Fix copy/paste error
* Add typescript as a valid parser value
Now that typescript development has started, each contributor has been adding this as a supported flag themselves.
This just adds it as an allowed option, with a warning that support for this parser is experimental.
I also tried to structure this a bit future proof, in case other experimental parsers come along.
* Simplifies the code at vjeux's request
Rather than a notion of "experimental" parsers, we'll just leave typescript out of the documentation until it's supported.
* Add check method to Prettier. Make CLI use that method for list-different
* Catch in check and return false if it throws
* Remove catch/finally from Prettier list-different bin
* remove try catch in prettier bin for list-different
@cpojer made a great suggestion to use `fs.readFileSync` instead of the async version. prettier is CPU-bound and if you want parallelism you can spin up many instances of prettier.
Fixes#994
* Make `--write` logs more informative.
* Add format duration to logs.
* Use colors instead of [ignore] and [update] labels.
* Remove `colors` dependency.
* Use `chalk` to colorize logs.
* Print filename before `format` and add duration info later.
* Print filename before read error.
This let's you run `pp(x) === pp(pp(x))` across an entire codebase quickly. I think it's small enough that we can merge it in to master while we are nailing down all the issues.
You can see the output in #610
* Add `--help` CLI flag
* Don't pick up unknown CLI options
This prevents people from adding new CLI options in the future, but
forgetting to add it explicitly to minimist, resulting in a false
"Ignored unknown option" warning.
* Add `-h` and `-v` option aliases
It always bugs me when those don't do `--help` and `--version` for no
reason in CLIs.
* Allow `echo 'test' | prettier` without the `--stdin` flag
* Improve CLI error handling and validation
- Handle errors the same way both when using stdin and when using files.
- Print validation errors nicely.
- Validate int options, instead of silently ignoring bad input.
- Warn about unknown parsers, falling back to babylon. If a new parser
is added in the future, this allows graceful degradation for tools
running an older version of prettier. (Just like how unknown options
are warnings instead of errors.)
- Add comments.
* Run prettier on bin/prettier.js
If you use the `--write` option, the files will end with a single
newline as expected. But if you let prettier print to stdout instead and
redirect that into a file, it will contain _two_ newlines at the end.
Demonstration of the correct `--write` behavior:
```
$ cat tmp.js
function test() {
return "hello";
}
$ wc -l tmp.js
3 tmp.js
$ ./bin/prettier.js tmp.js --write
tmp.js
$ cat tmp.js
function test() {
return "hello";
}
$ wc -l tmp.js
3 tmp.js
```
Notice how an extra line is added when redirecting stdout:
```
$ ./bin/prettier.js tmp.js > tmp2.js
$ cat tmp2.js
function test() {
return "hello";
}
$ wc -l tmp2.js
4 tmp2.js
```
With this commit things work as expected:
```
$ ./bin/prettier.js tmp.js > tmp2.js
$ cat tmp2.js
function test() {
return "hello";
}
$ wc -l tmp2.js
3 tmp2.js
```
Fixes#377.
* Use babel-code-frame for syntax errors
* Support the `--color` option more explicitly
* Update rollup config to handle babel-code-frame
* Use exact dependencies
- Add missing options to minimist. This removes "Ignored unknown
option:" warnings for options that actually exists.
- Put more interesting options closer to the top.
- Consistenly use full sentences.
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:

The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
The previous API was inconsistent. The new one is
```js
--parser flow
--parser babylon
{parser: 'flow'}
{parser: 'babylon'}
```
if we ever want to add new parsers in the future it'll allow that more easily.
I put a console.log in parser.js in both functions and tested that the test suite worked both with and without the change in run_spec. I also tested that both the previous and new command line options are working.
At some point in the future we'll likely want to get rid of the old api but might as well keep supporting it so we don't break anyone for now.