* Add option to require @prettier or @format pragma
Fixes#2397.
Inspired by `eslint-plugin-prettier` and the discussion in #2397, this
implements requiring a special comment pragma to be present in a file's
first comment in order to be formatted.
This will help large codebases gradually transition to prettier over
time without tons of churn or large code reviews.
I implemented this as a standard prettier "option", not just a typical
`argv` flag, as it is relevant in both the cli and the api. This way it
can be provided programmatically, on the command line, or standardized
in a prettierrc file so like the style options, every user can use this
setting consistently and only apply prettier to relevant files, no
mattier their editor integration.
This requires the pragma begin with `@` (in fact it's inserted if the
user doesn't provide it). Currently the usage implies it must be
"prettier" or "format", but it can technically be any value other than
"none", which is similar to the `trailingCommas` option.
cc @vjeux
* Don't quote anything in runPrettier; this is usually handled by a shell
* Make --require-pragma a boolean option
* Use jest-docblock to find pragmas without parsing the ast
* Clarify docs
* includes -> indexOf
* Move test out of integration
- Move the very important options --write and --list-different to the
top, in the new Output category. It could be argued that --help and
--version also belong there, but I don't think they deserve such a
prominent position, so they are left in the Other category.
--find-config-path could also be considered an Output option, but I
think it fits better in the Config category.
- Now that there's an Output category, should there also be an Input
category? I thought about that, but the only candidates for such a
category are --stdin and --stdin-filepath and I don't think they are
important enough to have their own category, so they stay in Other.
- Move --cursor-offset, --range-end and --range-start to a new Editor
category, since they are mainly for editor integrations.
- Document how Prettier deals with input and output by default.
- Move Format options first, since they feel the most important.
- Move --find-config-path to the Config category.
- Merge Command into Other. It wasn't clear what "Command" options are,
and the Other category isn't too long anyway.
- Move --range-end and --range-start into Other. The aren't Format
options.
* Implement cosmiconfig
* Add resolveOptions API and extname support
* Add --resolve-config and --config, rename resolveOptions to resolveConfig
* Move color to top-level CLI options
* Fix unknown param warning
* Change from {} to null when no config is found
* Change override API to emulate eslint
* Add test for eslint-style overrides
* Delete overrides from resolveConfig
* feat(api): add custom parser API
* test(api): add integration test for parser resolution
* chore(api): fix build after resolving conflicts
* docs(api): document custom parser API
* docs(api): fix typo
* chore(build): add parse5 to build and support yarn test --prod
* Allow --debug-check to work with stdin
This makes it easier to quickly test formatting code snippets with e.g.
pbpaste | prettier --debug-check
Before, the following would happen:
$ pbpaste | prettier --debug-check
stdin: TypeError: Invalid data, chunk must be a string or buffer, not undefined
at WriteStream.Socket.write (net.js:693:11)
at writeOutput (/Users/josephfrazier/workspace/prettier/bin/prettier.js:375:18)
at getStdin.then.input (/Users/josephfrazier/workspace/prettier/bin/prettier.js:275:7)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:169:7)
$
Now, it looks like this:
$ pbpaste | prettier --debug-check
(stdin)
$
* Test that --debug-check still prints filenames
See https://github.com/prettier/prettier/pull/2013#discussion_r120499306