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