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 prettier eslint-plugin-prettier
|
37 | ```
|
38 |
|
39 | **_`eslint-plugin-prettier` does not install Prettier or ESLint for you._** _You must install these yourself._
|
40 |
|
41 | Then, in your `.eslintrc`:
|
42 |
|
43 | ```json
|
44 | {
|
45 | "plugins": [
|
46 | "prettier"
|
47 | ],
|
48 | "rules": {
|
49 | "prettier/prettier": "error"
|
50 | }
|
51 | }
|
52 | ```
|
53 |
|
54 | ## Options
|
55 |
|
56 | * The first option:
|
57 |
|
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 | ```
|
63 |
|
64 | - Or the string `"fb"` may be used to set "Facebook style" defaults:
|
65 |
|
66 | ```json
|
67 | "prettier/prettier": ["error", "fb"]
|
68 | ```
|
69 |
|
70 | Equivalent to:
|
71 |
|
72 | ```json
|
73 | "prettier/prettier": ["error", {
|
74 | "singleQuote": true,
|
75 | "trailingComma": "all",
|
76 | "bracketSpacing": false,
|
77 | "jsxBracketSameLine": true,
|
78 | "parser": "flow"
|
79 | }]
|
80 | ```
|
81 |
|
82 | * The second option:
|
83 |
|
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:
|
85 |
|
86 | ```json
|
87 | "prettier/prettier": ["error", null, "@prettier"]
|
88 | ```
|
89 |
|
90 | Only files with `@prettier` in the heading docblock will be checked:
|
91 |
|
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.
|
115 |
|
116 | ## Contributing
|
117 |
|
118 | See [CONTRIBUTING.md](https://github.com/prettier/eslint-plugin-prettier/blob/master/CONTRIBUTING.md)
|