配置语言选项(已弃用)

JavaScript 生态系统具有多种运行时、版本、扩展和框架。其中每一个都可以有不同的支持语法和全局变量。ESLint 允许你配置特定于项目中使用的 JavaScript 的语言选项,例如自定义全局变量。你还可以使用插件来扩展 ESLint 以支持你项目的语言选项。

¥The JavaScript ecosystem has a variety of runtimes, versions, extensions, and frameworks. Each of these can have different supported syntax and global variables. ESLint lets you configure language options specific to the JavaScript used in your project, like custom global variables. You can also use plugins to extend ESLint to support your project’s language options.

指定环境

¥Specifying Environments

环境提供预定义的全局变量。可用的环境是:

¥An environment provides predefined global variables. The available environments are:

  • browser - 浏览器全局变量。

    ¥browser - browser global variables.

  • node - Node.js 全局变量和 Node.js 作用域。

    ¥node - Node.js global variables and Node.js scoping.

  • commonjs - CommonJS 全局变量和 CommonJS 作用域(将其用于使用 Browserify/WebPack 的仅限浏览器的代码)。

    ¥commonjs - CommonJS global variables and CommonJS scoping (use this for browser-only code that uses Browserify/WebPack).

  • shared-node-browser - Node.js 和浏览器共有的全局变量。

    ¥shared-node-browser - Globals common to both Node.js and Browser.

  • es6 - 启用除模块之外的所有 ECMAScript 6 功能(这会自动将 ecmaVersion 解析器选项设置为 6)。

    ¥es6 - enable all ECMAScript 6 features except for modules (this automatically sets the ecmaVersion parser option to 6).

  • es2016 - 添加所有 ECMAScript 2016 全局变量并自动将 ecmaVersion 解析器选项设置为 7。

    ¥es2016 - adds all ECMAScript 2016 globals and automatically sets the ecmaVersion parser option to 7.

  • es2017 - 添加所有 ECMAScript 2017 全局变量并自动将 ecmaVersion 解析器选项设置为 8。

    ¥es2017 - adds all ECMAScript 2017 globals and automatically sets the ecmaVersion parser option to 8.

  • es2018 - 添加所有 ECMAScript 2018 全局变量并自动将 ecmaVersion 解析器选项设置为 9。

    ¥es2018 - adds all ECMAScript 2018 globals and automatically sets the ecmaVersion parser option to 9.

  • es2019 - 添加所有 ECMAScript 2019 全局变量并自动将 ecmaVersion 解析器选项设置为 10。

    ¥es2019 - adds all ECMAScript 2019 globals and automatically sets the ecmaVersion parser option to 10.

  • es2020 - 添加所有 ECMAScript 2020 全局变量并自动将 ecmaVersion 解析器选项设置为 11。

    ¥es2020 - adds all ECMAScript 2020 globals and automatically sets the ecmaVersion parser option to 11.

  • es2021 - 添加所有 ECMAScript 2021 全局变量并自动将 ecmaVersion 解析器选项设置为 12。

    ¥es2021 - adds all ECMAScript 2021 globals and automatically sets the ecmaVersion parser option to 12.

  • es2022 - 添加所有 ECMAScript 2022 全局变量并自动将 ecmaVersion 解析器选项设置为 13。

    ¥es2022 - adds all ECMAScript 2022 globals and automatically sets the ecmaVersion parser option to 13.

  • es2023 - 添加所有 ECMAScript 2023 全局变量并自动将 ecmaVersion 解析器选项设置为 14。

    ¥es2023 - adds all ECMAScript 2023 globals and automatically sets the ecmaVersion parser option to 14.

  • es2024 - 添加所有 ECMAScript 2024 全局变量并自动将 ecmaVersion 解析器选项设置为 15。

    ¥es2024 - adds all ECMAScript 2024 globals and automatically sets the ecmaVersion parser option to 15.

  • worker - web worker 全局变量。

    ¥worker - web workers global variables.

  • amd - 根据 amd 规范将 require()define() 定义为全局变量。

    ¥amd - defines require() and define() as global variables as per the amd spec.

  • mocha - 添加所有 Mocha 测试全局变量。

    ¥mocha - adds all of the Mocha testing global variables.

  • jasmine - 添加所有 Jasmine 测试全局变量(适用于版本 1.3 和 2.0)。

    ¥jasmine - adds all of the Jasmine testing global variables for version 1.3 and 2.0.

  • jest - Jest 全局变量。

    ¥jest - Jest global variables.

  • phantomjs - PhantomJS 全局变量。

    ¥phantomjs - PhantomJS global variables.

  • protractor - Protractor 全局变量。

    ¥protractor - Protractor global variables.

  • qunit - QUnit 全局变量。

    ¥qunit - QUnit global variables.

  • jquery - jQuery 全局变量。

    ¥jquery - jQuery global variables.

  • prototypejs - Prototype.js 全局变量。

    ¥prototypejs - Prototype.js global variables.

  • shelljs - ShellJS 全局变量。

    ¥shelljs - ShellJS global variables.

  • meteor - Meteor 全局变量。

    ¥meteor - Meteor global variables.

  • mongo - MongoDB 全局变量。

    ¥mongo - MongoDB global variables.

  • applescript - AppleScript 全局变量。

    ¥applescript - AppleScript global variables.

  • nashorn - Java 8 Nashorn 全局变量。

    ¥nashorn - Java 8 Nashorn global variables.

  • serviceworker - Service Worker 全局变量。

    ¥serviceworker - Service Worker global variables.

  • atomtest - Atom 测试助手全局变量。

    ¥atomtest - Atom test helper globals.

  • embertest - Ember 测试助手全局变量。

    ¥embertest - Ember test helper globals.

  • webextensions - WebExtensions 全局变量。

    ¥webextensions - WebExtensions globals.

  • greasemonkey - GreaseMonkey 全局变量。

    ¥greasemonkey - GreaseMonkey globals.

这些环境并不相互排斥,因此你可以一次定义多个环境。

¥These environments are not mutually exclusive, so you can define more than one at a time.

可以在文件内部、配置文件中或使用 --env 命令行 标志指定环境。

¥Environments can be specified inside of a file, in configuration files or using the --env command line flag.

使用配置注释

¥Using configuration comments

要在 JavaScript 文件内使用注释指定环境,请使用以下格式:

¥To specify environments with a comment inside of a JavaScript file, use the following format:

/* eslint-env node, mocha */

这将启用 Node.js 和 Mocha 环境。

¥This enables Node.js and Mocha environments.

使用配置文件

¥Using configuration files

要在配置文件中指定环境,请使用 env 键。通过将每个环境设置为 true 来指定要启用的环境。例如,以下内容启用浏览器和 Node.js 环境:

¥To specify environments in a configuration file, use the env key. Specify which environments you want to enable by setting each to true. For example, the following enables the browser and Node.js environments:

{
	"env": {
		"browser": true,
		"node": true
	}
}

或者在 package.json 文件中

¥Or in a package.json file

{
	"name": "mypackage",
	"version": "0.0.1",
	"eslintConfig": {
		"env": {
			"browser": true,
			"node": true
		}
	}
}

以及 YAML 中的内容:

¥And in YAML:

---
env:
    browser: true
    node: true

使用插件

¥Using a plugin

如果你想使用插件中的环境,请确保在 plugins 数组中指定插件名称,然后使用无前缀的插件名称,后跟斜杠,后跟环境名称。例如:

¥If 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:

{
	"plugins": ["example"],
	"env": {
		"example/custom": true
	}
}

或者在 package.json 文件中

¥Or in a package.json file

{
	"name": "mypackage",
	"version": "0.0.1",
	"eslintConfig": {
		"plugins": ["example"],
		"env": {
			"example/custom": true
		}
	}
}

指定全局变量

¥Specifying Globals

ESLint 的一些核心规则依赖于代码在运行时可用的全局变量的知识。由于这些变量在不同环境之间可能会有很大差异,并且在运行时会被修改,因此 ESLint 不会假设你的执行环境中存在哪些全局变量。如果你想使用需要知道哪些全局变量可用的规则,你可以在配置文件中定义全局变量,或者在源代码中使用配置注释。

¥Some 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.

使用配置注释

¥Using configuration comments

要使用 JavaScript 文件中的注释指定全局变量,请使用以下格式:

¥To specify globals using a comment inside of your JavaScript file, use the following format:

/* global var1, var2 */

这定义了两个全局变量,var1var2。如果你想选择性地指定这些全局变量可以被写入(而不仅仅是被读取),那么你可以为每个变量设置一个 "writable" 标志:

¥This 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:

/* global var1:writable, var2:writable */

使用配置文件

¥Using configuration files

要在配置文件中配置全局变量,请将 globals 配置属性设置为包含要使用的每个全局变量的键的对象。对于每个全局变量键,设置相应的值等于 "writable" 以允许变量被覆盖或 "readonly" 不允许覆盖。例如:

¥To 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:

{
	"globals": {
		"var1": "writable",
		"var2": "readonly"
	}
}

以及 YAML 中的内容:

¥And in YAML:

---
globals:
    var1: writable
    var2: readonly

这些示例允许在代码中覆盖 var1,但不允许覆盖 var2

¥These examples allow var1 to be overwritten in your code, but disallow it for var2.

可以通过将其值设置为 "off" 来禁用全局变量。例如,在大多数 ES2015 全局变量可用但 Promise 不可用的环境中,你可以使用此配置:

¥Globals can be disabled by setting their value to "off". For example, in an environment where most ES2015 globals are available but Promise is unavailable, you might use this config:

{
	"env": {
		"es6": true
	},
	"globals": {
		"Promise": "off"
	}
}

由于历史原因,布尔值 false 和字符串值 "readable" 相当于 "readonly"。类似地,布尔值 true 和字符串值 "writeable" 等价于 "writable"。但是,不推荐使用这些旧值。

¥For 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 these older values is deprecated.

指定解析器选项

¥Specifying Parser Options

ESLint 允许你指定你想要支持的 JavaScript 语言选项。默认情况下,ESLint 需要 ECMAScript 5 语法。你可以使用解析器选项覆盖该设置以启用对其他 ECMAScript 版本和 JSX 的支持。

¥ESLint 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 and JSX using parser options.

请注意,支持 JSX 语法与支持 React 不同。React 将特定语义应用于 ESLint 无法识别的 JSX 语法。如果你使用 React,我们建议使用 eslint-plugin-react

¥Please 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 if you are using React.

同样,支持 ES6 语法与支持新的 ES6 全局变量(例如,Set 等新类型)不同。对于 ES6 语法,请使用 { "parserOptions": { "ecmaVersion": 6 } };对于新的 ES6 全局变量,请使用 { "env": { "es6": true } }。设置 { "env": { "es6": true } } 会自动启用 ES6 语法,但 { "parserOptions": { "ecmaVersion": 6 } } 不会自动启用 ES6 全局变量。总之,要仅支持 ES6 语法,请使用 { "parserOptions": { "ecmaVersion": 6 } },要同时支持 ES6 语法和新的 ES6 全局变量(例如 Set 和其他变量),请使用 { "env": { "es6": true } }

¥By the same token, supporting ES6 syntax is not the same as supporting new ES6 globals (e.g., new types such as Set). For ES6 syntax, use { "parserOptions": { "ecmaVersion": 6 } }; for new ES6 global variables, use { "env": { "es6": true } }. Setting { "env": { "es6": true } } enables ES6 syntax automatically, but { "parserOptions": { "ecmaVersion": 6 } } does not enable ES6 globals automatically. In summary, to support only ES6 syntax, use { "parserOptions": { "ecmaVersion": 6 } }, and to support both ES6 syntax and new ES6 global variables, such as Set and others, use { "env": { "es6": true } }.

解析器选项在 .eslintrc.* 文件中使用 parserOptions 属性设置。可用的选项有:

¥Parser options are set in your .eslintrc.* file with the parserOptions property. The available options are:

  • ecmaVersion - 设置为 3、5(默认)、6、7、8、9、10、11、12、13、14、15 或 16 以指定你要使用的 ECMAScript 语法版本。你还可以将其设置为 2015(与 6 相同)、2016(与 7 相同)、2017(与 8 相同)、2018(与 9 相同)、2019(与 10 相同)、2020(与 11 相同)、2021(与 12 相同)、2022(与 13 相同)、2023(与 14 相同)、2024(与 15 相同)或 2025(与 16 相同)以使用基于年份的命名。你还可以将 "latest" 设置为使用最新支持的版本。

    ¥ecmaVersion - set to 3, 5 (default), 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, or 16 to specify the version of ECMAScript syntax you want to use. You can also set it 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), 2022 (same as 13), 2023 (same as 14), 2024 (same as 15), or 2025 (same as 16) to use the year-based naming. You can also set "latest" to use the most recently supported version.

  • sourceType - 如果你的代码在 ECMAScript 模块中,则设置为 "script"(默认)或 "module"

    ¥sourceType - set to "script" (default) or "module" if your code is in ECMAScript modules.

  • allowReserved - 允许使用保留字作为标识符(如果 ecmaVersion 为 3)。

    ¥allowReserved - allow the use of reserved words as identifiers (if ecmaVersion is 3).

  • ecmaFeatures - 一个对象,指示你要使用哪些附加语言功能:

    ¥ecmaFeatures - an object indicating which additional language features you’d like to use:

    • globalReturn - 允许全局范围内的 return 语句

      ¥globalReturn - allow return statements in the global scope

    • impliedStrict - 启用全局 严格模式(如果 ecmaVersion 为 5 或更大)

      ¥impliedStrict - enable global strict mode (if ecmaVersion is 5 or greater)

    • jsx - 启用 JSX

      ¥jsx - enable JSX

以下是示例 .eslintrc.json 文件:

¥Here’s an example .eslintrc.json file:

{
	"parserOptions": {
		"ecmaVersion": "latest",
		"sourceType": "module",
		"ecmaFeatures": {
			"jsx": true
		}
	},
	"rules": {
		"semi": "error"
	}
}

设置解析器选项有助于 ESLint 确定什么是解析错误。默认情况下,所有语言选项都是 false

¥Setting parser options helps ESLint determine what is a parsing error. All language options are false by default.