]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/user-guide/configuring/rules.md
build: add missing dh-nodejs to build-dependencies
[pve-eslint.git] / eslint / docs / src / user-guide / configuring / rules.md
CommitLineData
8f9d1d4d
DC
1---
2title: Rules
3layout: doc
4eleventyNavigation:
5 key: configuring rules
6 parent: configuring
7 title: Configuring Rules
8 order: 3
5422a9cc 9
8f9d1d4d 10---
5422a9cc
TL
11
12## Configuring Rules
13
14ESLint comes with a large number of built-in rules and you can add more rules through plugins. You can modify which rules your project uses either using configuration comments or configuration files. To change a rule setting, you must set the rule ID equal to one of these values:
15
16* `"off"` or `0` - turn the rule off
17* `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code)
18* `"error"` or `2` - turn the rule on as an error (exit code is 1 when triggered)
19
20### Using configuration comments
21
22To configure rules inside of a file using configuration comments, use a comment in the following format:
23
24```js
25/* eslint eqeqeq: "off", curly: "error" */
26```
27
8f9d1d4d 28In this example, [`eqeqeq`](../../rules/eqeqeq) is turned off and [`curly`](../../rules/curly) is turned on as an error. You can also use the numeric equivalent for the rule severity:
5422a9cc
TL
29
30```js
31/* eslint eqeqeq: 0, curly: 2 */
32```
33
34This example is the same as the last example, only it uses the numeric codes instead of the string values. The `eqeqeq` rule is off and the `curly` rule is set to be an error.
35
36If a rule has additional options, you can specify them using array literal syntax, such as:
37
38```js
39/* eslint quotes: ["error", "double"], curly: 2 */
40```
41
8f9d1d4d 42This comment specifies the "double" option for the [`quotes`](../../rules/quotes) rule. The first item in the array is always the rule severity (number or string).
5422a9cc
TL
43
44Configuration comments can include descriptions to explain why the comment is necessary. The description must occur after the configuration and is separated from the configuration by two or more consecutive `-` characters. For example:
45
46```js
47/* eslint eqeqeq: "off", curly: "error" -- Here's a description about why this configuration is necessary. */
48```
49
50```js
51/* eslint eqeqeq: "off", curly: "error"
52 --------
53 Here's a description about why this configuration is necessary. */
54```
55
56```js
57/* eslint eqeqeq: "off", curly: "error"
58 * --------
59 * This will not work due to the line above starting with a '*' character.
60 */
61```
62
63### Using configuration files
64
65To configure rules inside of a configuration file, use the `rules` key along with an error level and any options you want to use. For example:
66
67```json
68{
69 "rules": {
70 "eqeqeq": "off",
71 "curly": "error",
72 "quotes": ["error", "double"]
73 }
74}
75```
76
77And in YAML:
78
79```yaml
80---
81rules:
82 eqeqeq: off
83 curly: error
84 quotes:
85 - error
86 - double
87```
88
89To configure a rule which is defined within a plugin you have to prefix the rule ID with the plugin name and a `/`. For example:
90
91```json
92{
93 "plugins": [
94 "plugin1"
95 ],
96 "rules": {
97 "eqeqeq": "off",
98 "curly": "error",
99 "quotes": ["error", "double"],
100 "plugin1/rule1": "error"
101 }
102}
103```
104
105And in YAML:
106
107```yaml
108---
109plugins:
110 - plugin1
111rules:
112 eqeqeq: 0
113 curly: error
114 quotes:
115 - error
116 - "double"
117 plugin1/rule1: error
118```
119
120In these configuration files, the rule `plugin1/rule1` comes from the plugin named `plugin1`. You can also use this format with configuration comments, such as:
121
122```js
123/* eslint "plugin1/rule1": "error" */
124```
125
126**Note:** When specifying rules from plugins, make sure to omit `eslint-plugin-`. ESLint uses only the unprefixed name internally to locate rules.
127
128## Disabling Rules
129
130### Using configuration comments
131
132To temporarily disable rule warnings in your file, use block comments in the following format:
133
134```js
135/* eslint-disable */
136
137alert('foo');
138
139/* eslint-enable */
140```
141
142You can also disable or enable warnings for specific rules:
143
144```js
145/* eslint-disable no-alert, no-console */
146
147alert('foo');
148console.log('bar');
149
150/* eslint-enable no-alert, no-console */
151```
152
8f9d1d4d
DC
153**Note:** `/* eslint-enable */` without any specific rules listed will cause all disabled rules to be re-enabled.
154
5422a9cc
TL
155To disable rule warnings in an entire file, put a `/* eslint-disable */` block comment at the top of the file:
156
157```js
158/* eslint-disable */
159
160alert('foo');
161```
162
163You can also disable or enable specific rules for an entire file:
164
165```js
166/* eslint-disable no-alert */
167
168alert('foo');
169```
170
8f9d1d4d
DC
171To ensure that a rule is never applied (regardless of any future enable/disable lines):
172
173```js
174/* eslint no-alert: "off" */
175
176alert('foo');
177```
178
5422a9cc
TL
179To disable all rules on a specific line, use a line or block comment in one of the following formats:
180
181```js
182alert('foo'); // eslint-disable-line
183
184// eslint-disable-next-line
185alert('foo');
186
187/* eslint-disable-next-line */
188alert('foo');
189
190alert('foo'); /* eslint-disable-line */
191```
192
193To disable a specific rule on a specific line:
194
195```js
196alert('foo'); // eslint-disable-line no-alert
197
198// eslint-disable-next-line no-alert
199alert('foo');
200
201alert('foo'); /* eslint-disable-line no-alert */
202
203/* eslint-disable-next-line no-alert */
204alert('foo');
205```
206
207To disable multiple rules on a specific line:
208
209```js
210alert('foo'); // eslint-disable-line no-alert, quotes, semi
211
212// eslint-disable-next-line no-alert, quotes, semi
213alert('foo');
214
215alert('foo'); /* eslint-disable-line no-alert, quotes, semi */
216
217/* eslint-disable-next-line no-alert, quotes, semi */
218alert('foo');
8f9d1d4d
DC
219
220/* eslint-disable-next-line
221 no-alert,
222 quotes,
223 semi
224*/
225alert('foo');
5422a9cc
TL
226```
227
228All of the above methods also work for plugin rules. For example, to disable `eslint-plugin-example`'s `rule-name` rule, combine the plugin's name (`example`) and the rule's name (`rule-name`) into `example/rule-name`:
229
230```js
231foo(); // eslint-disable-line example/rule-name
232foo(); /* eslint-disable-line example/rule-name */
233```
234
235Configuration comments can include descriptions to explain why the comment is necessary. The description must come after the configuration and needs to be separated from the configuration by two or more consecutive `-` characters. For example:
236
237```js
238// eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary.
239console.log('hello');
8f9d1d4d
DC
240
241/* eslint-disable-next-line no-console --
242 * Here's a very long description about why this configuration is necessary
243 * along with some additional information
244**/
245console.log('hello');
5422a9cc
TL
246```
247
248**Note:** Comments that disable warnings for a portion of a file tell ESLint not to report rule violations for the disabled code. ESLint still parses the entire file, however, so disabled code still needs to be syntactically valid JavaScript.
249
250### Using configuration files
251
252To disable rules inside of a configuration file for a group of files, use the `overrides` key along with a `files` key. For example:
253
254```json
255{
256 "rules": {...},
257 "overrides": [
258 {
259 "files": ["*-test.js","*.spec.js"],
260 "rules": {
261 "no-unused-expressions": "off"
262 }
263 }
264 ]
265}
266```
267
268### Disabling Inline Comments
269
270To disable all inline config comments, use the `noInlineConfig` setting. For example:
271
272```json
273{
274 "rules": {...},
275 "noInlineConfig": true
276}
277```
278
8f9d1d4d 279This setting is similar to [--no-inline-config](../command-line-interface#--no-inline-config) CLI option.
5422a9cc
TL
280
281#### Report unused `eslint-disable` comments
282
283To report unused `eslint-disable` comments, use the `reportUnusedDisableDirectives` setting. For example:
284
285```json
286{
287 "rules": {...},
288 "reportUnusedDisableDirectives": true
289}
290```
291
8f9d1d4d 292This setting is similar to [--report-unused-disable-directives](../command-line-interface#--report-unused-disable-directives) CLI option, but doesn't fail linting (reports as `"warn"` severity).