|
1 | 1 | # eslint-plugin-prettier [](https://travis-ci.org/prettier/eslint-plugin-prettier)
|
2 | 2 |
|
3 |
| -Runs [prettier](https://github.com/jlongster/prettier) as an eslint rule |
| 3 | +Runs [Prettier](https://github.com/prettier/prettier) as an [ESLint](http://eslint.org) rule and reports differences as individual ESLint issues. |
4 | 4 |
|
5 |
| -## Installation |
| 5 | +## Sample |
6 | 6 |
|
7 |
| -You'll first need to install [ESLint](http://eslint.org): |
| 7 | +```js |
| 8 | +error: Insert `,` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:22:25: |
| 9 | + 20 | import { |
| 10 | + 21 | observeActiveEditorsDebounced, |
| 11 | +> 22 | editorChangesDebounced |
| 12 | + | ^ |
| 13 | + 23 | } from './debounced';; |
| 14 | + 24 | |
| 15 | + 25 | import {observableFromSubscribeFunction} from '../commons-node/event'; |
8 | 16 |
|
9 |
| -``` |
10 |
| -$ npm install eslint --save-dev |
11 |
| -``` |
12 | 17 |
|
13 |
| -Next, install `prettier`: |
| 18 | +error: Delete `;` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:23:21: |
| 19 | + 21 | observeActiveEditorsDebounced, |
| 20 | + 22 | editorChangesDebounced |
| 21 | +> 23 | } from './debounced';; |
| 22 | + | ^ |
| 23 | + 24 | |
| 24 | + 25 | import {observableFromSubscribeFunction} from '../commons-node/event'; |
| 25 | + 26 | import {cacheWhileSubscribed} from '../commons-node/observable'; |
14 | 26 |
|
15 |
| -``` |
16 |
| -$ npm install prettier --save-dev |
| 27 | + |
| 28 | +2 errors found. |
17 | 29 | ```
|
18 | 30 |
|
19 |
| -Finally, install `eslint-plugin-prettier`: |
| 31 | +> `./node_modules/.bin/eslint --format codeframe pkg/commons-atom/ActiveEditorRegistry.js` (code from [nuclide](https://github.com/facebook/nuclide)). |
20 | 32 |
|
21 |
| -``` |
22 |
| -$ npm install eslint-plugin-prettier --save-dev |
23 |
| -``` |
| 33 | +## Installation |
24 | 34 |
|
25 |
| -**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-prettier` globally. |
| 35 | +```sh |
| 36 | +npm install --save-dev prettier eslint-plugin-prettier |
| 37 | +``` |
26 | 38 |
|
27 |
| -## Usage |
| 39 | +**_`eslint-plugin-prettier` does not install Prettier or ESLint for you._** _You must install these yourself._ |
28 | 40 |
|
29 |
| -Add `prettier` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix: |
| 41 | +Then, in your `.eslintrc`: |
30 | 42 |
|
31 | 43 | ```json
|
32 | 44 | {
|
33 |
| - "plugins": [ |
34 |
| - "prettier" |
35 |
| - ] |
| 45 | + "plugins": [ |
| 46 | + "prettier" |
| 47 | + ], |
| 48 | + "rules": { |
| 49 | + "prettier/prettier": "error" |
| 50 | + } |
36 | 51 | }
|
37 | 52 | ```
|
38 | 53 |
|
| 54 | +## Options |
39 | 55 |
|
40 |
| -Then configure the `prettier` rule under the `rules` section: |
| 56 | +* The first option: |
41 | 57 |
|
42 |
| -```json |
43 |
| -{ |
44 |
| - "rules": { |
45 |
| - "prettier/prettier": "error" |
46 |
| - } |
47 |
| -} |
48 |
| -``` |
| 58 | + - Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#api). Example: |
| 59 | + |
| 60 | + ```json |
| 61 | + "prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}] |
| 62 | + ``` |
49 | 63 |
|
50 |
| -You can also pass [`prettier` configuration](https://github.com/prettier/prettier#api) as an option: |
| 64 | + - Or the string `"fb"` may be used to set "Facebook style" defaults: |
51 | 65 |
|
52 |
| -```json |
53 |
| -{ |
54 |
| - "rules": { |
55 |
| - "prettier/prettier": ["error", {"trailingComma": "es5", "singleQuote": true}] |
56 |
| - } |
57 |
| -} |
58 |
| -``` |
| 66 | + ```json |
| 67 | + "prettier/prettier": ["error", "fb"] |
| 68 | + ``` |
59 | 69 |
|
60 |
| -The rule will report an error if your code does not match `prettier` style. The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style. |
| 70 | + Equivalent to: |
61 | 71 |
|
62 |
| ---- |
| 72 | + ```json |
| 73 | + "prettier/prettier": ["error", { |
| 74 | + "singleQuote": true, |
| 75 | + "trailingComma": "all", |
| 76 | + "bracketSpacing": false, |
| 77 | + "jsxBracketSameLine": true, |
| 78 | + "parser": "flow" |
| 79 | + }] |
| 80 | + ``` |
63 | 81 |
|
64 |
| -This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect patterns in the AST. (If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors.) You can use [eslint-config-prettier](https://github.com/lydell/eslint-config-prettier) to disable all formatting-related ESLint rules. If your desired formatting does not match the `prettier` output, you should use a different tool such as [prettier-eslint](https://github.com/kentcdodds/prettier-eslint) instead. |
| 82 | +* The second option: |
65 | 83 |
|
66 |
| -## Migrating to 2.0.0 |
| 84 | + - A string with a pragma that triggers this rule. By default, this rule applies to all files. However, if you set a pragma (this option), only files with that pragma in the heading docblock will be checked. All pragmas must start with `@`. Example: |
67 | 85 |
|
68 |
| -Starting in 2.0.0, `prettier` is a peerDependency of this module, rather than a dependency. This means that you can use any version of `prettier` with this plugin, but it also means that you need to install it separately and include it as a devDependency in your project. |
| 86 | + ```json |
| 87 | + "prettier/prettier": ["error", null, "@prettier"] |
| 88 | + ``` |
69 | 89 |
|
70 |
| -To install `prettier`, use: |
| 90 | + Only files with `@prettier` in the heading docblock will be checked: |
71 | 91 |
|
72 |
| -```bash |
73 |
| -npm install prettier --save-dev |
74 |
| -``` |
| 92 | + ```js |
| 93 | + /** @prettier */ |
| 94 | + |
| 95 | + console.log(1 + 2 + 3); |
| 96 | + ``` |
| 97 | + |
| 98 | + Or: |
| 99 | + |
| 100 | + ```js |
| 101 | + /** |
| 102 | + * @prettier |
| 103 | + */ |
| 104 | + |
| 105 | + console.log(4 + 5 + 6); |
| 106 | + ``` |
| 107 | + |
| 108 | + _This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._ |
| 109 | + |
| 110 | +* The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect patterns in the AST. (If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors.) You can use [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules. If your desired formatting does not match the `prettier` output, you should use a different tool such as [prettier-eslint](https://github.com/prettier/prettier-eslint) instead. |
75 | 115 |
|
76 | 116 | ## Contributing
|
77 | 117 |
|
78 |
| -See [CONTRIBUTING.md](https://github.com/not-an-aardvark/eslint-plugin-prettier/blob/master/CONTRIBUTING.md) |
| 118 | +See [CONTRIBUTING.md](https://github.com/prettier/eslint-plugin-prettier/blob/master/CONTRIBUTING.md) |
0 commit comments