配置文件(已弃用)

你可以将 ESLint 项目配置放在配置文件中。你可以包含内置规则、你希望它们如何执行、具有自定义规则的插件、可共享配置、你希望规则应用到哪些文件等等。

¥You can put your ESLint project configuration in a configuration file. You can include built-in rules, how you want them enforced, plugins with custom rules, shareable configurations, which files you want rules to apply to, and more.

配置文件格式

¥Configuration File Formats

ESLint 支持多种格式的配置文件:

¥ESLint supports configuration files in several formats:

  • JavaScript - 使用 .eslintrc.js 并导出包含配置的对象。

    ¥JavaScript - use .eslintrc.js and export an object containing your configuration.

  • JavaScript (ESM) - 在 JavaScript 包中运行 ESLint 时使用 .eslintrc.cjs,并在其 package.json 中指定 "type":"module"。请注意,ESLint 目前不支持 ESM 配置。

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

  • YAML - 使用 .eslintrc.yaml.eslintrc.yml 定义配置结构。

    ¥YAML - use .eslintrc.yaml or .eslintrc.yml to define the configuration structure.

  • JSON - 使用 .eslintrc.json 定义配置结构。ESLint 的 JSON 文件还允许 JavaScript 样式的注释。

    ¥JSON - use .eslintrc.json to define the configuration structure. ESLint’s JSON files also allow JavaScript-style comments.

  • package.json - 在你的 package.json 文件中创建一个 eslintConfig 属性并在那里定义你的配置。

    ¥package.json - create an eslintConfig property in your package.json file and define your configuration there.

如果同一目录中有多个配置文件,则 ESLint 仅使用一个。优先级顺序如下:

¥If there are multiple configuration files in the same directory, ESLint only uses one. The priority order is as follows:

  1. .eslintrc.js
  2. .eslintrc.cjs
  3. .eslintrc.yaml
  4. .eslintrc.yml
  5. .eslintrc.json
  6. package.json

使用配置文件

¥Using Configuration Files

有两种使用配置文件的方法。

¥There are two ways to use configuration files.

使用配置文件的第一种方法是通过 .eslintrc.*package.json 文件。ESLint 会自动在要 linted 的文件的目录中查找它们,并在连续的父目录中查找它们,一直到文件系统的根目录 (/)、当前用户的主目录 (~/),或者当指定 root: true 时。有关更多详细信息,请参阅下面的 级联和层次结构。当你希望对项目的不同部分使用不同的配置,或者希望其他人能够直接使用 ESLint 而无需记住传入配置文件时,配置文件会很有用。

¥The first way to use configuration files is via .eslintrc.* and package.json files. ESLint automatically looks 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 (/), the home directory of the current user (~/), or when root: true is specified. See Cascading and Hierarchy below for more details on this. 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.

使用配置文件的第二种方法是将文件保存在任何你想要的位置,并使用 --config 选项将其位置传递给 CLI,例如:

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

eslint -c myconfig.json myfiletotest.js

如果你使用一个配置文件并希望 ESLint 忽略任何 .eslintrc.* 文件,请确保将 --no-eslintrc--config 标志一起使用。

¥If you are using one configuration file and want ESLint to ignore any .eslintrc.* files, make sure to use --no-eslintrc along with the --config flag.

以下是使用 typescript-eslint 解析器支持 TypeScript 语法的示例 JSON 配置文件:

¥Here’s an example JSON configuration file that uses the typescript-eslint parser to support TypeScript syntax:

{
	"root": true,
	"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
	"parser": "@typescript-eslint/parser",
	"parserOptions": { "project": ["./tsconfig.json"] },
	"plugins": ["@typescript-eslint"],
	"rules": {
		"@typescript-eslint/strict-boolean-expressions": [
			2,
			{
				"allowString": false,
				"allowNumber": false
			}
		]
	},
	"ignorePatterns": ["src/**/*.test.ts", "src/frontend/generated/*"]
}

配置文件中的注释

¥Comments in configuration files

JSON 和 YAML 配置文件格式都支持注释(package.json 文件不应包含它们)。你可以对 JSON 文件使用 JavaScript 样式的注释,对 YAML 文件使用 YAML 样式的注释。ESLint 安全地忽略配置文件中的注释。这使你的配置文件更加人性化。

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

对于 JavaScript 样式的注释:

¥For JavaScript-style comments:

{
    "env": {
        "browser": true
    },
    "rules": {
        // Override our default settings just for this directory
        "eqeqeq": "warn",
        "strict": "off"
    }
}

对于 YAML 样式的注释:

¥For YAML-style comments:

env:
    browser: true
rules:
    # Override default settings
    eqeqeq: warn
    strict: off

添加共享设置

¥Adding Shared Settings

ESLint 支持将共享设置添加到配置文件中。插件使用 settings 来指定应在其所有规则中共享的信息。你可以将 settings 对象添加到 ESLint 配置文件中,并将其提供给每个执行的规则。如果你要添加自定义规则并希望它们能够访问相同的信息并易于配置,这可能会很有用。

¥ESLint supports adding shared settings into configuration files. Plugins use settings to specify the information that should be shared across all of its rules. You can add a settings object to the ESLint configuration file and it is supplied to every executed rule. This may be useful if you are adding custom rules and want them to have access to the same information and be easily configurable.

在 JSON 中:

¥In JSON:

{
	"settings": {
		"sharedData": "Hello"
	}
}

以及 YAML 中的内容:

¥And in YAML:

---
settings:
    sharedData: "Hello"

级联和层次结构

¥Cascading and Hierarchy

当使用 .eslintrc.*package.json 文件进行配置时,你可以利用配置级联。假设你的项目具有以下结构:

¥When using .eslintrc.* and package.json files for configuration, you can take advantage of configuration cascading. Suppose your project has the following structure:

your-project
├── .eslintrc.json
├── lib
│ └── source.js
└─┬ tests
  ├── .eslintrc.json
  └── test.js

配置级联基于被 lint 的文件的位置工作。如果与被 lint 的文件在同一目录中有 .eslintrc 文件,则该配置优先。然后,ESLint 搜索目录结构,合并沿途找到的任何 .eslintrc 文件,直到到达带有 root: true.eslintrc 文件或根目录。

¥The configuration cascade works based on the location of the file being linted. If there is an .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 an .eslintrc file with root: true or the root directory.

同样,如果根目录中有一个带有 eslintConfig 字段的 package.json 文件,则它描述的配置将应用于其下的所有子目录。但是,tests/ 目录中的 .eslintrc 文件描述的配置会覆盖冲突的规范。

¥In the same way, if there is a package.json file in the root directory with an eslintConfig field, the configuration it describes is applied to all subdirectories beneath it. However, the configuration described by the .eslintrc file in the tests/ directory overrides conflicting specifications.

your-project
├── package.json
├── lib
│ └── source.js
└─┬ tests
  ├── .eslintrc.json
  └── test.js

如果在同一目录中找到 .eslintrcpackage.json 文件,则 .eslintrc 优先,而不使用 package.json 文件。

¥If there is an .eslintrc and a package.json file found in the same directory, .eslintrc takes priority and the package.json file is not used.

默认情况下,ESLint 会在所有父文件夹中查找配置文件,直至根目录。如果你希望所有项目都遵循某种约定,这可能很有用,但有时会导致意外结果。要将 ESLint 限制到特定项目,请将 "root": true 放在 .eslintrc.* 文件或 package.json 文件的 eslintConfig 字段内,或放在项目根级别的 .eslintrc.* 文件中。一旦找到带有 "root": true 的配置,ESLint 就会停止在父文件夹中查找。

¥By default, ESLint looks 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 stops looking in parent folders once it finds a configuration with "root": true.

{
    "root": true
}

以及 YAML 中的内容:

¥And in YAML:

---
root: true

例如,考虑 projectA,它在 lib/ 目录中的 .eslintrc 文件中设置了 "root": true。在这种情况下,在对 main.js 进行 linting 时,会使用 lib/ 中的配置,但不使用 projectA/ 中的 .eslintrc 文件。

¥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/ are used, but the .eslintrc file in projectA/ is not.

home
└── user
    └── projectA
        ├── .eslintrc.json  <- Not used
        └── lib
            ├── .eslintrc.json  <- { "root": true }
            └── main.js

完整的配置层次结构(从最高优先级到最低优先级)如下:

¥The complete configuration hierarchy, from highest to lowest precedence, is as follows:

  1. 内联配置

    ¥Inline configuration

    1. /*eslint-disable*//*eslint-enable*/

      ¥/*eslint-disable*/ and /*eslint-enable*/

    2. /*global*/

    3. /*eslint*/

    4. /*eslint-env*/

  2. 命令行选项(或 CLIEngine 等效项):

    ¥Command line options (or CLIEngine equivalents):

    1. --global
    2. --rule
    3. --env
    4. -c, --config
  3. 项目级配置:

    ¥Project-level configuration:

    1. .eslintrc.*package.json 文件与 linted 文件位于同一目录中

      ¥.eslintrc.* or package.json file in the same directory as the linted file

    2. 继续在祖级目录中搜索 .eslintrc.*package.json 文件,直至根目录或直到找到带有 "root": true 的配置。

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

请注意,你首选操作系统上当前用户的主目录~/)在此上下文中也被视为根目录,并且搜索配置文件也会在那里停止。从 8.0.0 版本开始使用 删除对个人配置文件的支持,该目录中的配置文件将被忽略。

¥Please note that the home directory of the current user on your preferred operating system (~/) is also considered a root directory in this context and searching for configuration files stops there as well. And with the removal of support for Personal Configuration Files from the 8.0.0 release forward, configuration files present in that directory are ignored.

扩展配置文件

¥Extending Configuration Files

配置文件一旦扩展,就可以继承另一个配置文件的所有特性(包括规则、插件和语言选项)并修改所有选项。因此,有三种配置,定义如下:

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

  • 基本配置:扩展的配置。

    ¥Base config: the configuration that is extended.

  • 派生配置:扩展基本配置的配置。

    ¥Derived config: the configuration that extends the base configuration.

  • 结果实际配置:将派生配置合并到基本配置中的结果。

    ¥Resulting actual config: the result of merging the derived configuration into the base configuration.

extends 属性值可以是以下项之一:

¥The extends property value is either:

  • 指定配置的字符串(配置文件的路径、可共享配置的名称、eslint:recommendedeslint:all)。

    ¥a string that specifies a configuration (either a path to a config file, the name of a shareable config, eslint:recommended, or eslint:all).

  • 一个字符串数组,其中每个附加配置都会扩展前面的配置。

    ¥an array of strings where each additional configuration extends the preceding configurations.

ESLint 以递归方式扩展配置,因此基本配置也可以具有 extends 属性。extends 属性中的相对路径和可共享配置名称是从它们出现的配置文件的位置解析的。

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

可以从配置名称中省略 eslint-config- 前缀。例如,airbnb 解析为 eslint-config-airbnb

¥The eslint-config- prefix can be omitted from the configuration name. For example, airbnb resolves as eslint-config-airbnb.

rules 属性可以执行以下任何操作来扩展(或覆盖)规则集:

¥The rules property can do any of the following to extend (or override) the set of rules:

  • 启用附加规则

    ¥enable additional rules

  • 更改继承规则的严重性而不更改其选项:

    ¥change an inherited rule’s severity without changing its options:

    • 基本配置:"eqeqeq": ["error", "allow-null"]

      ¥Base config: "eqeqeq": ["error", "allow-null"]

    • 派生配置:"eqeqeq": "warn"

      ¥Derived config: "eqeqeq": "warn"

    • 结果实际配置:"eqeqeq": ["warn", "allow-null"]

      ¥Resulting actual config: "eqeqeq": ["warn", "allow-null"]

  • 覆盖来自基本配置的规则选项:

    ¥override options for rules from base configurations:

    • 基本配置:"quotes": ["error", "single", "avoid-escape"]

      ¥Base config: "quotes": ["error", "single", "avoid-escape"]

    • 派生配置:"quotes": ["error", "single"]

      ¥Derived config: "quotes": ["error", "single"]

    • 结果实际配置:"quotes": ["error", "single"]

      ¥Resulting actual config: "quotes": ["error", "single"]

  • 覆盖来自基本配置作为对象给出的规则选项:

    ¥override options for rules given as object from base configurations:

    • 基本配置:"max-lines": ["error", { "max": 200, "skipBlankLines": true, "skipComments": true }]

      ¥Base config: "max-lines": ["error", { "max": 200, "skipBlankLines": true, "skipComments": true }]

    • 派生配置:"max-lines": ["error", { "max": 100 }]

      ¥Derived config: "max-lines": ["error", { "max": 100 }]

    • 结果实际配置:"max-lines": ["error", { "max": 100 }] 其中 skipBlankLinesskipComments 默认为 false

      ¥Resulting actual config: "max-lines": ["error", { "max": 100 }] where skipBlankLines and skipComments default to false

使用可共享的配置包

¥Using a shareable configuration package

可共享配置 是一个导出配置对象的 npm 包。确保你已将包安装在项目根目录中,以便 ESLint 可以引用它。

¥A sharable configuration 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.

extends 属性值可以省略包名称的 eslint-config- 前缀。

¥The extends property value can omit the eslint-config- prefix of the package name.

npm init @eslint/config 命令可以创建一个配置,以便你可以扩展流行的样式指南(例如,eslint-config-standard)。

¥The npm init @eslint/config command can create a configuration so you can extend a popular style guide (for example, eslint-config-standard).

YAML 格式的配置文件示例:

¥Example of a configuration file in YAML format:

extends: standard
rules:
    comma-dangle:
        - error
        - always
    no-empty: warn

使用 eslint:recommended

¥Using eslint:recommended

extends 属性中使用 "eslint:recommended" 可启用报告常见问题的核心规则子集(这些规则在 规则页面 上用复选标记(推荐)标识)。

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

以下是扩展 eslint:recommended 并覆盖部分设置配置选项的示例:

¥Here’s an example of extending eslint:recommended and overriding some of the set configuration options:

JavaScript 格式的配置文件示例:

¥Example of a configuration file in JavaScript format:

module.exports = {
	extends: "eslint:recommended",
	rules: {
		// enable additional rules
		indent: ["error", 4],
		"linebreak-style": ["error", "unix"],
		quotes: ["error", "double"],
		semi: ["error", "always"],

		// override configuration set by extending "eslint:recommended"
		"no-empty": "warn",
		"no-cond-assign": ["error", "always"],

		// disable rules from base configurations
		"for-direction": "off",
	},
};

使用插件中的配置

¥Using a configuration from a plugin

plugin 是一个可以向 ESLint 添加各种扩展的 npm 包。插件可以执行多种功能,包括但不限于添加新规则和导出 可共享配置。确保包已安装在 ESLint 可以引用它的目录中。

¥A plugin 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. Make sure the package has been installed in a directory where ESLint can require it.

plugins 属性值 可以省略包名称的 eslint-plugin- 前缀。

¥The plugins property value can omit the eslint-plugin- prefix of the package name.

extends 属性值可以由以下项组成:

¥The extends property value can consist of:

  • plugin:

  • 包名称(你可以从中省略前缀,例如,reacteslint-plugin-react 的缩写)

    ¥the package name (from which you can omit the prefix, for example, react is short for eslint-plugin-react)

  • /

  • 配置名称(例如,recommended

    ¥the configuration name (for example, recommended)

JSON 格式的配置文件示例:

¥Example of a configuration file in JSON format:

{
	"plugins": ["react"],
	"extends": ["eslint:recommended", "plugin:react/recommended"],
	"rules": {
		"react/no-set-state": "off"
	}
}

使用配置文件

¥Using a configuration file

extends 属性值可以是基本 配置文件 的绝对路径或相对路径。ESLint 解析相对于使用它的配置文件的基本配置文件的相对路径。

¥The extends property value can be an absolute or relative path to a base configuration file. ESLint resolves a relative path to a base configuration file relative to the configuration file that uses it.

JSON 格式的配置文件示例:

¥Example of a configuration file in JSON format:

{
	"extends": [
		"./node_modules/coding-standard/eslintDefaults.js",
		"./node_modules/coding-standard/.eslintrc-es6",
		"./node_modules/coding-standard/.eslintrc-jsx"
	],
	"rules": {
		"eqeqeq": "warn"
	}
}

使用 "eslint:all"

¥Using "eslint:all"

extends 属性值可以是 "eslint:all",以启用当前安装的 ESLint 版本中的所有核心规则。核心规则集可以在 ESLint 的任何小版本或大版本中更改。

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

重要:不建议将此配置用于生产,因为它会随着 ESLint 的每个小版本和主要版本而变化。使用风险自负。

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

你可以启用所有核心规则作为快捷方式,以便在决定项目配置时探索规则和选项,特别是如果你很少覆盖选项或禁用规则。规则的默认选项不受 ESLint 的认可(例如,quotes 规则的默认选项并不意味着双引号比单引号更好)。

¥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 rule does not mean double quotes are better than single quotes).

如果你的配置扩展了 eslint:all,则在升级到较新的 ESLint 主要版本或次要版本后,请在 命令行 上使用 --fix 选项之前查看报告的问题,以便你知道新的可修复规则是否会对代码进行更改。

¥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, so you know if a new fixable rule will make changes to the code.

JavaScript 格式的配置文件示例:

¥Example of a configuration file in JavaScript format:

module.exports = {
	extends: "eslint:all",
	rules: {
		// override default options
		"comma-dangle": ["error", "always"],
		indent: ["error", 2],
		"no-cond-assign": ["error", "always"],

		// disable now, but enable in the future
		"one-var": "off", // ["error", "never"]

		// disable
		"init-declarations": "off",
		"no-console": "off",
		"no-inline-comments": "off",
	},
};

基于 Glob 模式的配置

¥Configuration Based on Glob Patterns

v4.1.0+。有时需要更精细控制的配置,例如,如果同一目录中的文件的配置必须不同。在这种情况下,你可以在 overrides 键下提供仅适用于与特定通配符模式匹配的文件的配置,使用与你在命令行上传递的相同格式(例如 app/**/*.test.js)。

¥v4.1.0+. Sometimes a more fine-controlled configuration is necessary, like if the configuration for files within the same directory has to be different. In this case, you can provide configurations under the overrides key that 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).

覆盖中的通配符模式使用 minimatch 语法

¥Glob patterns in overrides use minimatch syntax.

覆盖如何工作?

¥How do overrides work?

可以使用 overrides 键覆盖配置中基于文件通配符模式的设置。使用 overrides 键的示例如下:

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

在你的 .eslintrc.json 中:

¥In your .eslintrc.json:

{
	"rules": {
		"quotes": ["error", "double"]
	},

	"overrides": [
		{
			"files": ["bin/*.js", "lib/*.js"],
			"excludedFiles": "*.test.js",
			"rules": {
				"quotes": ["error", "single"]
			}
		}
	]
}

以下是配置文件中覆盖的工作方式:

¥Here is how overrides work in a configuration file:

  • 这些模式针对相对于配置文件目录的文件路径应用。例如,如果你的配置文件具有路径 /Users/john/workspace/any-project/.eslintrc.js 而你想要 lint 的文件具有路径 /Users/john/workspace/any-project/lib/util.js,则 .eslintrc.js 中提供的模式将针对相对路径 lib/util.js 执行。

    ¥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 is executed against the relative path lib/util.js.

  • 在同一个配置文件中,通配符模式覆盖的优先级高于常规配置。同一配置中的多个覆盖按顺序应用。也就是说,配置文件中的最后一个覆盖块始终具有最高优先级。

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

  • glob 特定配置的工作方式与任何其他 ESLint 配置几乎相同。覆盖块可以包含常规配置中有效的任何配置选项,但 rootignorePatterns 除外。

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

    • glob 特定配置可以具有 extends 设置,但扩展配置中的 root 属性将被忽略。扩展配置中的 ignorePatterns 属性仅用于 glob 特定配置匹配的文件。

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

    • 仅当父配置和子配置的通配符模式都匹配时,才会应用嵌套的 overrides 设置。当扩展配置具有 overrides 设置时,情况相同。

      ¥Nested overrides settings are applied only if the glob patterns of both the parent config and the child config are matched. This is the same when the extended configs have an overrides setting.

  • 可以在单个覆盖块中提供多个通配符模式。文件必须至少匹配提供的模式之一才能应用配置。

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

  • 覆盖块还可以指定要从匹配中排除的模式。如果文件与任何排除的模式匹配,则配置将不适用。

    ¥Override blocks can also specify patterns to exclude from matches. If a file matches any of the excluded patterns, the configuration won’t apply.

相对通配符模式

¥Relative glob patterns

project-root
├── app
│   ├── lib
│   │   ├── foo.js
│   │   ├── fooSpec.js
│   ├── components
│   │   ├── bar.js
│   │   ├── barSpec.js
│   ├── .eslintrc.json
├── server
│   ├── server.js
│   ├── serverSpec.js
├── .eslintrc.json

app/.eslintrc.json 中的配置定义了通配符模式 **/*Spec.js。此模式与 app/.eslintrc.json 的基本目录相关。因此,此模式将匹配 app/lib/fooSpec.jsapp/components/barSpec.js,但不匹配 server/serverSpec.js。如果你在 project-root 文件夹中的 .eslintrc.json 文件中定义了相同的模式,它将匹配所有三个 *Spec 文件。

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

如果通过 --config CLI 选项提供配置,则配置中的通配符模式与当前工作目录相关,而不是与给定配置的基本目录相关。例如,如果存在 --config configs/.eslintrc.json,则配置中的通配符模式与 . 而不是 ./configs 相关。

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

指定要 lint 的目标文件

¥Specifying target files to lint

如果你使用 CLI 指定目录(例如 eslint lib),ESLint 会在目录中搜索目标文件以进行 lint。目标文件是 *.js 或与任何 overrides 条目匹配的文件(但排除任何以 * 结尾的 files 条目)。

¥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 *).

如果你指定了 --ext 命令行选项以及目录,则目标文件只是具有指定文件扩展名的文件,而不管 overrides 条目如何。

¥If you specified the --ext command line option along with directories, the target files are only the files that have specified file extensions regardless of overrides entries.

个人配置文件(已弃用)

¥Personal Configuration Files (deprecated)

⚠️ 此功能已弃用。此功能已在 8.0.0 版本中删除。如果你想继续使用个人配置文件,请使用 --config CLI 选项。有关此决定的更多信息,请参阅 RFC 28RFC 32

¥⚠️ This feature has been deprecated. This feature was removed in the 8.0.0 release. If you want to continue to use personal configuration files, please use the --config CLI option. For more information regarding this decision, please see RFC 28 and RFC 32.

~/ 指的是 你首选操作系统上当前用户的主目录。这里提到的个人配置文件是 ~/.eslintrc.* 文件,目前其处理方式与其他配置文件不同。

¥~/ refers to the home directory of the current user on your preferred operating system. The personal configuration file being referred to here is ~/.eslintrc.* file, which is currently handled differently than other configuration files.

ESLint 如何查找个人配置文件?

¥How does ESLint find personal configuration files?

如果 eslint 在项目中找不到任何配置文件,eslint 将加载 ~/.eslintrc.* 文件。

¥If eslint could not find any configuration file in the project, eslint loads ~/.eslintrc.* file.

如果 eslint 可以在项目中找到配置文件,则 eslint 会忽略 ~/.eslintrc.* 文件,即使它位于项目目录的祖级目录中。

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

个人配置文件如何表现?

¥How do personal configuration files behave?

~/.eslintrc.* 文件的行为与常规配置文件类似,但有一些例外:

¥~/.eslintrc.* files behave similarly to regular configuration files, with some exceptions:

~/.eslintrc.* 文件从 ~/node_modules/ 加载可共享的配置和自定义解析器 - 类似于 require() - 在用户的主目录中。请注意,它不会加载全局安装的软件包。

¥~/.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.

~/.eslintrc.* 文件默认从 $CWD/node_modules 加载插件,以便唯一地标识插件。如果你想使用带有 ~/.eslintrc.* 文件的插件,则必须在每个项目本地安装插件。或者,你可以使用 --resolve-plugins-relative-to CLI 选项 更改 ESLint 加载插件的位置。

¥~/.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 to change the location from which ESLint loads plugins.