Commit Graph

199 Commits (545a712dc02df98b1a8c973582185f54b8b43bca)

Author SHA1 Message Date
Will Binns-Smith d5e5d66407 Add option to require @prettier or @format pragma (#2772)
* 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
2017-09-13 09:03:18 -07:00
Simon Lydell 709e271051 Improve CLI usage summary and fix typo 2017-09-13 18:21:04 +08:00
Simon Lydell 45d200a06d Improve option categorization and usage summary
- 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.
2017-09-13 18:21:04 +08:00
Simon Lydell 5d435abe0a Tweak option categories
- 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.
2017-09-13 18:21:04 +08:00
Simon Lydell c99f7e2e38 Tweak option descriptions 2017-09-13 18:21:04 +08:00
Simon Lydell 4e5c808730 Fix typo in option description 2017-09-13 18:21:04 +08:00
Lucas Azzola 3f74c45b91 Support graphql(schema, `query`) (#2781) 2017-09-11 20:18:11 +02:00
长天之云 df5482008e fix(prettierignore): support absolute filename (#2783) 2017-09-10 18:21:56 +02:00
ikatyang 1a4aac8f9b refactor: put `no-option` after `option` 2017-09-10 14:10:42 +08:00
ikatyang 0c3fe2bc9f refactor: sort category 2017-09-10 13:45:18 +08:00
ikatyang b9bc54aab6 refactor: adjust category 2017-09-10 13:37:21 +08:00
ikatyang eaafbb77aa refactor: adjust warning message 2017-09-10 12:17:33 +08:00
ikatyang 4d4d8e3f4e refactor: normalizeArgv without using getter 2017-09-09 13:38:28 +08:00
ikatyang 97100c42c4 test(integration): add tests for cursor-offset 2017-09-09 12:06:50 +08:00
ikatyang 6ab8edafd5 test: add tests 2017-09-09 11:49:22 +08:00
ikatyang c88a45bbf7 refactor: categorize usages 2017-09-08 16:52:09 +08:00
ikatyang 08460e5ca5 test: add tests for `--stdin-filepath` 2017-09-08 15:08:19 +08:00
ikatyang a0e7613058 refactor: set deprecared option value warning on getter 2017-09-08 13:54:28 +08:00
ikatyang d9fd340902 test: add test for deprecated option value 2017-09-08 12:36:00 +08:00
ikatyang b13a104a7b test: add test for `--flow-parser` 2017-09-08 11:45:59 +08:00
ikatyang 0630d174ab test(integration): fix status 2017-09-08 10:59:48 +08:00
ikatyang de2f61cd3e Merge branch 'master' into refactor/cli 2017-09-08 10:58:28 +08:00
ikatyang 0981de9790 refactor: add missing dot 2017-09-08 08:54:59 +08:00
ikatyang be22996fd3 refactor: sort usages 2017-09-07 13:39:39 +08:00
ikatyang c337f9bb23 refactor: auto generate usage from options 2017-09-07 13:32:15 +08:00
Ika aaac83d126 refactor(runPrettier): reduce duplicate code (#2764) 2017-09-07 07:16:45 +02:00
Lucas Azzola 87503e5ef7 Refactor error handling (#2750) 2017-09-06 09:52:59 +10:00
Mitermayer Reis c59a82cbae Add CLI option '--config-precedence' (#2733)
Add CLI option '--config-precedence'
2017-09-05 08:56:23 +10:00
Simon Lydell 58350cbe59 Fix failing test on Travis CI
Chalk disables colors in CI environments, making the test pass locally
but not on Travis CI.

This also makes the snapshot more readable.
2017-09-03 17:02:12 +02:00
ikatyang ca7be87f9e test: remove unnecessary `eslint-disable` 2017-09-03 22:06:55 +08:00
ikatyang ac9fa43114 test(integration): add more tests 2017-09-03 21:31:33 +08:00
Ika c4e5463514 refactor(cli): separate files and make it pure as possible (#2730)
* refactor(bin): resolveConfig

* refactor(bin): getOptionsForFile

* refactor(bin): remove `mapSeries`

* refactor: move content from bin to src/cli

* refactor: wrap content with function

* refactor: move constants to another file

* refactor: move utils to another file

* refactor: move functions

* refactor: extract functions

* refactor: remove unnecessary variable

* refactor: move `src/cli` to `src` with prefix `cli`

* refactor: use template literal

* refactor: remove unnecessary variable

* refactor: extract `listDifferent`

* refactor: extract `format`

* refactor: change error message `Invalid configuration:`

* refactor: bind args with argv

* refactor: extract `getOptionsForFile`

* refactor: extract `formatStdin`

* refactor: extract `eachFilename`

* refactor: extract `formatFiles`

* refactor: remove unnecessary export

* refactor: use `globby.sync()`

* refactor: add `runPrettier.sync()` helper

* refactor: fix linting

* refactor: mock `process.argv`

* chore: use `--forceExit` to avoid process hanging

* refactor: mock `get-stream`

* refactor: remove unnecessary stuff

* refactor: fix linting

* refactor: wrap `run` with `try..catch.finally`

* refactor: restore `process.exitCode` after testing
2017-09-03 19:21:06 +10:00
Ika 088aa71b07 feat(resolve-config): add `.sync()` method (#2722)
* feat(resolve-config): add `sync` option

* docs(readme): add `sync` option

* fix(clearCache):  always clear both

* chore(deps): switch to 3.0 branch

* feat: replace `sync` option with `.sync()`

* docs(readme): update `.sync()`
2017-09-01 20:17:07 +10:00
Simon Lydell 7c9e589323 Fix CLI option parsing (#2684)
Fixes #2683
2017-08-29 08:16:04 +02:00
Lucas Azzola 1c806f81b2 Rename --resolve-config to --find-config-path (#2453) 2017-07-11 23:57:26 +10:00
Evilebot Tnawi 469059dc20 CLI: Support .ignore files (#2412)
Closes #2294
2017-07-11 23:33:46 +10:00
Lucas Azzola dcccfed366 Implement cosmiconfig for workspace configuration (#2434)
* 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
2017-07-10 22:26:36 +10:00
Joseph Frazier 16bc297572 Fix --debug-print-doc (#2400)
* Add test of --debug-print-doc

See https://github.com/prettier/prettier/issues/2396

* Pass test of --debug-print-doc

See https://github.com/prettier/prettier/issues/2396
2017-07-05 18:25:51 +02:00
Joseph Frazier e82a774568 Make --list-different to work with --stdin (#2393)
* Add test for `prettier --stdin --list-different`

See https://github.com/prettier/prettier/issues/2389

* Pass test for `prettier --stdin --list-different`

This fixes https://github.com/prettier/prettier/issues/2389

* Deduplicate --list-different handling

* Exit-early to reduce nesting in listDifferent()

* Simplify condition in listDifferent()
2017-07-04 14:41:46 -04:00
Evilebot Tnawi a0cadad222 feat(cli): support multiple patterns with ignore pattern (#2356) 2017-07-04 17:55:06 +02:00
Lucas Azzola 21f913cb6e CLI: ignore node_modules with ./**-style glob, fixes #2110 (#2185) 2017-06-18 14:35:42 +10:00
Joseph Frazier aedc6d4269 Add integration test of invalid syntax (#2160) 2017-06-16 09:10:39 +10:00
Joseph Frazier 3bbfa97152 Check exit codes in integration tests (#2148) 2017-06-15 09:56:48 +10:00
Lucas Azzola 364c38de0d Implement custom parser API (#1783)
* 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
2017-06-12 16:51:12 +10:00
Joseph Frazier 552ceb30e8 Allow --debug-check to work with stdin (#2013)
* 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
2017-06-06 16:10:24 -07:00
Philipp Spieß d679f235ed Also infer parser when using `--list-different` (#1997)
* Also infer parser when using `--list-different`

* Add integration tests for parser inference
2017-06-06 08:24:56 -07:00
Joseph Frazier 451f4aaad5 Add regression test for --debug-check (#1917)
See https://github.com/prettier/prettier/pull/1916
2017-06-02 15:30:39 -07:00
Joseph Frazier f6dabc0d27 Add regression test for CLI shebang preservation (#1901)
Following up on https://github.com/prettier/prettier/pull/1899
2017-06-02 13:42:44 -07:00
Michał Pierzchała 7a0df958dc Ignore node_modules when running prettier from CLI (#1683)
* Ignore node_modules by default

* Add integration test suite for --with-node-modules CLI arg

* Refactor tests to snapshots

* Disable eslint for purposely faulty files

* Fix node 4

* Fix prettier

* Gitignore only top-level node_modules
2017-05-23 14:19:04 -07:00