1 | # eslint-plugin-prettier [![Build Status](https://travis-ci.org/prettier/eslint-plugin-prettier.svg?branch=master)](https://travis-ci.org/prettier/eslint-plugin-prettier)
|
2 |
|
3 | Runs [Prettier](https://github.com/prettier/prettier) as an [ESLint](http://eslint.org) rule and reports differences as individual ESLint issues.
|
4 |
|
5 | ## Sample
|
6 |
|
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';
|
16 |
|
17 |
|
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';
|
26 |
|
27 |
|
28 | 2 errors found.
|
29 | ```
|
30 |
|
31 | > `./node_modules/.bin/eslint --format codeframe pkg/commons-atom/ActiveEditorRegistry.js` (code from [nuclide](https://github.com/facebook/nuclide)).
|
32 |
|
33 | ## Installation
|
34 |
|
35 | ```sh
|
36 | npm install --save-dev eslint-plugin-prettier
|
37 | npm install --save-dev --save-exact prettier
|
38 | ```
|
39 |
|
40 | **_`eslint-plugin-prettier` does not install Prettier or ESLint for you._** _You must install these yourself._
|
41 |
|
42 | Then, in your `.eslintrc.json`:
|
43 |
|
44 | ```json
|
45 | {
|
46 | "plugins": [
|
47 | "prettier"
|
48 | ],
|
49 | "rules": {
|
50 | "prettier/prettier": "error"
|
51 | }
|
52 | }
|
53 | ```
|
54 |
|
55 | ## Recommended Configuration
|
56 |
|
57 | 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.
|
58 |
|
59 | 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.
|
60 |
|
61 | To integrate this plugin with `eslint-config-prettier`, you can use the `"recommended"` configuration:
|
62 |
|
63 | 1. In addition to the above installation instructions, install `eslint-config-prettier`:
|
64 |
|
65 | ```sh
|
66 | npm install --save-dev eslint-config-prettier
|
67 | ```
|
68 |
|
69 | 2. Then all you need in your `.eslintrc.json` is:
|
70 |
|
71 | ```json
|
72 | {
|
73 | "extends": [
|
74 | "plugin:prettier/recommended"
|
75 | ]
|
76 | }
|
77 | ```
|
78 |
|
79 | This does three things:
|
80 |
|
81 | 1. Enables `eslint-plugin-prettier`.
|
82 | 2. Sets the `prettier/prettier` rule to `"error"`.
|
83 | 3. Extends the `eslint-config-prettier` configuration.
|
84 |
|
85 | You can then set Prettier's own options inside a `.prettierrc` file.
|
86 |
|
87 | ## Options
|
88 |
|
89 | > Note: While it is possible to pass options to Prettier via your ESLint configuration file, it is not recommended because editor extensions such as `prettier-atom` and `prettier-vscode` **will** read [`.prettierrc`](https://prettier.io/docs/en/configuration.html), but **won't** read settings from ESLint, which can lead to an inconsistent experience.
|
90 |
|
91 | * The first option:
|
92 | - Objects are passed directly to Prettier as [options](https://prettier.io/docs/en/options.html). Example:
|
93 |
|
94 | ```json
|
95 | "prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
|
96 | ```
|
97 |
|
98 | - Or the string `"fb"` may be used to set "Facebook style" defaults:
|
99 |
|
100 | ```json
|
101 | "prettier/prettier": ["error", "fb"]
|
102 | ```
|
103 |
|
104 | Equivalent to:
|
105 |
|
106 | ```json
|
107 | "prettier/prettier": ["error", {
|
108 | "singleQuote": true,
|
109 | "trailingComma": "all",
|
110 | "bracketSpacing": false,
|
111 | "jsxBracketSameLine": true,
|
112 | "parser": "flow"
|
113 | }]
|
114 | ```
|
115 | NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, [config files are ignored](https://github.com/prettier/eslint-plugin-prettier/issues/46))
|
116 |
|
117 | * The second option:
|
118 |
|
119 | - 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:
|
120 |
|
121 | ```json
|
122 | "prettier/prettier": ["error", null, "@prettier"]
|
123 | ```
|
124 |
|
125 | Only files with `@prettier` in the heading docblock will be checked:
|
126 |
|
127 | ```js
|
128 | /** @prettier */
|
129 |
|
130 | console.log(1 + 2 + 3);
|
131 | ```
|
132 |
|
133 | Or:
|
134 |
|
135 | ```js
|
136 | /**
|
137 | * @prettier
|
138 | */
|
139 |
|
140 | console.log(4 + 5 + 6);
|
141 | ```
|
142 |
|
143 | _This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._
|
144 |
|
145 | * The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
|
146 |
|
147 | ---
|
148 |
|
149 | ## Contributing
|
150 |
|
151 | See [CONTRIBUTING.md](https://github.com/prettier/eslint-plugin-prettier/blob/master/CONTRIBUTING.md)
|
152 |
|
\ | No newline at end of file |