Configuration Files

Configuration File Formats

指定配置文件使用顺序:

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

    Using Configuration Files

  7. 配置文件root属性:如果不为true,则eslint 会不限于此文件的规则并逐级向上查找配置文件,直到顶级目录。

  8. 注释和js注释一致

    Adding Shared Settings

    settings 字段 暂时不解

    Cascading and Hierarchy

    级联和层次结构

主要结合root字段以及多个配置文件规则优先顺序

Extending Configuration Files

扩展配置文件

extends 字段

  1. // .eslintrc.js
  2. export default {
  3. // extends 有以下两种写法 字符串和数组
  4. // "extends": "eslint:recommended",
  5. "extends": ["airbnb"]
  6. // airbnb 是eslint-config-airbnb的简写,可以省略前缀eslint-config-
  7. };

plugins 字段

  1. // .eslintrc.js
  2. export default {
  3. "plugins": [
  4. "react"
  5. // react 是eslint-plugin-react的缩写,可以省略前缀eslint-plugin-
  6. ],
  7. // 继承也可以继承插件的规则 react/recommended 为eslint-plugin-recommended的缩写
  8. "extends": [
  9. "eslint:recommended",
  10. "plugin:react/recommended"
  11. ],
  12. "rules": {
  13. "react/no-set-state": "off"
  14. }
  15. }

Configuration Based on Glob Patterns

基于全局模式的配置

overrides 字段
当同一目录但是不同文件夹下的规则不同时可使用此规则

  1. {
  2. "rules": {
  3. "quotes": ["error", "double"]
  4. },
  5. "overrides": [
  6. {
  7. "files": ["bin/*.js", "lib/*.js"],
  8. "excludedFiles": "*.test.js",
  9. "rules": {
  10. "quotes": ["error", "single"]
  11. }
  12. }
  13. ]
  14. }

Personal Configuration Files

已废弃

Language Options

Specifying Environments

指定环境

env

  1. module.exports = {
  2. "env": {
  3. "browser": true,
  4. "es2021": true,
  5. "node": true
  6. }
  7. };

Specifying Globals

指定全局变量

  1. {
  2. "globals": {
  3. "var1": "writable",
  4. "var2": "readonly"
  5. }
  6. }

Specifying Parser Options

指定解析器

  1. {
  2. "parserOptions": {
  3. // 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
  4. "ecmaVersion": "latest", // 默认es5
  5. // sourceType - set to "script" (default) or "module" if your code is in ECMAScript modules.
  6. "sourceType": "module", // 默认script 如果是es6的语法则用module
  7. "ecmaFeatures": { // 附加的语言功能 见下方英文注释
  8. "jsx": true
  9. }
  10. },
  11. }
  12. // ecmaFeatures - an object indicating which additional language features you'd like to use:
  13. // globalReturn - allow return statements in the global scope
  14. // impliedStrict - enable global strict mode (if ecmaVersion is 5 or greater)
  15. // jsx - enable JSX

Rules

Configuring Rules

  • “off”/0 关闭规则
  • “warn”/1
  • “error”/2

    Using configuration comments

    通过文件头部的注释指定规则

  1. /*eslint quotes: ["error", "double"]*/
  2. // 意思是字符串必须是双引号"double", 如果不是 则是error的错误
  3. const a = "122"; // ✔️

Using configuration files

通过配置文件指定规则

  1. {
  2. "rules": {
  3. "quotes": ["error", "double"],
  4. // "quotes": 2
  5. // 如果只有一个数字则说明是用了默认规则
  6. // quotes 的默认规则就是double,则如果不是double则就是2 error
  7. "plugin1/rule1": "error" // 等同于eslint-plugin-plugin1/rule1
  8. }
  9. }

Disabling Rules

Using configuration comments

  1. /* eslint-disable */
  2. /* eslint-disable no-alert */
  3. // eslint-disable-line or /* eslint-disable-line */
  4. // eslint-disable-next-line or /* eslint-disable-next-line */
  5. alert('foo'); // eslint-disable-line no-alert 具体规则

Using configuration files

指定文件的规则

  1. {
  2. "rules": {...},
  3. "overrides": [
  4. {
  5. "files": ["*-test.js","*.spec.js"],
  6. "rules": {
  7. "no-unused-expressions": "off"
  8. }
  9. }
  10. ]
  11. }

Disabling Inline Comments

不允许行内注释

  1. {
  2. "rules": {...},
  3. "noInlineConfig": true
  4. }

不允许文件头部注释

  1. {
  2. "rules": {...},
  3. "reportUnusedDisableDirectives": true
  4. }

Plugins

Specifying Parser

eslint默认使用Espree作为它的解析器,如果使用第三方parser确保其是能正常兼容的。以下是可兼容的三个parser

  • Esprima
  • @babel/eslint-parser - A wrapper around the Babel parser that makes it compatible with ESLint.
  • @typescript-eslint/parser - A parser that converts TypeScript into an ESTree-compatible form so it can be used in ESLint.

    注意在使用第三方parser配置的前提下,仍需要配置parserOptions,以确保可以兼容ecmaVersion的属性默认值es5

Specifying Processor

  1. {
  2. "plugins": ["a-plugin"],
  3. "processor": "a-plugin/a-processor"
  4. }
  5. {
  6. "plugins": ["a-plugin"],
  7. "overrides": [
  8. {
  9. "files": ["*.md"],
  10. "processor": "a-plugin/markdown"
  11. }
  12. ]
  13. }
  14. {
  15. "plugins": ["a-plugin"],
  16. "overrides": [
  17. {
  18. "files": ["*.md"],
  19. "processor": "a-plugin/markdown"
  20. },
  21. {
  22. "files": ["**/*.md/*.js"],
  23. "rules": {
  24. "strict": "off"
  25. }
  26. }
  27. ]
  28. }

Configuring Plugins

  1. {
  2. "plugins": [
  3. "plugin1",
  4. "eslint-plugin-plugin2"
  5. ]
  6. }
  1. {
  2. // ...
  3. "plugins": [
  4. "jquery", // means eslint-plugin-jquery
  5. ]
  6. // ...
  7. }
  1. {
  2. // ...
  3. "plugins": [
  4. "@jquery/jquery", // means @jquery/eslint-plugin-jquery
  5. "@foobar" // means @foobar/eslint-plugin
  6. ]
  7. // ...
  8. }
  1. {
  2. // ...
  3. "plugins": [
  4. "jquery", // eslint-plugin-jquery
  5. "@foo/foo", // @foo/eslint-plugin-foo
  6. "@bar" // @bar/eslint-plugin
  7. ],
  8. "extends": [
  9. "plugin:@foo/foo/recommended",
  10. "plugin:@bar/recommended"
  11. ],
  12. "rules": {
  13. "jquery/a-rule": "error",
  14. "@foo/foo/some-rule": "error",
  15. "@bar/another-rule": "error"
  16. },
  17. "env": {
  18. "jquery/jquery": true,
  19. "@foo/foo/env-foo": true,
  20. "@bar/env-bar": true,
  21. }
  22. // ...
  23. }

Ignoring Code

ignorePatternsin Config Files

ignorePatterns 的配置

  1. {
  2. "ignorePatterns": ["temp.js", "**/vendor/*.js"],
  3. "rules": {
  4. //...
  5. }
  6. }

The.eslintignoreFile

符合node-ignore

  • 通过#注释
  • 路径相对于当前目录
  • Lines preceded by ! are negated patterns that re-include a pattern that was ignored by an earlier pattern.
  • Ignore patterns behave according to the .gitignore specification.

一些隐含的规则

Using eslintIgnore in package.json

Ignored File Warnings