UNPKG

5.19 kBMarkdownView Raw
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
3Runs [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
8error: 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
18error: 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
282 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
36npm install --save-dev eslint-plugin-prettier
37npm 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
42Then, in your `.eslintrc.json`:
43
44```json
45{
46 "plugins": ["prettier"],
47 "rules": {
48 "prettier/prettier": "error"
49 }
50}
51```
52
53## Recommended Configuration
54
55This 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.
56
57If 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.
58
59To integrate this plugin with `eslint-config-prettier`, you can use the `"recommended"` configuration:
60
611. In addition to the above installation instructions, install `eslint-config-prettier`:
62
63 ```sh
64 npm install --save-dev eslint-config-prettier
65 ```
66
672. Then you need to add `plugin:prettier/recommended` as the last extension in your `.eslintrc.json`:
68
69 ```json
70 {
71 "extends": ["plugin:prettier/recommended"]
72 }
73 ```
74
75This does three things:
76
77- Enables `eslint-plugin-prettier`.
78- Sets the `prettier/prettier` rule to `"error"`.
79- Extends the `eslint-config-prettier` configuration.
80
81You can then set Prettier's own options inside a `.prettierrc` file.
82
833. In order to support special ESLint plugins (e.g. [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react)), add extra exclusions for the plugins you use like so:
84
85```json
86{
87 "extends": [
88 "plugin:prettier/recommended",
89 "prettier/flowtype",
90 "prettier/react",
91 "prettier/standard"
92 ]
93}
94```
95
96For the list of every available exclusion rule set, please see the [readme of eslint-config-prettier](https://github.com/prettier/eslint-config-prettier/blob/master/README.md).
97
98## Options
99
100> 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.
101
102- The first option:
103
104 - An object representing [options](https://prettier.io/docs/en/options.html) that will be passed into prettier. Example:
105
106 ```json
107 "prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
108 ```
109
110 NB: This option will merge and override any config set with `.prettierrc` files
111
112- The second option:
113
114 - An object with the following options
115
116 - `usePrettierrc`: Enables loading of the Prettier configuration file, (default: `true`). May be useful if you are using multiple tools that conflict with each other, or do not wish to mix your ESLint settings with your Prettier configuration.
117
118 ```json
119 "prettier/prettier": ["error", {}, {
120 "usePrettierrc": false
121 }]
122 ```
123
124 - `fileInfoOptions`: Options that are passed to [prettier.getFileInfo](https://prettier.io/docs/en/api.html#prettiergetfileinfofilepath-options) to decide whether a file needs to be formatted. Can be used for example to opt-out from ignoring files located in `node_modules` directories.
125
126 ```json
127 "prettier/prettier": ["error", {}, {
128 "fileInfoOptions": {
129 "withNodeModules": true
130 }
131 }]
132 ```
133
134- The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
135
136---
137
138## Contributing
139
140See [CONTRIBUTING.md](https://github.com/prettier/eslint-plugin-prettier/blob/master/CONTRIBUTING.md)