规则

配置规则

ESLint 内置了大量规则,您可以通过插件添加更多规则。您可以使用配置注释或配置文件来修改项目使用的规则。要更改规则设置,您必须将规则 ID 设置为以下值之一:

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

使用配置注释

要使用配置注释在文件内配置规则,请使用以下格式的注释:

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

在此示例中,eqeqeq 被关闭,curly 被打开作为错误。您还可以对规则严重性使用数字等价物:

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

此示例与上一个示例相同,只是它使用数字代码而不是字符串值。eqeqeq 规则关闭,curly 规则设置为错误。

mU0YwgxuWk81ufodJNoSropA8B5UnBbsiD4rpa9JPvZFDj+iGHP/zA0OODw9hAuaSbeANayFEfsCWU/VrcZJwE7YtaqKm+PWsU41w9YCG3GndAoRubIfMh7sYXkqr/2r

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

9sPLyvmw+kaKaxdQul88Khom7Aq8XuM1Ld3RBV2w4hV5HsARqeaiN+1eTH0nDh5X0qgfEVuAwiv0OnQVxC7FujMpVFPuv1mji8jHdVGLrQyJUuQQxRQG1LukUUvG3OuK7TdnY0yGfag8iLEjhlP3bDPgijRxOIbH+Cr1GFfc+bHTRWnWs116Ifnbqfwk87QdGui51PtCSYfH8FK12rwrAzyOvREeUkknQBJmspi1hJIIfntkah7nqH4H+nBoLE3/

JzIsrUoSRe9YV2o8tPpS5OTjxQLAZTf+pRaZEbaoXf7s/UuuR0bTMueepUZWurvw9iGBFrraatApSsEBpe62wZBwYwmFrFzQxl0SypErqhYlMg99Cxh817q//VfdzhuYm8++rM5fNK/gnELS8btbu3dkhNIxNfQKtwMTCQPJkiKkemzdi/NcwzGOl3gHDeDRSPFNx084QTTo39r/HAruGGkS1GyAdiRH9nDSOp+RzIY=

/* 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.
 */

使用配置文件

kX9MuCNvSSYS15EKxFYkKJxBdyKnk2LIK4vsolI89UVhQxOOEo3W2A6nR3uFJf3rmcsFiVHm3j3fgO6Q1zyZN3BcInnsfxt9jK1YRAzyfmprPN9BTWSWTQaLX7WBRiUvimH1W0nsat2ZZFvwRTqrpkh8MdS67j5XEHLsoYUCgX0OHEWv55QBaIUdroj0VK6P

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

FjjaowfCh7np20qUoaJU0A==

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

FeNmUPZZIf+KPhhOOGYNfeV6AUGpDigMOOHUgQv3zPDNXIqVsRNxvtIEiEwKvev011kwINyxmiFJBrSZ4SSsAvWBPRB8yp/trHQV59t4/iCsl2pAXRV/LAhxP3qeoSkSunaVYLZvfK++jg3f0RJd/kwaHUbABlndIbOuW1huLp4=

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

FjjaowfCh7np20qUoaJU0A==

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

eWqveRieBgIyTcyFlBqwGoyJHZK/RHF1VYFWVaCypBPFyl6vzzHDmfOm4NG0jK428O4ijWlY+eIbeZZ38Gai8Kc8aQAGQ5Z2q5v1zxZnaj1OmngVkTwLTb5pQo2humOMLBzQuPZimk6zPTt7f9AkxmhicVxEDI6KsPb2pKlMfpjNzOHFaz+RZQd/p8wjinqqc3a4w/nyRA8nPGoEsThixCHxu8w+e9Dk4tfGajzmivo=

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

FNf50nEebOwvCjc4ZKS5CbXdF7uvEZb6yjU2Kt2kOnsQsU/fBXKCZNApBo+OFR4qe8Wy3S6QroF31MpaM77v9Tg37Tt7LXZYiOguSXedWS6RPN/Kd3dHLTCfv/f17YFuQx7bknHdU94ln+Kdr8i1gB3Yugv/YV13dUUBILfSD67GOCQS4+/8KnM3Upnq2cZXBhsnQTuibH1JNKQh8rSN/lwwMNG1MLgGcMva91uV0zY=

禁用规则

使用配置注释

0FszLja+X15esgViHS4YVbPawDTwPbJBK1I337oFg9AAo7Rq+XCDP/HPyIjlk726+pLhMavYXY0YBpCyFbY5UUH1S0yZ5yO9Kv8qeJUjr6I=

/* eslint-disable */

alert('foo');

/* eslint-enable */

+qsFes/QNxpYvErE8ZbtVm6cbfFJ4Yb+j+M9oxu+MJr5xE1M5Hm9aLFShHi/nH2KZKiAwWzr93P7iRYd/3tEXg==

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

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

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

FNf50nEebOwvCjc4ZKS5CUhjVQJbugr/4ovPyvXT6BPoGU3OGa+tQg+2xUuaZrv4nHmA1LHWjVtqhW0ud0iOLEr8m29TF8r+rcBXY77gyCrHNp3fbtPPiGd25k0Y2E5WAKggm3ibTJ7A+3tISAf2dGRfPYm1AHPyKY3EGlLPgKORePfgGuK2fMNPHfTPl5Lb

WBueO6aCSE2MKaLHieHGDgpeRJKmE+0on8MWuezJvkvwOIj8agYUNm/81gVn2T5KRX7VVBBTolBXmwYnhuj+8VUkYhxJi2s/UmBa/wq62VgZn5PJ6km+hx38Cghm/ivo2nBUURw761X+4nlR1u61H5bcYrbB+vMzqljWpVciVLE=

/* eslint-disable */

alert('foo');

+qsFes/QNxpYvErE8ZbtVjPOINULXHkCi3K+gqbajxbd4fkptN52w/F6kpST8MUCO8b9jHGi6ksc9lOysucooA==

/* eslint-disable no-alert */

alert('foo');

2ZhN9rh0JLSQpsmQQVmF05QmdISIWCCzcSsm6aPjuhJazABtTpZRQSOiaPjMlj+zKMWgHaJsRepWYqHOdri3BmTdI1cs3KrE2ZXb00/G+QQ=

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

alert('foo');

qagYqsAMDrjODv5ObU8RCbCu2R+UpM1f7bsE3uYwgV2VxZcMsCnYTXL9klg9ARINijnpK2DEZdDZc+LN96xnrWPjiVT2CvyusbohHUsyl5lCZVHnkvPD+w98iwcxQCzK

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

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

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

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

qagYqsAMDrjODv5ObU8RCYfiFeemegB3RSvUftRMa7y/dnSeGkM5CAGD4e/lX8Nl

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');

qagYqsAMDrjODv5ObU8RCcP9P1CyqEsD4KZ4S9uZFbN96tvObCFswRS6zkpr4KZa

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');

x2YrhdWLmrH0cfMMtk+x47DdZrqvgBq4AnHTDL6yTAp2jrvh0m/MrQmOJPdNQIJBYPs+xSHzdrFRuoouw5cSXD41ACrtuOLkYxhetqqsRqWdPDKLykNoFiQ7KEYxbcDbkZeL5BwTiSIoWgdalSRKgBmxOr2XchH9FOYq+2x6HBPe8tnldhtF8nqDVv+ZmbdpVUzy7g8hzX/Qy2aWl08Bs/N42dbQez38FNKeFkfqxKpNJxU2QHCQu/KQgtq+52fZklnx6gt3tw9518FNtvZYXhmtkcJc+w7tRMIb9A613G38bW3pYp9nvvGPpnz0frZEn+l7/rDdKK8uTRDk3Zu5wdHllsm4djymh9eqL42JepY=

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

JzIsrUoSRe9YV2o8tPpS5OTjxQLAZTf+pRaZEbaoXf7s/UuuR0bTMueepUZWurvw9iGBFrraatApSsEBpe62wS+RwfYD0GEpHigXX7FB4L9RHQo38oUiUlf/NcwOZZ0Lj+kQEthtWDVb5MPT8Qy+aeEbNV3pwMxbUKTGg4l57thCY6Oh+8Wre33tKYQ0E4nce4/MAORIik746ZVq/a2QmEqc1Z+aGxvtDqGXJlOMk5E=

// 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');

FNf50nEebOwvCjc4ZKS5CUk9ZAZbdjS6v0uBn4eVUVVpLiwbv1M6QLgoYTAzt9uRvKUswvyN1ARz3O4xfhIeGqdLf9R61T0a0YhFJtLM14BjbSTvwcWRxCFMBweP/Qx9yIDDxLVAOyGdsG94aVCweFlgQEURQObB2iglU1eJi9JwjXNX+Yyu2TzGyZy6SHewxKaCj3ePCdcPpFPShrvScm99yuTlkXLvIsvYMQ/VP7fmiY6Bgg86Be1Q0aDqBwT3wEKHYtX+iHmiY/BPOZlm1JglKf7i5ocjK4+Hemp1eThHEbk9TxXqcvBruPFY2QT3

使用配置文件

1PlSbrclE2GIAsKFw/TuEteoUtUHy/p/U4QZcF6VgSSrp0lmTebAq6xE70h6jLjcpWnfKUlUX8poIb4Z51Ctu9kepv10fkIf5WhC9r6jBQbBXjNOIBCQhzeF1frvO9/rReDuHmV+qsDlsvPSvgixIc2WmbnfL0Cmtahfy4/8JFM=

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

禁用内联注释

lhq2ZSvynHWS/bzCNoWnk2LEDmatiSG1KN2KAf3mubpOqfMhj5WQITMo/fLohasfzeEKhG6vV2Oiv5MwCfRA0bzCBr3T2h0pDK6qVtDjlWQKgLd1kqdu7oDEKo3y2JuU

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

wKRXbyOP+BMT0otATdnOC8MBF9bPdwsmgVylC+xuDPca3cL4CUt9MQNpPiux65oUy6BBp844u5BN00QPOVuzKaLKqGGpXb0+g5hQJEp5D04PBQVsEYm/L4yb51IAYwSeeZpZCHwxL3uYsGIghRY7xw==

报告未使用的 eslint-disable 注释

88rsFGHAzG26Q1UmMFVn3dtVtP/87LeD1tfCnHCKN+PXAI8nEJfhYGHJUj6y6kFkBDdoB1OmJ9LAOmPNPLFdrXgo89UHcXE/oiUy3bPVt0C/KCYIctaTU5CDXyPvF4KowS+n1KceQ/N48L6iUYjdhZAyiu+x1c6kZainwl/N//n61ZI6ZTanhxyI+0xMj2AO

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

wKRXbyOP+BMT0otATdnOC4AqHZAb0r2G1z09hf2rFBJwijTErwyUW3Lc11h7gCp2ZkqsqLhbYYXyrv7lfF0S8W8wCS+k4C14acM7tcJ6M3hhRtnsu40hIccYG6SCUcZFDEAJT0JJasFrrDSYpkSpMSpNV6kBRcxVY8RL9xEgEtbmlXa4wqRr2vmhNGo0Rs+5iba0DpC2FXM505emVAP98rNcbu45nzw/CiNdVgi9zP2BX0JT8tJP18kOJAyPISUBFunMj3N429hKxZmkD0K/GQ==