]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/user-guide/configuring/configuration-files.md
import eslint 7.28.0
[pve-eslint.git] / eslint / docs / user-guide / configuring / configuration-files.md
1 # Configuration Files
2
3 * [Configuration File Formats](#configuration-file-formats)
4 * [Using Configuration Files](#using-configuration-files)
5 * [Adding Shared Settings](#adding-shared-settings)
6 * [Cascading and Hierarchy](#cascading-and-hierarchy)
7 * [Extending Configuration Files](#extending-configuration-files)
8 * [Configuration Based on Glob Patterns](#configuration-based-on-glob-patterns)
9 * [Personal Configuration Files](#personal-configuration-files)
10
11 ## Configuration File Formats
12
13 ESLint supports configuration files in several formats:
14
15 * **JavaScript** - use `.eslintrc.js` and export an object containing your configuration.
16 * **JavaScript (ESM)** - use `.eslintrc.cjs` when running ESLint in JavaScript packages that specify `"type":"module"` in their `package.json`. Note that ESLint does not support ESM configuration at this time.
17 * **YAML** - use `.eslintrc.yaml` or `.eslintrc.yml` to define the configuration structure.
18 * **JSON** - use `.eslintrc.json` to define the configuration structure. ESLint's JSON files also allow JavaScript-style comments.
19 * **package.json** - create an `eslintConfig` property in your `package.json` file and define your configuration there.
20
21 If there are multiple configuration files in the same directory, ESLint will only use one. The priority order is as follows:
22
23 1. `.eslintrc.js`
24 1. `.eslintrc.cjs`
25 1. `.eslintrc.yaml`
26 1. `.eslintrc.yml`
27 1. `.eslintrc.json`
28 1. `package.json`
29
30 ## Using Configuration Files
31
32 There are two ways to use configuration files.
33
34 The first way to use configuration files is via `.eslintrc.*` and `package.json` files. ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem (unless `root: true` is specified). Configuration files can be useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.
35
36 The second way to use configuration files is to save the file wherever you would like and pass its location to the CLI using the `--config` option, such as:
37
38 eslint -c myconfig.json myfiletotest.js
39
40 If you are using one configuration file and want ESLint to ignore any `.eslintrc.*` files, make sure to use [`--no-eslintrc`](https://eslint.org/docs/user-guide/command-line-interface#-no-eslintrc) along with the [`-c`](https://eslint.org/docs/user-guide/command-line-interface#-c-config) flag.
41
42 ### Comments in configuration files
43
44 Both the JSON and YAML configuration file formats support comments (package.json files should not include them). You can use JavaScript-style comments for JSON files and YAML-style comments for YAML files. ESLint safely ignores comments in configuration files. This allows your configuration files to be more human-friendly.
45
46 For JavaScript-style comments:
47
48 ```js
49 {
50 "env": {
51 "browser": true
52 },
53 "rules": {
54 // Override our default settings just for this directory
55 "eqeqeq": "warn",
56 "strict": "off"
57 }
58 }
59 ```
60
61 For YAML-style comments:
62
63 ```yaml
64 env:
65 browser: true
66 rules:
67 # Override default settings
68 eqeqeq: warn
69 strict: off
70 ```
71
72 ## Adding Shared Settings
73
74 ESLint supports adding shared settings into configuration files. Plugins use `settings` to specify information that should be shared across all of its rules. You can add `settings` object to ESLint configuration file and it will be supplied to every rule being executed. This may be useful if you are adding custom rules and want them to have access to the same information and be easily configurable.
75
76 In JSON:
77
78 ```json
79 {
80 "settings": {
81 "sharedData": "Hello"
82 }
83 }
84 ```
85
86 And in YAML:
87
88 ```yaml
89 ---
90 settings:
91 sharedData: "Hello"
92 ```
93
94 ## Cascading and Hierarchy
95
96 When using `.eslintrc.*` and `package.json` files for configuration, you can take advantage of configuration cascading. Suppose you have the following structure:
97
98 ```text
99 your-project
100 ├── .eslintrc.json
101 ├── lib
102 │ └── source.js
103 └─┬ tests
104 ├── .eslintrc.json
105 └── test.js
106 ```
107
108 The configuration cascade works based on the location of the file being linted. If there is a `.eslintrc` file in the same directory as the file being linted, then that configuration takes precedence. ESLint then searches up the directory structure, merging any `.eslintrc` files it finds along the way until reaching either a `.eslintrc` file with `root: true` or the root directory.
109
110 In the same way, if there is a `package.json` file in the root directory with an `eslintConfig` field, the configuration it describes will apply to all subdirectories beneath it, but the configuration described by the `.eslintrc` file in the `tests/` directory will override it where there are conflicting specifications.
111
112 ```text
113 your-project
114 ├── package.json
115 ├── lib
116 │ └── source.js
117 └─┬ tests
118 ├── .eslintrc.json
119 └── test.js
120 ```
121
122 If there is an `.eslintrc` and a `package.json` file found in the same directory, `.eslintrc` will take priority and `package.json` file will not be used.
123
124 By default, ESLint will look for configuration files in all parent folders up to the root directory. This can be useful if you want all of your projects to follow a certain convention, but can sometimes lead to unexpected results. To limit ESLint to a specific project, place `"root": true` inside the `.eslintrc.*` file or `eslintConfig` field of the `package.json` file or in the `.eslintrc.*` file at your project's root level. ESLint will stop looking in parent folders once it finds a configuration with `"root": true`.
125
126 ```js
127 {
128 "root": true
129 }
130 ```
131
132 And in YAML:
133
134 ```yaml
135 ---
136 root: true
137 ```
138
139 For example, consider `projectA` which has `"root": true` set in the `.eslintrc` file in the `lib/` directory. In this case, while linting `main.js`, the configurations within `lib/` will be used, but the `.eslintrc` file in `projectA/` will not.
140
141 ```text
142 home
143 └── user
144 └── projectA
145 ├── .eslintrc.json <- Not used
146 └── lib
147 ├── .eslintrc.json <- { "root": true }
148 └── main.js
149 ```
150
151 The complete configuration hierarchy, from highest to lowest precedence, is as follows:
152
153 1. Inline configuration
154 1. `/*eslint-disable*/` and `/*eslint-enable*/`
155 1. `/*global*/`
156 1. `/*eslint*/`
157 1. `/*eslint-env*/`
158 1. Command line options (or CLIEngine equivalents):
159 1. `--global`
160 1. `--rule`
161 1. `--env`
162 1. `-c`, `--config`
163 1. Project-level configuration:
164 1. `.eslintrc.*` or `package.json` file in the same directory as the linted file
165 1. Continue searching for `.eslintrc.*` and `package.json` files in ancestor directories up to and including the root directory or until a config with `"root": true` is found.
166
167 ## Extending Configuration Files
168
169 A configuration file, once extended, can inherit all the traits of another configuration file (including rules, plugins, and language options) and modify all the options. As a result, there are three configurations, as defined below:
170
171 * Base config: the configuration that is extended.
172 * Derived config: the configuration that extends the base configuration.
173 * Resulting actual config: the result of merging the derived configuration into the base configuration.
174
175 The `extends` property value is either:
176
177 * a string that specifies a configuration (either a path to a config file, the name of a shareable config, `eslint:recommended`, or `eslint:all`)
178 * an array of strings where each additional configuration extends the preceding configurations
179
180 ESLint extends configurations recursively, so a base configuration can also have an `extends` property. Relative paths and shareable config names in an `extends` property are resolved from the location of the config file where they appear.
181
182 The `eslint-config-` prefix can be omitted from the configuration name. For example, `airbnb` resolves as `eslint-config-airbnb`.
183
184 The `rules` property can do any of the following to extend (or override) the set of rules:
185
186 * enable additional rules
187 * change an inherited rule's severity without changing its options:
188 * Base config: `"eqeqeq": ["error", "allow-null"]`
189 * Derived config: `"eqeqeq": "warn"`
190 * Resulting actual config: `"eqeqeq": ["warn", "allow-null"]`
191 * override options for rules from base configurations:
192 * Base config: `"quotes": ["error", "single", "avoid-escape"]`
193 * Derived config: `"quotes": ["error", "single"]`
194 * Resulting actual config: `"quotes": ["error", "single"]`
195
196 ### Using a shareable configuration package
197
198 A [sharable configuration](https://eslint.org/docs/developer-guide/shareable-configs) is an npm package that exports a configuration object. Make sure that you have installed the package in your project root directory, so that ESLint can require it.
199
200 The `extends` property value can omit the `eslint-config-` prefix of the package name.
201
202 The `eslint --init` command can create a configuration so you can extend a popular style guide (for example, `eslint-config-standard`).
203
204 Example of a configuration file in YAML format:
205
206 ```yaml
207 extends: standard
208 rules:
209 comma-dangle:
210 - error
211 - always
212 no-empty: warn
213 ```
214
215 ### Using `eslint:recommended`
216
217 Using `"eslint:recommended"` in the `extends` property enables a subset of core rules that report common problems (these rules are identified with a checkmark (recommended) on the [rules page](https://eslint.org/docs/rules/)).
218
219 Here's an example of extending `eslint:recommended` and overriding some of the set configuration options:
220
221 Example of a configuration file in JavaScript format:
222
223 ```js
224 module.exports = {
225 "extends": "eslint:recommended",
226 "rules": {
227 // enable additional rules
228 "indent": ["error", 4],
229 "linebreak-style": ["error", "unix"],
230 "quotes": ["error", "double"],
231 "semi": ["error", "always"],
232
233 // override configuration set by extending "eslint:recommended"
234 "no-empty": "warn",
235 "no-cond-assign": ["error", "always"],
236
237 // disable rules from base configurations
238 "for-direction": "off",
239 }
240 }
241 ```
242
243 ### Using a configuration from a plugin
244
245 A [plugin](https://eslint.org/docs/developer-guide/working-with-plugins) is an npm package that can add various extensions to ESLint. A plugin can perform numerous functions, including but not limited to adding new rules and exporting [shareable configurations](https://eslint.org/docs/developer-guide/working-with-plugins#configs-in-plugins). Make sure the package has been installed in a directory where ESLint can require it.
246
247 The `plugins` [property value](./plugins.md#configuring-plugins) can omit the `eslint-plugin-` prefix of the package name.
248
249 The `extends` property value can consist of:
250
251 * `plugin:`
252 * the package name (from which you can omit the prefix, for example, `react` is short for `eslint-plugin-react`)
253 * `/`
254 * the configuration name (for example, `recommended`)
255
256 Example of a configuration file in JSON format:
257
258 ```json
259 {
260 "plugins": [
261 "react"
262 ],
263 "extends": [
264 "eslint:recommended",
265 "plugin:react/recommended"
266 ],
267 "rules": {
268 "react/no-set-state": "off"
269 }
270 }
271 ```
272
273 ### Using a configuration file
274
275 The `extends` property value can be an absolute or relative path to a base [configuration file](#using-configuration-files). ESLint resolves a relative path to a base configuration file relative to the configuration file that uses it.
276
277 Example of a configuration file in JSON format:
278
279 ```json
280 {
281 "extends": [
282 "./node_modules/coding-standard/eslintDefaults.js",
283 "./node_modules/coding-standard/.eslintrc-es6",
284 "./node_modules/coding-standard/.eslintrc-jsx"
285 ],
286 "rules": {
287 "eqeqeq": "warn"
288 }
289 }
290 ```
291
292 ### Using `"eslint:all"`
293
294 The `extends` property value can be `"eslint:all"` to enable all core rules in the currently installed version of ESLint. The set of core rules can change at any minor or major version of ESLint.
295
296 **Important:** This configuration is **not recommended for production use** because it changes with every minor and major version of ESLint. Use it at your own risk.
297
298 You might enable all core rules as a shortcut to explore rules and options while you decide on the configuration for a project, especially if you rarely override options or disable rules. The default options for rules are not endorsements by ESLint (for example, the default option for the [`quotes`](https://eslint.org/docs/rules/quotes) rule does not mean double quotes are better than single quotes).
299
300 If your configuration extends `eslint:all`, after you upgrade to a newer major or minor version of ESLint, review the reported problems before you use the `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fix), so you know if a new fixable rule will make changes to the code.
301
302 Example of a configuration file in JavaScript format:
303
304 ```js
305 module.exports = {
306 "extends": "eslint:all",
307 "rules": {
308 // override default options
309 "comma-dangle": ["error", "always"],
310 "indent": ["error", 2],
311 "no-cond-assign": ["error", "always"],
312
313 // disable now, but enable in the future
314 "one-var": "off", // ["error", "never"]
315
316 // disable
317 "init-declarations": "off",
318 "no-console": "off",
319 "no-inline-comments": "off",
320 }
321 }
322 ```
323
324 ## Configuration Based on Glob Patterns
325
326 <b>v4.1.0+.</b> Sometimes a more fine-controlled configuration is necessary, for example, if the configuration for files within the same directory has to be different. Therefore you can provide configurations under the `overrides` key that will only apply to files that match specific glob patterns, using the same format you would pass on the command line (e.g., `app/**/*.test.js`).
327
328 ### How do overrides work?
329
330 It is possible to override settings based on file glob patterns in your configuration by using the `overrides` key. An example of using the `overrides` key is as follows:
331
332 In your `.eslintrc.json`:
333
334 ```json
335 {
336 "rules": {
337 "quotes": ["error", "double"]
338 },
339
340 "overrides": [
341 {
342 "files": ["bin/*.js", "lib/*.js"],
343 "excludedFiles": "*.test.js",
344 "rules": {
345 "quotes": ["error", "single"]
346 }
347 }
348 ]
349 }
350 ```
351
352 Here is how overrides work in a configuration file:
353
354 * The patterns are applied against the file path relative to the directory of the config file. For example, if your config file has the path `/Users/john/workspace/any-project/.eslintrc.js` and the file you want to lint has the path `/Users/john/workspace/any-project/lib/util.js`, then the pattern provided in `.eslintrc.js` will be executed against the relative path `lib/util.js`.
355 * Glob pattern overrides have higher precedence than the regular configuration in the same config file. Multiple overrides within the same config are applied in order. That is, the last override block in a config file always has the highest precedence.
356 * A glob specific configuration works almost the same as any other ESLint config. Override blocks can contain any configuration options that are valid in a regular config, with the exception of `root` and `ignorePatterns`.
357 * A glob specific configuration can have an `extends` setting, but the `root` property in the extended configs is ignored. The `ignorePatterns` property in the extended configs is used only for the files the glob specific configuration matched.
358 * Nested `overrides` setting will be applied only if the glob patterns of both of the parent config and the child config matched. This is the same when the extended configs have an `overrides` setting.
359 * Multiple glob patterns can be provided within a single override block. A file must match at least one of the supplied patterns for the configuration to apply.
360 * Override blocks can also specify patterns to exclude from matches. If a file matches any of the excluded patterns, the configuration won't apply.
361
362 ### Relative glob patterns
363
364 ```
365 project-root
366 ├── app
367 │ ├── lib
368 │ │ ├── foo.js
369 │ │ ├── fooSpec.js
370 │ ├── components
371 │ │ ├── bar.js
372 │ │ ├── barSpec.js
373 │ ├── .eslintrc.json
374 ├── server
375 │ ├── server.js
376 │ ├── serverSpec.js
377 ├── .eslintrc.json
378 ```
379
380 The config in `app/.eslintrc.json` defines the glob pattern `**/*Spec.js`. This pattern is relative to the base directory of `app/.eslintrc.json`. So, this pattern would match `app/lib/fooSpec.js` and `app/components/barSpec.js` but **NOT** `server/serverSpec.js`. If you defined the same pattern in the `.eslintrc.json` file within in the `project-root` folder, it would match all three of the `*Spec` files.
381
382 If a config is provided via the `--config` CLI option, the glob patterns in the config are relative to the current working directory rather than the base directory of the given config. For example, if `--config configs/.eslintrc.json` is present, the glob patterns in the config are relative to `.` rather than `./configs`.
383
384 ### Specifying target files to lint
385
386 If you specified directories with CLI (e.g., `eslint lib`), ESLint searches target files in the directory to lint. The target files are `*.js` or the files that match any of `overrides` entries (but exclude entries that are any of `files` end with `*`).
387
388 If you specified the [`--ext`](https://eslint.org/docs/user-guide/command-line-interface#ext) command line option along with directories, the target files are only the files that have specified file extensions regardless of `overrides` entries.
389
390 ## Personal Configuration Files (deprecated)
391
392 ⚠️ **This feature has been deprecated**. This feature will be removed in the 8.0.0 release. If you want to continue to use personal configuration files, please use the [`--config` CLI option](https://eslint.org/docs/user-guide/command-line-interface#-c---config). For more information regarding this decision, please see [RFC 28](https://github.com/eslint/rfcs/pull/28) and [RFC 32](https://github.com/eslint/rfcs/pull/32).
393
394 `~/` refers to [the home directory of the current user on your preferred operating system](https://nodejs.org/api/os.html#os_os_homedir). The personal configuration file being referred to here is `~/.eslintrc.*` file, which is currently handled differently than other configuration files.
395
396 ### How does ESLint find personal configuration files?
397
398 If `eslint` could not find any configuration file in the project, `eslint` loads `~/.eslintrc.*` file.
399
400 If `eslint` could find configuration files in the project, `eslint` ignores `~/.eslintrc.*` file even if it's in an ancestor directory of the project directory.
401
402 ### How do personal configuration files behave?
403
404 `~/.eslintrc.*` files behave similarly to regular configuration files, with some exceptions:
405
406 `~/.eslintrc.*` files load shareable configs and custom parsers from `~/node_modules/` – similarly to `require()` – in the user's home directory. Please note that it doesn't load global-installed packages.
407
408 `~/.eslintrc.*` files load plugins from `$CWD/node_modules` by default in order to identify plugins uniquely. If you want to use plugins with `~/.eslintrc.*` files, plugins must be installed locally per project. Alternatively, you can use the [`--resolve-plugins-relative-to` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--resolve-plugins-relative-to) to change the location from which ESLint loads plugins.