]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/user-guide/configuring/language-options.md
import eslint 7.28.0
[pve-eslint.git] / eslint / docs / user-guide / configuring / language-options.md
CommitLineData
5422a9cc
TL
1# Language Options
2
3
4* [Specifying Environments](#specifying-environments)
5* [Specifying Globals](#specifying-globals)
6* [Specifying Parser Options](#specifying-parser-options)
7
8## Specifying Environments
9
10An environment provides predefined global variables. The available environments are:
11
12* `browser` - browser global variables.
13* `node` - Node.js global variables and Node.js scoping.
14* `commonjs` - CommonJS global variables and CommonJS scoping (use this for browser-only code that uses Browserify/WebPack).
15* `shared-node-browser` - Globals common to both Node.js and Browser.
16* `es6` - enable all ECMAScript 6 features except for modules (this automatically sets the `ecmaVersion` parser option to 6).
17* `es2017` - adds all ECMAScript 2017 globals and automatically sets the `ecmaVersion` parser option to 8.
18* `es2020` - adds all ECMAScript 2020 globals and automatically sets the `ecmaVersion` parser option to 11.
19* `es2021` - adds all ECMAScript 2021 globals and automatically sets the `ecmaVersion` parser option to 12.
20* `worker` - web workers global variables.
21* `amd` - defines `require()` and `define()` as global variables as per the [amd](https://github.com/amdjs/amdjs-api/wiki/AMD) spec.
22* `mocha` - adds all of the Mocha testing global variables.
23* `jasmine` - adds all of the Jasmine testing global variables for version 1.3 and 2.0.
24* `jest` - Jest global variables.
25* `phantomjs` - PhantomJS global variables.
26* `protractor` - Protractor global variables.
27* `qunit` - QUnit global variables.
28* `jquery` - jQuery global variables.
29* `prototypejs` - Prototype.js global variables.
30* `shelljs` - ShellJS global variables.
31* `meteor` - Meteor global variables.
32* `mongo` - MongoDB global variables.
33* `applescript` - AppleScript global variables.
34* `nashorn` - Java 8 Nashorn global variables.
35* `serviceworker` - Service Worker global variables.
36* `atomtest` - Atom test helper globals.
37* `embertest` - Ember test helper globals.
38* `webextensions` - WebExtensions globals.
39* `greasemonkey` - GreaseMonkey globals.
40
41These environments are not mutually exclusive, so you can define more than one at a time.
42
43Environments can be specified inside of a file, in configuration files or using the `--env` [command line](https://eslint.org/docs/user-guide/command-line-interface) flag.
44
45### Using configuration comments
46
47To specify environments using a comment inside of your JavaScript file, use the following format:
48
49```js
50/* eslint-env node, mocha */
51```
52
53This enables Node.js and Mocha environments.
54
55### Using configuration files
56
57To specify environments in a configuration file, use the `env` key and specify which environments you want to enable by setting each to `true`. For example, the following enables the browser and Node.js environments:
58
59```json
60{
61 "env": {
62 "browser": true,
63 "node": true
64 }
65}
66```
67
68Or in a `package.json` file
69
70```json
71{
72 "name": "mypackage",
73 "version": "0.0.1",
74 "eslintConfig": {
75 "env": {
76 "browser": true,
77 "node": true
78 }
79 }
80}
81```
82
83And in YAML:
84
85```yaml
86---
87 env:
88 browser: true
89 node: true
90```
91
92### Using a plugin
93
94If you want to use an environment from a plugin, be sure to specify the plugin name in the `plugins` array and then use the unprefixed plugin name, followed by a slash, followed by the environment name. For example:
95
96```json
97{
98 "plugins": ["example"],
99 "env": {
100 "example/custom": true
101 }
102}
103```
104
105Or in a `package.json` file
106
107```json
108{
109 "name": "mypackage",
110 "version": "0.0.1",
111 "eslintConfig": {
112 "plugins": ["example"],
113 "env": {
114 "example/custom": true
115 }
116 }
117}
118```
119
120## Specifying Globals
121
122Some of ESLint's core rules rely on knowledge of the global variables available to your code at runtime. Since these can vary greatly between different environments as well as be modified at runtime, ESLint makes no assumptions about what global variables exist in your execution environment. If you would like to use rules that require knowledge of what global variables are available, you can define global variables in your configuration file or by using configuration comments in your source code.
123
124### Using configuration comments
125
126To specify globals using a comment inside of your JavaScript file, use the following format:
127
128```js
129/* global var1, var2 */
130```
131
132This defines two global variables, `var1` and `var2`. If you want to optionally specify that these global variables can be written to (rather than only being read), then you can set each with a `"writable"` flag:
133
134```js
135/* global var1:writable, var2:writable */
136```
137
138### Using configuration files
139
140To configure global variables inside of a configuration file, set the `globals` configuration property to an object containing keys named for each of the global variables you want to use. For each global variable key, set the corresponding value equal to `"writable"` to allow the variable to be overwritten or `"readonly"` to disallow overwriting. For example:
141
142```json
143{
144 "globals": {
145 "var1": "writable",
146 "var2": "readonly"
147 }
148}
149```
150
151And in YAML:
152
153```yaml
154---
155 globals:
156 var1: writable
157 var2: readonly
158```
159
160These examples allow `var1` to be overwritten in your code, but disallow it for `var2`.
161
162Globals can be disabled with the string `"off"`. For example, in an environment where most ES2015 globals are available but `Promise` is unavailable, you might use this config:
163
164```json
165{
166 "env": {
167 "es6": true
168 },
169 "globals": {
170 "Promise": "off"
171 }
172}
173```
174
175For historical reasons, the boolean value `false` and the string value `"readable"` are equivalent to `"readonly"`. Similarly, the boolean value `true` and the string value `"writeable"` are equivalent to `"writable"`. However, the use of older values is deprecated.
176
177
178## Specifying Parser Options
179
180ESLint allows you to specify the JavaScript language options you want to support. By default, ESLint expects ECMAScript 5 syntax. You can override that setting to enable support for other ECMAScript versions as well as JSX by using parser options.
181
182Please note that supporting JSX syntax is not the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn't recognize. We recommend using [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react) if you are using React and want React semantics.
183By the same token, supporting ES6 syntax is not the same as supporting new ES6 globals (e.g., new types such as
184`Set`).
185For ES6 syntax, use `{ "parserOptions": { "ecmaVersion": 6 } }`; for new ES6 global variables, use `{ "env":
186{ "es6": true } }`. `{ "env": { "es6": true } }` enables ES6 syntax automatically, but `{ "parserOptions": { "ecmaVersion": 6 } }` does not enable ES6 globals automatically.
187
188Parser options are set in your `.eslintrc.*` file by using the `parserOptions` property. The available options are:
189
190* `ecmaVersion` - set to 3, 5 (default), 6, 7, 8, 9, 10, 11, or 12 to specify the version of ECMAScript syntax you want to use. You can also set to 2015 (same as 6), 2016 (same as 7), 2017 (same as 8), 2018 (same as 9), 2019 (same as 10), 2020 (same as 11), or 2021 (same as 12) to use the year-based naming.
191* `sourceType` - set to `"script"` (default) or `"module"` if your code is in ECMAScript modules.
192* `ecmaFeatures` - an object indicating which additional language features you'd like to use:
193 * `globalReturn` - allow `return` statements in the global scope
194 * `impliedStrict` - enable global [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) (if `ecmaVersion` is 5 or greater)
195 * `jsx` - enable [JSX](https://facebook.github.io/jsx/)
196
197Here's an example `.eslintrc.json` file:
198
199```json
200{
201 "parserOptions": {
202 "ecmaVersion": 6,
203 "sourceType": "module",
204 "ecmaFeatures": {
205 "jsx": true
206 }
207 },
208 "rules": {
209 "semi": "error"
210 }
211}
212```
213
214Setting parser options helps ESLint determine what is a parsing error. All language options are `false` by default.