内置规则
eslint 内置规则介绍
modules.exports = {
// 所有规则页面(https://eslint.org/docs/rules)标记为 ✔ 的规则将会默认开启
extends: ['eslint:recommended'],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
/* Rules *代表recommended也就是'error', ()包裹代表可--fix自动修复, options代表有其它选项建议去官网查看用法, 还有这个规则适合什么时候使用的介绍 */
rules: {
/* Possible Problems: These rules relate to possible logic errors in code */
// options Array回调函数需要return https://eslint.org/docs/rules/array-callback-return
'array-callback-return': [
'off',
{
allowImplicit: false, checkForEach: false },
],
// * 派生类的Constructor中调用super()
'constructor-super': 'error',
// * for循环方向错误导致终止条件无法到达
'for-direction': 'error',
// *options getter函数需要return https://eslint.org/docs/rules/getter-return
'getter-return': ['error', {
allowImplicit: false }],
// * 禁止使用async函数作为new Promise()的参数
'no-async-promise-executor': 'error',
// 禁止for循环中使用await
'no-await-in-loop': 'off',
// * 禁止修改class声明的变量
'no-class-assign': 'error',
// * 禁止与-0比较
'no-compare-neg-zero': 'error',
// *options 禁止测试条件中出现赋值操作符 https://eslint.org/docs/rules/no-cond-assign
'no-cond-assign': ['error', 'except-parens'],
// * 禁止修改const声明的变量
'no-const-assign': 'error',
// *options 禁止常量表达式作为测试条件 https://eslint.org/docs/rules/no-constant-condition
'no-constant-condition': ['error', {
checkLoops: true }],
// 禁止在constructor中return一个值
'no-constructor-return': 'off',
// * 禁止在正则表达式中使用控制字符(ASCII:0~31)
'no-control-regex': 'error',
// * 禁止使用debugger
'no-debugger': 'error',
// * 禁止函数定义中出现重复的参数
'no-dupe-args': 'error',
// * 禁止class中定义重复的成员
'no-dupe-class-members': 'error',
// * 禁止else if中使用重复的条件
'no-dupe-else-if': 'error',
// * 禁止对象字面量中使用重复的key
'no-dupe-keys': 'error',
// * 禁止switch出现重复的case
'no-duplicate-case': 'error',
// options 禁止重复import模块 https://eslint.org/docs/rules/no-duplicate-imports
'no-duplicate-imports': ['off', {
includeExports: false }],
// * 禁止在正则表达式中使用空字符集
'no-empty-character-class': 'error',
// * 禁止空的结构模式
'no-empty-pattern': 'error',
// * 禁止catch语句中修改exception参数
'no-ex-assign': 'error',
// *options 禁止case语句落空 https://eslint.org/docs/rules/no-fallthrough
'no-fallthrough': ['error', {
commentPattern: 'falls?\\s?through' }],
// * 禁止对函数声明重新赋值
'no-func-assign': 'error',
// * 禁止直接赋值import导入的变量
'no-import-assign': 'error',
// *options 禁止在嵌套的块作用域中使用var或函数声明 https://eslint.org/docs/rules/no-inner-declarations
'no-inner-declarations': ['error', 'functions'],
// *options 禁止在 RegExp 构造函数中使用无效的正则表达式字符串 https://eslint.org/docs/rules/no-invalid-regexp
'no-invalid-regexp': ['error', {
allowConstructorFlags: [] }],
// *options 禁止不规则的空白(可以配置字符串,模板字符串,注释,正则表达式字面量) https://eslint.org/docs/rules/no-irregular-whitespace
'no-irregular-whitespace': ['error', {
skipStrings: true }],
// * 禁止使用超出浮点数精度的数字字面量
'no-loss-of-precision': 'error',
// * 禁止正则表达式中使用多代码点组成的一个字符
'no-misleading-character-class': 'error',
// * 禁止Symbol构造函数(本来也不支持)
'no-new-symbol': 'error',
// * 禁止某些全局对象被当作函数或构造函数调用(Math,Date,Reflect,Atomics)
'no-obj-calls': 'error',
// 禁止从new Promise()的函数参数中return一个值
'no-promise-executor-return': 'off',
// * 禁止直接在对象实例上调用某些 Object.prototype 方法(call,apply可以)
'no-prototype-builtins': 'error',
// *options 禁止自身赋值 https://eslint.org/docs/rules/no-self-assign
'no-self-assign': ['error', {
props: true }],
// 禁止自身比较
'no-self-compare': 'off',
// * 禁止setter函数return一个值
'no-setter-return': 'error',
// * 禁止稀疏数组
'no-sparse-arrays': 'error',
// 禁止在常规字符串中出现模板字面量占位符语法
'no-template-curly-in-string': 'off',
// * 禁止在派生类构造函数中在super()调用之前使用this/super
'no-this-before-super': 'error',
// *options 禁止使用未声明变量 https://eslint.org/docs/rules/no-undef
'no-undef': ['error', {
typeof: false }],
// * 禁止令人困惑的多行表达式
'no-unexpected-multiline': 'error',
// 禁止一成不变的循环条件
'no-unmodified-loop-condition': 'off',
// * 禁止return, throw, break, continue之后出现的无法执行的语句及子类构造函数没有调用super()
'no-unreachable': 'error',
// options 禁止最多只有一次的循环 https://eslint.org/docs/rules/no-unreachable-loop
'no-unreachable-loop': ['off', {
ignore: [] }],
// * 禁止finally块中直接使用return, throw, break, continue
'no-unsafe-finally': 'error',
// *options 禁止对关系运算符的左操作数使用否定操作符 https://eslint.org/docs/rules/no-unsafe-negation
'no-unsafe-negation': ['error', {
enforceForOrderingRelations: false }],
// *options 禁止在不允许使用undefined的上下文中使用可选链(?.) https://eslint.org/docs/rules/no-unsafe-options-chaining
'no-unsafe-optional-chaining': [
'error',
{
disallowArithmeticOperators: false },
],
// 禁止未使用的私有类成员对象
'no-unused-private-class-members': 'off',
// *options 禁止出现未使用的变量 https://eslint.org/docs/rules/no-unused-vars
'no-unused-vars': [
'error',
{
vars: 'all',
args: 'after-used',
caughtErrors: 'none',
ignoreRestSiblings: false,
},
],
// options 禁止未声明就使用 https://eslint.org/docs/rules/no-use-before-define
'no-use-before-define': [
'off',
{
classes: true, functions: true, variables: true },
],
// * 禁止在正则表达式中使用无用的反向引用
'no-useless-backreference': 'error',
// options 禁止由于 await 或 yield的使用而可能导致出现竞态条件的赋值 https://eslint.org/docs/rules/require-atomic-updates
'require-atomic-updates': ['off', {
allowProperties: false }],
// *options 使用isNaN()检查NaN https://eslint.org/docs/rules/use-isnan
'use-isnan': [
'error',
{
enforceForSwitchCase: true, enforceForIndexOf: false },
],
// *options 强制typeof表达式与有效的字符串字面量比较 https://eslint.org/docs/rules/valid-typeof
'valid-typeof': ['error', {
requireStringLiterals: true }],
/* Suggestions: These rules suggest alternate ways of doing things */
// options 强制getter和setter成对出现在Object和Class中(默认可以只有getter) https://eslint.org/docs/rules/accessor-pairs
'accessor-pairs': [
'off',
{
setWithoutGet: true,
getWithoutSet: false,
enforceForClassMembers: true,
},
],
// (options) 需要在箭头函数体中使用大括号(只有as-needed才可以使用第三个参数)
'arrow-body-style': [
'off',
'as-needed',
{
requireReturnForObjectLiteral: false },
],
// 模拟var像let一样拥有块作用域
'block-scoped-var': 'off',
// options 使用驼峰命名 https://eslint.org/docs/rules/camelcase
camelcase: [
'off',
{
properties: 'always',
ignoreDestructuring: false,
ignoreImports: false,
ignoreGlobals: false,
allow: [],
},
],
// (options) 强制或禁止对注释的第一个字母大写(可以使用block,line单独配置单行多行注释) https://eslint.org/docs/rules/capitalized-comments
'capitalized-comments': [
'off',
'always',
{
ignorePattern: 'jscs|jshint|eslint|istanbul|global|globals|exported',
ignoreInlineComments: false,
ignoreConsecutiveComments: false,
},
],
// options 强制类中的方法使用this, 否则需要将不使用this的实例方法改为静态方法 https://eslint.org/docs/rules/class-methods-use-this
'class-methods-use-this': [
'off',