配置规则

规则是 ESLint 的核心构建块。规则验证您的代码是否满足特定期望,以及如果不满足该期望该怎么办。规则还可以包含特定于该规则的其他配置选项。

ESLint 自带大量的 内置规则,你可以通过插件添加更多的规则。您可以使用配置注释或配置文件修改您的项目使用的规则。

规则严重性

要更改规则的严重性,请将规则 ID 设置为以下值之一:

  • "off"0 - 关闭规则
  • "warn"1 - 打开规则作为警告(不影响退出代码)
  • "error"2 - 打开规则作为错误(触发时退出代码为 1)

规则通常设置为 "error",以在持续集成测试、预提交检查和拉取请求合并期间强制遵守规则,因为这样做会导致 ESLint 以非零退出代码退出。

VcMpe2THfWIPTo6h2EGEEQKN0BWzaAZ0Q20pW0Y73zdfLSALmbuyXj/Qz2/flTzYgzPsUXUBRxSLE+ci+i7211gbOEldaGAPa144N+bxsojztkYGWh1sxXaZdjhCx0v5p8XNOKyLbazXP2BfDdFsuABNz2uwoSFIhY+0+e3meixVEd5HU1ueW83Dxq/WTN+AcF5653Hrz1CH5f8lkFJV5IDU7zuS2psoyX5oXeYDrvI8fHevEvWuDaWAqvrkrHz6a9gj8KMpMI1c4uXhgiraWDe9XSDfpzx5ph8Lgfn3CMYt+YczQR0mXabjQkD9ZWj3XFF3Mnpfj4hwE8HwAChhvkAMd12prVhQmIjxuOP3K2XZYZ6ebNyMY+RfgFcObl9/66djWiJWE9LsPmToOyPMQapcXkHK2hzR/9oANVNt1/PlI5qjT3RMDGZu06EwbuBY+IxkV40eGTKKIbsCRTrRdugk4bGZOuAkOHWAZERHuEDuHJPO4V3VHuvyV9+PRBu/LqUVm6FkAijkJnzBOOmEJEyzYzFxO6PO3qG6tcWAwof2AN/p19UTxTJng8pg1kKM

使用配置注释

T7p3K0JgklWQA6SMaZoWyYjvSV2Q0VBQWBklJ0eW5CSK0OI6eBFMty6XdRLdopEuudei7PE6P/LM4WTxMcG83hG3Xt4g0yjOY54CdM3s+vYpXga5aBkTPa9v4apWpHy/

/* eslint eqeqeq: "off", curly: "error" */

uKd6UuSfYSGM92EfEt+puvLZif1NW150IdzepC2Vtr141NDVDku/Kiiv89hByAhlJRqK3LZLT3DemymuB4slaoULB9WDWtUE2IaVDxQ8klW9mGXiNMqZxz2pHpKZb2cuhpHD1HI6D/QZ4G1U7xjnj/BEwwSST4med/t1qhTqaNNk9VwqOHmOFsPkd8JYkb9uWi1/gY9C+zW5xglD2efQdl9sA17XY4C8AseS11JXKMroIxsF//6UW2j+SeevcYYwgaQElTv1OyyKifS6gLQiipdqeWzxXEFy4HHQL1yTULEryZNGWQxGcb3HBZJ29SKcZ2sTYbwoBvS2xyC2cVi3ag==

/* eslint eqeqeq: 0, curly: 2 */

5D9JC/zG9pfv8pV7UXYgz9h3HHlb7JyfkUh9tMvPNHYv/v2G+BOkuG4rYNvSCMcfRiRSg3bjm6xwSHvU/wXV8Jq4AxkiEoVkYCfCvwULXOAUMb9GouBVuuWkTjg+u8GdjGHWzQoJa9foxp8S1LW/04l1kqxJzacMcdwrh6VzcT2EzoSCNdNuJahbuKnQkkpl0I2pwszqT1tMHGbxKALlfT8nzA4Jsq0FEdAIeMeijdE=

nda7xzw4XdbcAdStYSunGOLnHRdzR23nREwTtb1+IuVtmp7DaBzv3GgZN0jTzxPNXBcFO3itLtjNVNZj9T2b2HLeZFazd94jXiM7xCba8JmMkVLIVWhvHpTWVHd5DpQ/

/* eslint quotes: ["error", "double"], curly: 2 */

SnlovrIiLqcM5byyVtOfEKGqtbYnvhOpvQ2Rz0bNy2YpVcRUORqAHuqSse+IN2yw6bLSAJl6DI9y5XFBdK6gtbeGDh2PzDkXR7jYab7hOhXUoYy4B7GDSd3A04bmtaMBozdQejjAKCRS7aVg1Z/MwYzSGEPFQ73gogleMNFjo5ALt9XeJYv15QQTnOclv1LzQFX5sc85G6NsZZwtSXZycg8jVAWsZxVi6P1wKT0OiwiCQunmCiObES5e4DSe3tT9

配置注释描述

/NwSMN1h0qzvd0V5oa4SH3AO+Gd/RWnlXrPXnMByrPJxxMA4lEwFXEajrsSzLfMXSzlv6+96TNqiFYP/b+qHETQrPrYgLATA+G7AkPBB1qsI5b/7MyYF76quIR1aZeLotYAKcTY7EZPD2bIHk2kuuGSou5qyv4QPreETlHHa13J1FW7eHvWP9IL2qaMrPPnw4W2MogBt+xq27XVM8s2NJxiucvBLL1aDMQmyM+vZgnE=

/* eslint eqeqeq: "off", curly: "error" -- Here's a description about why this configuration is necessary. */
/* eslint eqeqeq: "off", curly: "error"
    --------
    Here's a description about why this configuration is necessary. */
/* eslint eqeqeq: "off", curly: "error"
 * --------
 * This will not work due to the line above starting with a '*' character.
 */

使用配置文件

dvSF9U51Hom/MBOHSickBgnlc/rduZoJdfquxptW4TisLMTJ8xhU3EoEkv3zXUk/Zz6jIF/FKh4GuTiV7lQ6Tnt/2txDP30LhflPyv3xcHWyovYtP96dh48ZsHrFdsoYN2IFVw6dks4xqhNW7UyPBqQJgT2tNws7ZNKgmHUj4fScW04rPYPRbkOY/AtAwUt7

{
    "rules": {
        "eqeqeq": "off",
        "curly": "error",
        "quotes": ["error", "double"]
    }
}

lObsq6aAiA8MkzIPYrFeAw==

---
rules:
  eqeqeq: off
  curly: error
  quotes:
    - error
    - double

来自插件的规则

DffyoQ2hWpCEJWlEy1Dtp2XwZ+itOYgnpYhAjFLeRQ0G6lmT5yObINqeAz4jHK4GR56ziVJg/oOoHUtqQOVdgDXWZKA/oBr+Ml5fvjKyTmmTX242X+jM3jZtyPRVsNVZUGh2zg3BafFlh2jbrEyDeg==

HSCgdjey2XDclPo7p3c8/4WVary9nmESwQ8ry3/sM6Y=

{
    "plugins": [
        "plugin1"
    ],
    "rules": {
        "eqeqeq": "off",
        "curly": "error",
        "quotes": ["error", "double"],
        "plugin1/rule1": "error"
    }
}

lObsq6aAiA8MkzIPYrFeAw==

---
plugins:
  - plugin1
rules:
  eqeqeq: 0
  curly: error
  quotes:
    - error
    - "double"
  plugin1/rule1: error

UEXi7ASXHFGsw0aRH+mA0ykqn56HJJX8hUbdBd64QxuSMTFPjR9qjSK1sUp8OVInaGYKSXh3Nhj50o5NJjEIgHqqv9atZc0CdrJnhR+6WT26g75qhobYOdGu0nzU9WBs6oqTZZuQgrrKDwVGoMdoPpEh52z526rx+FpAIoYGnxx8O7hruvy6J3Xgq3Zidm2BGVZavcB1VWeFqBbgI8EBLRkB2RxbvozzOUV0yem2KtC2Kdnoy955OeEwiWyONgQv

9gZapPHb3ohqUFg8pHBEeVoufqs6AChQe/5Q8tjQ8RtRpaHx7YAk8A9yj3fxSB8W7syD8JDH3UTnPDx8W0ckqw==

/* eslint "plugin1/rule1": "error" */

4y/020zCKZw9AYAGpZ/mLZfjOp47CLoHC8xiTC0CZCn6o9/Eqsyw3SniecqI9e3jzqth47SklpwuqChoycE7nBjlkoKQhxM+NBS6orN5DqmFAhjwMn0+754cGkfvldJchEBCPBsbNTs/4+QV9g8FDEoFNsN7zxD9iJuQthmq1nw+P7fE9YD65QTskpSA04FXYFI/5JJt0j0lVlPzd1GRBO2+TdPxUIMx7S6oi0oGVZM=

禁用规则

使用配置注释

TLcmCI8MvnpDh8wzowwT123O2+nmdHz1w/M4xLm55g38UGWorVN4X3B54OnT8vkVmyctRaZSOsrIiBvF5T0IZ+yHsDxLn9KBeInUxMLS0BwuC0c2qhMA1GvyDNsICuBk

/* eslint-disable */

alert('foo');

/* eslint-enable */

/TLaSYPZl8Cjv+KFeMweCFGFp1cxJVX1ieqkgJXDsIDVu4dv+l3k2qN3Q6F0xToGk43lOVLUUu9xyoTNLpN44A==

/* eslint-disable no-alert, no-console */

alert('foo');
console.log('bar');

/* eslint-enable no-alert, no-console */

4y/020zCKZw9AYAGpZ/mLVf8yTPJME+sdHZ7JxYpfMhekrZd0pKone9ZvTw9hKyFQzK5ceIVBjPgpQeNtdP9CrOXQ7bpHWGbub8pL9eXlqFvFwzRCdReH11pz047bVw6pPus90HeuoOwVdTAGozb3EhLU6KNhl1pZU2TjagoAm0DO1lBqPr4bHo+wW8kTLMG

FwQU7WMBTfXIz4sZa3zOb4CdUf4+L76/9ytRbbbUNOtqsHByZbtna5wAi8xBCgYUFavyPy0GPi74GIef2eFkRYDDE9+wngVXA7Qsxx+ZMwHTboGa2waHftsBQMySP5r+dJjeMJrBt60vaXwog0vStDllz03UrndBy+gpCZyqZ2s=

/* eslint-disable */

alert('foo');

/TLaSYPZl8Cjv+KFeMweCH50merI1C9VYGl0AtAWKTa6dVhBHLtdcmGGWgu+90/8S/e69yTFDtftykNVC0Nsdw==

/* eslint-disable no-alert */

alert('foo');

XEZJLk9k1LyD5K70a453mkcgoYotdagR2+1ysGJUpY0Ot/EIEByBCeT+xP29fghc0dm+jYlg+ih2RmZSmdEDakpIf/7HZAX4X0k4yFNEzZw=

/* eslint no-alert: "off" */

alert('foo');

JN32/00iEYRA6g9Royzrsf7shwSUpHCNfhp1WKOgEIT0aDgjOtzDOzBHyqu06bhIB9n1EOKmPYniXufpkzPnWwFSdTWqVyFGDZPQ1F2FA3OaMlXHTGwhbnCty7WJFAek

alert('foo'); // eslint-disable-line

// eslint-disable-next-line
alert('foo');

/* eslint-disable-next-line */
alert('foo');

alert('foo'); /* eslint-disable-line */

JN32/00iEYRA6g9RoyzrsSX3QAudIh3HZTLBz2Mlwx2Dd8Tq8ratPzUFhatp14oh

alert('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert
alert('foo');

alert('foo'); /* eslint-disable-line no-alert */

/* eslint-disable-next-line no-alert */
alert('foo');

JN32/00iEYRA6g9RoyzrsWtEPs+tv/IA5VneogMmzQpnQxbf83AMcFEJtHYSijv7

alert('foo'); // eslint-disable-line no-alert, quotes, semi

// eslint-disable-next-line no-alert, quotes, semi
alert('foo');

alert('foo'); /* eslint-disable-line no-alert, quotes, semi */

/* eslint-disable-next-line no-alert, quotes, semi */
alert('foo');

/* eslint-disable-next-line
  no-alert,
  quotes,
  semi
*/
alert('foo');

J6Mm7ZU+iEuF1LE2eAqHbUH+YGKZid98tFD7D1Qdsk32SjANL301b+jGc35xRBde4WvirNfFSbo4vBI2q12hkpx6Zycnc2SLOY+Ul76Vhcz7offLQwDrrrhcDdD8/GNfcJ/GO5kqcPZvhbHY3Iv0wN7u0UspIa6IorvPsV3FPpDdlCXpFvooMXisZ81wWiUX74AP8Z1aWJoTnstZzth804ajmtGd+6JuSJXdfZj7lyqhtDWx8sT9U6FD5nGTsn3Tym0Epcs3MM5szjSzeKlZ71QnWoO84uoCmXyheCvjsrzAd1JBi14ESWbApNxAG2rKdt9A3oovHgrQ2hj07gZQvc/QGJq5YaJ6JHCyCodthYg=

foo(); // eslint-disable-line example/rule-name
foo(); /* eslint-disable-line example/rule-name */

4y/020zCKZw9AYAGpZ/mLYoxvia+aram+cySxW0En8qas9FSFqo4iaKjcvA5fH6Q/ZdJs4+bu8niGZ7eKv51LfnJxDk1DzOrJUCyAM6w7pYtqkNm4GsIz4DVoFOkD0mHoyS6vX5hMi0p/uX0e3NbzLIxmn6vlFj9L7CQO9QPI8GZpW+ybQL8w8XUHq43RdG2yHJtRcJR+3wFUcpisu9b0yiLamTXn/By4vQSgGo9VTJdWW+ESeqbcL2PDa9nX/UL+ifpI9NYnEa1anypadsEndlCHnx66WVhtYdtoIpO39DE9dj0DH3LedFbIAnQwC+B

注释描述

/NwSMN1h0qzvd0V5oa4SHzWFIzYbIiiFyp/9BkMRjFF9d/OGGjRSpZrYhSjRpYXGWYfi0lcbiK9Piz0gz6+LqZer0Ulgb1zsPbT361nCUBKBCekmCFW104Uel7TRW69Su0BlF+qcLEQCzWq3kShp2a1TwPMU0ey6TDf9inruQvPjRtHCZUv5lMfVwl4CqpqpoHyCVPqliEVXOliC3q38Kfh6ibe0CLrni8XooQb7KMwloESIv8+U1yV+Cb5imlf/s13LuhoMdAQl8Xrf7nWeAA==

// eslint-disable-next-line no-console -- Here's a description about why this configuration is necessary.
console.log('hello');

/* eslint-disable-next-line no-console --
 * Here's a very long description about why this configuration is necessary
 * along with some additional information
**/
console.log('hello');

使用配置文件

eRKje4fC5Rw6MiqxufTu3sWHBt40hCouyViXnQRSdrRHI0zdmjGCx5a274XdZf8lgvqFB717fLsJSqR9YAKbRkKFvS23DYnWmci5+10JQJgR0r0ICjX+HdOP8ucrjdlA8FhHz3xWJbxSyqNps1SAPZMjybheKjzrJDX+6Liqzt8=

{
  "rules": {...},
  "overrides": [
    {
      "files": ["*-test.js","*.spec.js"],
      "rules": {
        "no-unused-expressions": "off"
      }
    }
  ]
}

禁用内联注释

XdNleA8l3vB2yQ7xYt0Rr5Ci8etwmWqinmyJwkpe4r35qb/pH4wIAKn0E8TXiM/pEnF3WsMnmVLj4N2ZC86hr8RdYWXf+5ZEHO8L3AxreJsOihozazopFzzjMupp+Z5GUNOyYd4lOID1VTsIJqN+5A==

{
  "rules": {...},
  "noInlineConfig": true
}

yJoe94pDfpfyhOmdWud+njcmosBpAo/iczxgrOaqw7mujIH72eHeSTBMDONJ5WpQ7mlSa0gDp7hBG8rnDdoOddTbtN/b3XGMyK8+DukLubZW1Sxp2KOjEpdD80OKlwq77nQsC3k+kpp/j4Hh6oAcnkSaPksOmXlQVbFGEnTOrKaG79WwhOX6QdkcNLrp1CNLgFPe9UvVQhutOdOsjlWOYQ==

报告未使用的 eslint-disable 注释

99AikE7kVdqR5XW9NgC36423UZ/6kvfxA5XcvrHPYuPm+yb4hOPkLirtiYjN4MjRcMcjsNaXdvU0fIrZutqzFhn/7Tm9VDjrlWTncOlCiEDPJLE6ZOF3jAWBur4H93L1t/6ojZpjsTvzO8RkvwEhLt/Sgy/1o/wMFtXVrGNqEG83JkFc8rNlsEQ/TIqtPzy2

{
  "rules": {...},
  "reportUnusedDisableDirectives": true
}

MfGvFMg8vBWub4cHUPEHEENMA75LhXw30Ki7SMG3v4XHiXJNmE4L3Kpn0S5tKdtwswP601zByit/E4fptJ3GfVYzI1kngQhFEWm3cdOC0CA/zWtM0mr3S7ICO95jgCNao8zkeWMBXJZ3SFfV+xZ0Uwu/XUqNUF3zFuQYOkxx4bq/7G1O6cYPTxGB1H2ldi/Z4otddF8aEPx1Xh1iQQsG+JXZfXGsMXy+TCawqJNSALIvp53pG5ZiZEZLAl6/egjRbDYrxmySFbF//hHuTQeE1Q==