UNPKG

13.9 kBMarkdownView Raw
1# eslint-plugin-prettier [![Build Status](https://github.com/prettier/eslint-plugin-prettier/workflows/CI/badge.svg?branch=master)](https://github.com/prettier/eslint-plugin-prettier/actions?query=workflow%3ACI+branch%3Amaster)
2
3Runs [Prettier](https://github.com/prettier/prettier) as an [ESLint](https://eslint.org) rule and reports differences as individual ESLint issues.
4
5If your desired formatting does not match Prettier’s output, you should use a different tool such as [prettier-eslint](https://github.com/prettier/prettier-eslint) instead.
6
7Please read [Integrating with linters](https://prettier.io/docs/en/integrating-with-linters.html) before installing.
8
9## TOC <!-- omit in toc -->
10
11- [Sample](#sample)
12- [Installation](#installation)
13- [Configuration (legacy: `.eslintrc*`)](#configuration-legacy-eslintrc)
14- [Configuration (new: `eslint.config.js`)](#configuration-new-eslintconfigjs)
15- [`Svelte` support](#svelte-support)
16- [`arrow-body-style` and `prefer-arrow-callback` issue](#arrow-body-style-and-prefer-arrow-callback-issue)
17- [Options](#options)
18- [Sponsors](#sponsors)
19- [Backers](#backers)
20- [Contributing](#contributing)
21- [Changelog](#changelog)
22- [License](#license)
23
24## Sample
25
26```js
27error: Insert `,` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:22:25:
28 20 | import {
29 21 | observeActiveEditorsDebounced,
30> 22 | editorChangesDebounced
31 | ^
32 23 | } from './debounced';;
33 24 |
34 25 | import {observableFromSubscribeFunction} from '../commons-node/event';
35
36
37error: Delete `;` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:23:21:
38 21 | observeActiveEditorsDebounced,
39 22 | editorChangesDebounced
40> 23 | } from './debounced';;
41 | ^
42 24 |
43 25 | import {observableFromSubscribeFunction} from '../commons-node/event';
44 26 | import {cacheWhileSubscribed} from '../commons-node/observable';
45
46
472 errors found.
48```
49
50> `./node_modules/.bin/eslint --format codeframe pkg/commons-atom/ActiveEditorRegistry.js` (code from [nuclide](https://github.com/facebook/nuclide)).
51
52## Installation
53
54```sh
55npm install --save-dev eslint-plugin-prettier eslint-config-prettier
56npm install --save-dev --save-exact prettier
57```
58
59**_`eslint-plugin-prettier` does not install Prettier or ESLint for you._** _You must install these yourself._
60
61This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect potential bugs. If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors. Our recommended configuration automatically enables [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules.
62
63## Configuration (legacy: `.eslintrc*`)
64
65For [legacy configuration](https://eslint.org/docs/latest/use/configure/configuration-files), this plugin ships with a `plugin:prettier/recommended` config that sets up both `eslint-plugin-prettier` and [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) in one go.
66
67Add `plugin:prettier/recommended` as the _last_ item in the extends array in your `.eslintrc*` config file, so that `eslint-config-prettier` has the opportunity to override other configs:
68
69```json
70{
71 "extends": ["plugin:prettier/recommended"]
72}
73```
74
75This will:
76
77- Enable the `prettier/prettier` rule.
78- Disable the `arrow-body-style` and `prefer-arrow-callback` rules which are problematic with this plugin - see the below for why.
79- Enable the `eslint-config-prettier` config which will turn off ESLint rules that conflict with Prettier.
80
81## Configuration (new: `eslint.config.js`)
82
83For [flat configuration](https://eslint.org/docs/latest/use/configure/configuration-files-new), this plugin ships with an `eslint-plugin-prettier/recommended` config that sets up both `eslint-plugin-prettier` and [`eslint-config-prettier`](https://github.com/prettier/eslint-config-prettier) in one go.
84
85Import `eslint-plugin-prettier/recommended` and add it as the _last_ item in the configuration array in your `eslint.config.js` file so that `eslint-config-prettier` has the opportunity to override other configs:
86
87```js
88const eslintPluginPrettierRecommended = require('eslint-plugin-prettier/recommended');
89
90module.exports = [
91 // Any other config imports go at the top
92 eslintPluginPrettierRecommended,
93];
94```
95
96This will:
97
98- Enable the `prettier/prettier` rule.
99- Disable the `arrow-body-style` and `prefer-arrow-callback` rules which are problematic with this plugin - see the below for why.
100- Enable the `eslint-config-prettier` config which will turn off ESLint rules that conflict with Prettier.
101
102## `Svelte` support
103
104We recommend to use [`eslint-plugin-svelte`](https://github.com/ota-meshi/eslint-plugin-svelte) instead of [`eslint-plugin-svelte3`](https://github.com/sveltejs/eslint-plugin-svelte3) because `eslint-plugin-svelte` has a correct [`eslint-svelte-parser`](https://github.com/ota-meshi/svelte-eslint-parser) instead of hacking.
105
106When use with `eslint-plugin-svelte3`, `eslint-plugin-prettier` will just ignore the text passed by `eslint-plugin-svelte3`, because the text has been modified.
107
108If you still decide to use `eslint-plugin-svelte3`, you'll need to run `prettier --write *.svelte` manually.
109
110## `arrow-body-style` and `prefer-arrow-callback` issue
111
112If you use [arrow-body-style](https://eslint.org/docs/rules/arrow-body-style) or [prefer-arrow-callback](https://eslint.org/docs/rules/prefer-arrow-callback) together with the `prettier/prettier` rule from this plugin, you can in some cases end up with invalid code due to a bug in ESLint’s autofix – see [issue #65](https://github.com/prettier/eslint-plugin-prettier/issues/65).
113
114For this reason, it’s recommended to turn off these rules. The `plugin:prettier/recommended` config does that for you.
115
116You _can_ still use these rules together with this plugin if you want, because the bug does not occur _all the time._ But if you do, you need to keep in mind that you might end up with invalid code, where you manually have to insert a missing closing parenthesis to get going again.
117
118If you’re fixing large of amounts of previously unformatted code, consider temporarily disabling the `prettier/prettier` rule and running `eslint --fix` and `prettier --write` separately.
119
120## Options
121
122> 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.
123
124- The first option:
125
126 - An object representing [options](https://prettier.io/docs/en/options.html) that will be passed into prettier. Example:
127
128 ```json
129 {
130 "prettier/prettier": [
131 "error",
132 {
133 "singleQuote": true,
134 "parser": "flow"
135 }
136 ]
137 }
138 ```
139
140 NB: This option will merge and override any config set with `.prettierrc` files
141
142- The second option:
143
144 - An object with the following options
145
146 - `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. And also, it is possible to run prettier without loading the prettierrc config file [via the CLI's --no-config option](https://prettier.io/docs/en/cli.html#--no-config) or through the API by [calling prettier.format() without passing through the options generated by calling resolveConfig](https://prettier.io/docs/en/api.html#prettierresolveconfigfilepath--options).
147
148 ```json
149 {
150 "prettier/prettier": [
151 "error",
152 {},
153 {
154 "usePrettierrc": false
155 }
156 ]
157 }
158 ```
159
160 - `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.
161
162 ```json
163 {
164 "prettier/prettier": [
165 "error",
166 {},
167 {
168 "fileInfoOptions": {
169 "withNodeModules": true
170 }
171 }
172 ]
173 }
174 ```
175
176- The rule is auto fixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.
177
178---
179
180## Sponsors
181
182| @prettier/plugin-eslint | eslint-config-prettier | eslint-plugin-prettier | prettier-eslint | prettier-eslint-cli |
183| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
184| [![@prettier/plugin-eslint Open Collective sponsors](https://opencollective.com/prettier-plugin-eslint/tiers/sponsors.svg)](https://opencollective.com/prettier-plugin-eslint) | [![eslint-config-prettier Open Collective backers](https://opencollective.com/eslint-config-prettier/tiers/sponsors.svg)](https://opencollective.com/eslint-config-prettier) | [![eslint-plugin-prettier Open Collective backers](https://opencollective.com/eslint-plugin-prettier/tiers/sponsors.svg)](https://opencollective.com/rxts) | [![prettier-eslint Open Collective sponsors](https://opencollective.com/prettier-eslint/tiers/sponsors.svg)](https://opencollective.com/prettier-eslint) | [![prettier-eslint-cli Open Collective backers](https://opencollective.com/prettier-eslint-cli/tiers/sponsors.svg)](https://opencollective.com/prettier-eslint-cli) |
185
186## Backers
187
188| @prettier/plugin-eslint | eslint-config-prettier | eslint-plugin-prettier | prettier-eslint | prettier-eslint-cli |
189| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
190| [![@prettier/plugin-eslint Open Collective backers](https://opencollective.com/prettier-plugin-eslint/tiers/backers.svg)](https://opencollective.com/prettier-plugin-eslint) | [![eslint-config-prettier Open Collective backers](https://opencollective.com/eslint-config-prettier/tiers/backers.svg)](https://opencollective.com/eslint-config-prettier) | [![eslint-plugin-prettier Open Collective backers](https://opencollective.com/eslint-plugin-prettier/tiers/backers.svg)](https://opencollective.com/rxts) | [![prettier-eslint Open Collective backers](https://opencollective.com/prettier-eslint/tiers/backers.svg)](https://opencollective.com/prettier-eslint) | [![prettier-eslint-cli Open Collective backers](https://opencollective.com/prettier-eslint-cli/tiers/backers.svg)](https://opencollective.com/prettier-eslint-cli) |
191
192## Contributing
193
194See [CONTRIBUTING.md](https://github.com/prettier/eslint-plugin-prettier/blob/master/CONTRIBUTING.md)
195
196## Changelog
197
198Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.md).
199
200## License
201
202[MIT](http://opensource.org/licenses/MIT)