跳到主要内容

JavaScript 规则

Airbnb

Airbnb 是 ESLint 中最流行的规则集之一。由 Airbnb 团队维护,被广泛认可和使用。Airbnb 规则以其严苛著称,它强制要求代码风格、变量命名、函数定义等各个方面的高度一致性,是许多项目的基石和起点。

// .eslintrc.js

module.exports = {
extends: [
'airbnb-base',
],
};
注意
  • 截止至目前,eslint-config-airbnb-base 仍不支持 ESLint 9.x 版本,使用时请注意。使用时请使用 ESLint 8.x 版本,耐心等待 eslint-config-airbnb-base 升级。
  • 由于 eslint-config-airbnb-base 项目的一些管理问题,导致其更新比较缓慢,后续需要持续关注项目情况。

最佳实践

规则名称错误级别配置选项描述
accessor-pairsoff-强制 getter/setter 成对出现
array-callback-returnerror{ allowImplicit: true }强制数组方法的回调函数中有 return 语句
block-scoped-varerror-禁止在块作用域外使用变量
complexityoff20限制代码圈复杂度
class-methods-use-thiserror{ exceptMethods: [] }强制类方法使用 this
consistent-returnerror-要求函数返回类型一致
curlyerrormulti-line强制使用大括号控制流语句
default-caseerror{ commentPattern: '^no default$' }要求 switch 语句必须有 default
default-case-lasterror-要求 default case 放在最后
default-param-lasterror-要求默认参数放在最后
dot-notationerror{ allowKeywords: true }强制使用点号访问属性
dot-locationerrorproperty强制点号与属性同一行
eqeqeqerroralways, { null: 'ignore' }强制使用 ===!==
grouped-accessor-pairserror-要求 getter/setter 成组出现
guard-for-inerror-要求 for-in 循环包含 if 语句
max-classes-per-fileerror1限制每个文件的类数量
no-alertwarn-禁止使用 alert
no-callererror-禁止使用 arguments.caller/callee
no-case-declarationserror-禁止 case 语句中声明变量
no-constructor-returnerror-禁止构造函数返回值
no-div-regexoff-禁止在正则表达式中使用除法符号
no-else-returnerror{ allowElseIf: false }禁止在 else 前有 return
no-empty-functionerror{ allow: [...] }禁止空函数
no-empty-patternerror-禁止空解构模式
no-empty-static-blockoff-禁止空静态块
no-eq-nulloff-禁止与 null 比较
no-evalerror-禁止使用 eval
no-extend-nativeerror-禁止扩展原生对象
no-extra-binderror-禁止不必要的 bind
no-extra-labelerror-禁止不必要的标签
no-fallthrougherror-禁止 case 穿透
no-floating-decimalerror-禁止浮点小数
no-global-assignerror{ exceptions: [] }禁止覆盖原生对象
no-native-reassignoff-禁止覆盖原生对象 (已弃用)
no-implicit-coercionoff{ boolean: false, ... }禁止隐式类型转换
no-implicit-globalsoff-禁止隐式全局变量
no-implied-evalerror-禁止隐式 eval
no-invalid-thisoff-禁止无效的 this 上下文
no-iteratorerror-禁止使用 __iterator__
no-labelserror{ allowLoop: false, ... }禁止标签语句
no-lone-blockserror-禁止不必要的嵌套块
no-loop-funcerror-禁止循环中创建函数
no-magic-numbersoff{ ignore: [], ... }禁止魔法数字
no-multi-spaceserror{ ignoreEOLComments: false }禁止多个空格
no-multi-strerror-禁止多行字符串
no-newerror-禁止 new 操作符副作用
no-new-funcerror-禁止 new Function
no-new-wrapperserror-禁止 new 包装对象
no-nonoctal-decimal-escapeerror-禁止非八进制十进制转义
no-object-constructoroff-禁止 Object 构造函数
no-octalerror-禁止八进制字面量
no-octal-escapeerror-禁止八进制转义序列
no-param-reassignerror{ props: true, ... }禁止参数重新赋值
no-protoerror-禁止 __proto__
no-redeclareerror-禁止重复声明变量
no-restricted-propertieserror多对象配置限制特定对象属性
no-return-assignerroralways禁止 return 中赋值
no-return-awaiterror-禁止不必要的 return await
no-script-urlerror-禁止 javascript: URL
no-self-assignerror{ props: true }禁止自我赋值
no-self-compareerror-禁止自我比较
no-sequenceserror-禁止逗号操作符
no-throw-literalerror-禁止抛出字面量
no-unmodified-loop-conditionoff-禁止未修改的循环条件
no-unused-expressionserror{ allowShortCircuit: false, ... }禁止未使用的表达式
no-unused-labelserror-禁止未使用的标签
no-useless-calloff-禁止不必要的 .call/.apply
no-useless-catcherror-禁止不必要的 catch
no-useless-concaterror-禁止不必要的字符串连接
no-useless-escapeerror-禁止不必要的转义
no-useless-returnerror-禁止不必要的 return
no-voiderror-禁止 void 操作符
no-warning-commentsoff{ terms: [...], location: 'start' }禁止警告注释
no-witherror-禁止 with 语句
prefer-promise-reject-errorserror{ allowEmptyReject: true }要求 Promise reject 错误对象
prefer-named-capture-groupoff-建议命名捕获组
prefer-object-has-ownoff-建议使用 Object.hasOwn()
prefer-regex-literalserror{ disallowRedundantWrapping: true }建议使用正则字面量
radixerror-要求 parseInt 使用基数
require-awaitoff-要求 async 函数有 await
require-unicode-regexpoff-要求正则表达式使用 u 标志
vars-on-toperror-要求变量声明在作用域顶部
wrap-iifeerroroutside, { functionPrototypeMethods: false }要求立即执行函数使用括号包裹
yodaerror-禁止 Yoda 条件

错误

规则名错误级别配置选项描述
for-directionerror-防止循环条件错误导致无限循环
getter-returnerror{ allowImplicit: true }强制 getter 函数必须有返回值
no-async-promise-executorerror-禁止使用异步函数作为 Promise 执行器
no-await-in-looperror-禁止在循环内使用 await
no-compare-neg-zeroerror-禁止与 -0 进行比较
no-cond-assignerror"always"禁止在条件语句中进行赋值操作
no-consolewarn-警告使用 console 语句
no-constant-binary-expressionoff-关闭检测常量二进制表达式问题
no-constant-conditionwarn-警告在条件中使用常量值
no-control-regexerror-禁止在正则表达式中使用控制字符
no-debuggererror-禁止使用 debugger 语句
no-dupe-argserror-禁止函数参数重复定义
no-dupe-else-iferror-禁止 else if 中有重复条件
no-dupe-keyserror-禁止对象字面量中重复的键
no-duplicate-caseerror-禁止 switch 语句中有重复的 case
no-emptyerror-禁止空块语句
no-empty-character-classerror-禁止正则表达式中出现空字符类
no-ex-assignerror-禁止对 catch 子句中的异常重新赋值
no-extra-boolean-casterror-禁止不必要的布尔类型转换
no-extra-parensoff"all", { conditionalAssign: true, nestedBinaryExpressions: false, returnAssign: false, ignoreJSX: "all", enforceForArrowConditionals: false }关闭检测多余的括号(配置保留)
no-extra-semierror-禁止不必要的分号
no-func-assignerror-禁止对函数声明重新赋值
no-import-assignerror-禁止对导入的绑定进行赋值
no-inner-declarationserror-禁止在嵌套块中声明函数或变量
no-invalid-regexperror-禁止无效的正则表达式
no-irregular-whitespaceerror-禁止不规则的空白字符
no-loss-of-precisionerror-禁止数字精度丢失
no-misleading-character-classerror-禁止正则中可能导致误解的字符类
no-obj-callserror-禁止将全局对象当作函数调用
no-new-native-nonconstructoroff-关闭检测无法作为构造函数的内置对象调用
no-promise-executor-returnerror-禁止在 Promise 执行器中返回值
no-prototype-builtinserror-禁止直接调用对象的原型方法(如 hasOwnProperty
no-regex-spaceserror-禁止正则表达式中出现多个空格
no-setter-returnerror-强制 setter 函数必须有返回值
no-sparse-arrayserror-禁止稀疏数组(如 [1,,2]
no-template-curly-in-stringerror-禁止字符串中出现模板字面量占位符语法
no-unexpected-multilineerror-禁止出现意外的多行表达式
no-unreachableerror-禁止不可达的代码
no-unreachable-looperror{ ignore: [] }禁止无法正常退出的循环
no-unsafe-finallyerror-禁止在 finally 块中使用控制流语句
no-unsafe-negationerror-禁止不安全的取反操作(如 !obj in expr
no-unsafe-optional-chainingerror{ disallowArithmeticOperators: true }禁止不安全的可选链操作
no-unused-private-class-membersoff-关闭检测未使用的私有类成员
no-useless-backreferenceerror-禁止正则表达式中无用的反向引用
no-negated-in-lhsoff-关闭检测 in 操作符的左操作符是否被取反(已弃用)
require-atomic-updatesoff-关闭要求异步操作必须是原子性的
use-isnanerror-必须使用 isNaN() 检查 NaN
valid-jsdocoff-关闭强制有效的 JSDoc 注释
valid-typeoferror{ requireStringLiterals: true }强制 typeof 操作符必须与有效字符串字面量比较

node.js

规则名称错误级别配置选项描述
callback-returnoff-强制在回调函数中使用 return 语句
global-requireerror-强制 require() 在模块顶部调用
handle-callback-erroff-强制回调函数的错误处理
no-buffer-constructorerror-禁用 Buffer() 构造函数(已废弃,建议迁移到 eslint-plugin-node
no-mixed-requiresoff[false]禁止混合常规变量和模块引入
no-new-requireerror-禁止对 require 使用 new
no-path-concaterror-禁止路径字符串拼接
no-process-envoff-禁用 process.env
no-process-exitoff-禁用 process.exit()
no-restricted-modulesoff-禁止使用特定模块(已废弃,建议改用 no-restricted-imports
no-syncoff-禁用同步方法

风格

规则名错误级别配置选项描述
array-bracket-newlineoff'consistent'强制数组括号的换行风格一致
array-element-newlineoff{ multiline: true, minItems: 3 }多行或超过最小项时换行(已关闭)
array-bracket-spacingerror'never'禁止数组括号内添加空格
block-spacingerror'always'强制块内保留空格
brace-styleerror'1tbs', { allowSingleLine: true }大括号风格为 1tbs,允许单行块
camelcaseerror{ properties: 'never', ignoreDestructuring: false }强制驼峰命名(属性名除外)
capitalized-commentsoff'never', { line: {...}, block: {...} }关闭注释首字母大写检查
comma-dangleerror{ arrays: 'always-multiline', ... }多行结构强制拖尾逗号
comma-spacingerror{ before: false, after: true }逗号后必须空格,前无空格
comma-styleerror'last', { exceptions: {...} }逗号置于行尾
computed-property-spacingerror'never'禁止计算属性内空格
consistent-thisoff-关闭强制统一 this 别名
eol-lasterror'always'文件末尾强制换行
function-call-argument-newlineerror'consistent'函数调用参数换行风格一致
func-call-spacingerror'never'禁止函数名与括号间空格
func-name-matchingoff'always', { includeCommonJSModuleExports: false }关闭函数名匹配检查
func-nameswarn-警告未命名函数表达式
func-styleoff'expression'关闭函数声明风格检查
function-paren-newlineerror'multiline-arguments'多行参数时括号换行
id-denylistoff-关闭标识符黑名单检查
id-lengthoff-关闭标识符长度检查
id-matchoff-关闭标识符命名模式检查
implicit-arrow-linebreakerror'beside'箭头函数返回值与箭头同行
indenterror2, { SwitchCase: 1, ... }强制2空格缩进,处理不同语法结构
jsx-quotesoff'prefer-double'关闭JSX引号偏好检查
key-spacingerror{ beforeColon: false, afterColon: true }键后必须空格,键前无空格
keyword-spacingerror{ before: true, after: true, ... }关键字前后保留空格
line-comment-positionoff{ position: 'above' }关闭行注释位置检查
linebreak-styleerror'unix'强制Unix换行符(\n
lines-between-class-memberserror'always', { exceptAfterSingleLine: false }类成员间保留空行
lines-around-commentoff-关闭注释周围空行检查
lines-around-directiveerror{ before: 'always', after: 'always' }指令周围保留空行
logical-assignment-operatorsoff'always', { enforceForIfStatements: true }关闭逻辑赋值操作符检查
max-depthoff4关闭代码块嵌套深度检查
max-lenerror100, 2, { ignoreUrls: true, ... }单行最大长度100字符
max-linesoff{ max: 300, ... }关闭文件最大行数检查
max-lines-per-functionoff{ max: 50, ... }关闭函数最大行数检查
max-nested-callbacksoff-关闭回调嵌套深度检查
max-paramsoff3关闭函数最大参数检查
max-statementsoff10关闭函数最大语句数检查
max-statements-per-lineoff{ max: 1 }关闭单行最大语句数检查
multiline-comment-styleoff'starred-block'关闭多行注释风格检查
multiline-ternaryoff'never'关闭多行三元表达式检查
new-caperror{ newIsCap: true, capIsNew: false, ... }构造函数首字母大写限制
new-parenserror-构造函数调用必须带括号
newline-after-varoff-关闭变量声明后空行检查
newline-before-returnoff-关闭return前空行检查
newline-per-chained-callerror{ ignoreChainWithDepth: 4 }链式调用超过4层需换行
no-array-constructorerror-禁止使用数组构造函数
no-bitwiseerror-禁止位运算符
no-continueerror-禁止使用continue
no-inline-commentsoff-关闭禁止行内注释检查
no-lonely-iferror-禁止单独的if语句
no-mixed-operatorserror{ groups: [...], allowSamePrecedence: false }禁止混合使用不同优先级操作符
no-mixed-spaces-and-tabserror-禁止混用空格和制表符
no-multi-assignerror-禁止连续赋值
no-multiple-empty-lineserror{ max: 1, maxBOF: 0, maxEOF: 0 }最多1个连续空行
no-negated-conditionoff-关闭禁止否定条件检查
no-nested-ternaryerror-禁止嵌套三元表达式
no-new-objecterror-禁止使用new Object
no-pluspluserror-禁止++--
no-restricted-syntaxerrorForInStatement, ForOfStatement, ...禁用特定语法结构(如for-in
no-spaced-funcoff-关闭禁止函数名与括号间空格检查
no-tabserror-禁止使用制表符
no-ternaryoff-关闭禁止三元操作符检查
no-trailing-spaceserror{ skipBlankLines: false, ... }禁止行尾空格
no-underscore-dangleerror{ allow: [], ... }禁止下划线开头或结尾的标识符
no-unneeded-ternaryerror{ defaultAssignment: false }禁止不必要的三元表达式
no-whitespace-before-propertyerror-禁止属性前的空格
nonblock-statement-body-positionerror'beside', { overrides: {} }单行非块语句与声明同行
object-curly-spacingerror'always'对象花括号内保留空格
object-curly-newlineerror{ ObjectExpression: { minProperties: 4, ... } }对象属性超过3项时换行
object-property-newlineerror{ allowAllPropertiesOnSameLine: true }允许对象属性同行
one-varerror'never'禁止合并变量声明
one-var-declaration-per-lineerror'always'每行一个变量声明
operator-assignmenterror'always'强制操作符简写形式
operator-linebreakerror'before', { overrides: { '=': 'none' } }操作符置于行首(等号例外)
padded-blockserror'never', { allowSingleLineBlocks: true }块内不填充空行(单行块允许)
padding-line-between-statementsoff-关闭语句间空行检查
prefer-exponentiation-operatorerror-使用指数操作符替代Math.pow
prefer-object-spreaderror-使用对象展开替代Object.assign
quote-propserror'as-needed', { keywords: false, ... }按需引用对象属性名
quoteserror'single', { avoidEscape: true }使用单引号(允许转义)
require-jsdocoff-关闭JSDoc注释检查
semierror'always'强制分号结尾
semi-spacingerror{ before: false, after: true }分号后空格,前无空格
semi-styleerror'last'分号置于行尾
sort-keysoff'asc', { caseSensitive: false, ... }关闭对象键排序检查
sort-varsoff-关闭变量排序检查
space-before-blockserror-块前必须空格
space-before-function-parenerror{ anonymous: 'always', named: 'never', ... }函数名后无空格,匿名函数后有空格
space-in-parenserror'never'括号内不保留空格
space-infix-opserror-中缀操作符周围保留空格
space-unary-opserror{ words: true, nonwords: false }单词类一元操作符后空格
spaced-commenterror'always', { line: {...}, block: {...} }注释前保留空格
switch-colon-spacingerror{ after: true, before: false }switch case冒号后空格,前无空格
template-tag-spacingerror'never'模板标签后无空格
unicode-bomerror'never'禁止Unicode BOM头
wrap-regexoff-关闭正则表达式括号包裹检查

变量

规则名称错误级别配置选项描述
init-declarationsoff-强制或禁止变量声明时初始化
no-catch-shadowoff-禁止catch子句参数与外部变量同名
no-delete-varerror-禁止使用delete删除变量
no-label-varerror-禁止标签与变量同名
no-restricted-globalserror[ { name: "isFinite", message: "Use Number.isFinite instead" }, { name: "isNaN", message: "Use Number.isNaN instead" }, "addEventListener", "blur", ..., "top" ]禁止使用特定全局变量并提供替代建议
no-shadowerror-禁止变量声明覆盖外部作用域变量
no-shadow-restricted-nameserror-禁止覆盖受限标识符(如undefined
no-undeferror-禁止使用未声明变量
no-undef-initerror-禁止初始化变量为undefined
no-undefinedoff-禁止使用undefined变量
no-unused-varserror{ vars: "all", args: "after-used", ignoreRestSiblings: true }禁止未使用变量,可配置检测范围
no-use-before-defineerror{ functions: true, classes: true, variables: true }禁止在定义前使用变量/函数/类

配置选项说明

  • no-restricted-globals 的完整受限列表包含:
    isFinite/isNaN(带自定义提示)及 addEventListener, blur, close, closed, confirm, defaultStatus, ..., top 等全局变量
  • no-unused-vars 配置表示:检查所有变量、参数从使用位置后开始检测、忽略剩余属性
  • no-use-before-define 配置表示:检查函数/类/变量均需先定义后使用

ECMAScript 6

规则名称错误级别配置选项描述
arrow-body-styleerror"as-needed", { requireReturnForObjectLiteral: false }要求箭头函数体在可能的情况下省略大括号
arrow-parenserror"always"要求箭头函数参数始终使用括号
arrow-spacingerror{ before: true, after: true }强制箭头函数的箭头前后空格一致性
constructor-supererror-禁止在构造函数中在调用 super() 之前使用 this/super
generator-star-spacingerror{ before: false, after: true }强制 generator 函数中星号周围空格的一致性
no-class-assignerror-禁止修改类声明
no-confusing-arrowerror{ allowParens: true }禁止可能与比较操作符混淆的箭头函数语法
no-const-assignerror-禁止修改 const 声明的变量
no-dupe-class-memberserror-禁止类成员中的重复名称
no-duplicate-importsoff-禁止重复模块导入(已禁用)
no-new-symbolerror-禁止使用 new 操作符创建 Symbol 实例
no-restricted-exportserror{ restrictedNamedExports: ["default", "then"] }限制指定的命名导出
no-restricted-importsoff{ paths: [], patterns: [] }限制指定的模块导入(已禁用)
no-this-before-supererror-禁止在构造函数中在 super() 调用前使用 this
no-useless-computed-keyerror-禁止不必要的计算属性键
no-useless-constructorerror-禁止不必要的构造函数
no-useless-renameerror{ ignoreDestructuring: false, ignoreImport: false, ignoreExport: false }禁止不必要的重命名解构
no-varerror-要求使用 let 或 const 代替 var
object-shorthanderror"always", { ignoreConstructors: false, avoidQuotes: true }强制对象字面量简写语法
prefer-arrow-callbackerror{ allowNamedFunctions: false, allowUnboundThis: true }要求回调函数使用箭头函数
prefer-consterror{ destructuring: "any", ignoreReadBeforeAssign: true }要求使用 const 声明不会被重新赋值的变量
prefer-destructuringerror{ VariableDeclarator: { array: false, object: true }, AssignmentExpression: { array: true, object: false } }, { enforceForRenamedProperties: false }强制使用解构赋值
prefer-numeric-literalserror-禁用 parseInt() 而使用二进制、八进制和十六进制字面量
prefer-reflectoff-要求使用 Reflect 方法(已禁用)
prefer-rest-paramserror-要求使用 rest 参数代替 arguments
prefer-spreaderror-要求使用扩展运算符代替 .apply()
prefer-templateerror-要求使用模板字面量代替字符串拼接
require-yielderror-要求 generator 函数内包含 yield 语句
rest-spread-spacingerror"never"强制剩余和扩展运算符周围空格的一致性
sort-importsoff{ ignoreCase: false, ignoreDeclarationSort: false, ignoreMemberSort: false, memberSyntaxSortOrder: ["none", "all", "multiple", "single"] }强制导入声明排序(已禁用)
symbol-descriptionerror-要求 Symbol 描述参数
template-curly-spacingerror-强制模板字符串中花括号内的空格
yield-star-spacingerror"after"强制 yield* 表达式中星号周围空格

模块导入

规则名错误级别配置选项描述
import/no-unresolvederror{ commonjs: true, caseSensitive: true }确保导入的模块路径可解析
import/namederror-验证命名导出是否存在
import/defaultoff-关闭默认导出的验证
import/namespaceoff-关闭命名空间导入验证
import/exporterror-确保文件内所有导出语法有效
import/no-named-as-defaulterror-禁止将命名导出与默认导出混淆
import/no-named-as-default-membererror-禁止通过默认导出访问命名导出
import/no-deprecatedoff-关闭对废弃模块的检查
import/no-extraneous-dependencieserror{ devDependencies: [...], optionalDependencies: false }禁止引入无关依赖
import/no-mutable-exportserror-禁止导出可变变量
import/no-commonjsoff-关闭对 CommonJS 语法的限制
import/no-amderror-禁止 AMD 语法
import/no-nodejs-modulesoff-关闭对 Node.js 内置模块的限制
import/firsterror-确保所有导入语句在模块顶部
import/imports-firstoff-关闭“导入必须置顶”的旧规则
import/no-duplicateserror-禁止重复导入同一模块
import/no-namespaceoff-关闭对命名空间导入的限制
import/extensionserror'ignorePackages', { js: 'never', mjs: 'never', jsx: 'never' }强制文件扩展名规范
import/ordererror{ groups: [['builtin', 'external', 'internal']] }控制导入顺序和分组
import/newline-after-importerror-导入语句后需有空行
import/prefer-default-exporterror-建议使用默认导出
import/no-restricted-pathsoff-关闭对特定路径的限制
import/max-dependenciesoff{ max: 10 }限制单个文件的最大依赖数
import/no-absolute-patherror-禁止使用绝对路径导入
import/no-dynamic-requireerror-禁止动态 require() 语法
import/no-internal-modulesoff{ allow: [] }禁止导入内部模块
import/unambiguousoff-关闭对非 ES 模块的检测
import/no-webpack-loader-syntaxerror-禁止 Webpack 特有的加载语法
import/no-unassigned-importoff-禁止未赋值的导入
import/no-named-defaulterror-禁止命名默认导出别名
import/no-anonymous-default-exportoff{ allowArray: false, ... }禁止匿名默认导出
import/exports-lastoff-确保导出语句在文件末尾
import/group-exportsoff-强制分组导出语句
import/no-default-exportoff-禁止默认导出
import/no-named-exportoff-禁止命名导出
import/no-self-importerror-禁止模块导入自身
import/no-cycleerror{ maxDepth: '∞' }禁止模块循环依赖
import/no-useless-path-segmentserror{ commonjs: true }禁止冗余路径片段
import/dynamic-import-chunknameoff{ webpackChunknameFormat: '[0-9a-zA-Z-_/.]+' }控制动态导入的代码块命名
import/no-relative-parent-importsoff-禁止相对父级目录导入
import/no-unused-modulesoff{ missingExports: true, ... }检测未使用的模块
import/no-import-module-exportserror{ exceptions: [] }禁止混合 importmodule.exports
import/no-relative-packageserror-禁止相对路径导入其他包

严格模式

规则名错误级别配置选项描述
stricterror'never'禁止使用'use strict'指令。当使用 Babel 等转译工具时,它会自动添加'use strict'指令

Unicorn

eslint-plugin-unicorn 提供了许多现代、实用的规则,用于提升代码质量和可读性,例如强制使用新的 JavaScript 语法(如 Array.prototype.flat )、简化代码逻辑等。它帮助开发者编写更“干净”的代码。

// .eslintrc.js

module.exports = {
extends: [
'plugin:unicorn/recommended',
],
};
注意

这是一个实验性功能,请谨慎使用!部分规则和 Airbnb 规则有冲突,目前仍在整理中。如遇规则冲突,请以Airbnb 规则为准。

规则名称错误级别配置选项描述
unicorn/better-regexoff-强制正则表达式更加优化和简洁
unicorn/catch-error-nameerror-强制错误变量在 catch 子句中命名为 error
unicorn/consistent-asserterror-强制使用一致的断言方法(例如 assert.strictEqual 而不是 assert.equal
unicorn/consistent-date-cloneerror-强制使用一致的方法来克隆日期(例如 new Date(date)
unicorn/consistent-destructuringoff-强制使用一致的结构分配方式
unicorn/consistent-empty-array-spreaderror-强制空数组展开时使用一致的方式
unicorn/consistent-existence-index-checkerror-强制使用一致的索引存在性检查(例如 index in array 而不是 array[index] !== undefined
unicorn/consistent-function-scopingerror-强制将没有依赖外部变量的函数提升到更高作用域
unicorn/custom-error-definitionoff-强制自定义错误必须继承自 error
unicorn/empty-brace-spaceserror-强制在花括号内使用一致的空格
unicorn/error-messageerror-强制在 throw 语句中提供错误信息
unicorn/escape-caseerror-强制转义序列使用大写或小写(例如 \n 而不是 \N
unicorn/expiring-todo-commentserror-强制 TODO 注释包含过期日期
unicorn/explicit-length-checkerror-强制显式地检查长度(例如 array.length > 0 而不是 array.length
unicorn/filename-caseerror-强制文件名使用特定的大小写风格(如驼峰、短横线等)
unicorn/import-styleerror-强制使用特定的导入风格(例如只使用默认导入或命名导入)
unicorn/new-for-builtinserror-强制对内置对象使用 new(例如 new Error() 而不是 Error()
unicorn/no-abusive-eslint-disableerror-禁止滥用 eslint-disable 注释
unicorn/no-accessor-recursionerror-禁止在访问器中递归调用自身(会导致栈溢出)
unicorn/no-anonymous-default-exporterror-禁止匿名默认导出
unicorn/no-array-callback-referenceerror-禁止在数组方法中传递回调函数的引用(应使用内联函数)
unicorn/no-array-for-eacherror-禁止使用 Array.prototype.forEach,推荐使用 for...of
unicorn/no-array-method-this-argumenterror-禁止在数组方法中使用 this 参数(应使用箭头函数)
unicorn/no-array-reduceerror-尽量避免使用 Array.prototype.reduce,除非有特定需求
unicorn/no-array-reverseerror-禁止使用 Array.prototype.reverse,除非非常必要
unicorn/no-array-sorterror-禁止使用 Array.prototype.sort 而不带比较函数(对数字排序会出错)
unicorn/no-await-expression-membererror-禁止在 await 表达式后直接使用成员表达式(可能导致意外行为)
unicorn/no-await-in-promise-methodserror-禁止在 Promise 方法(如 thencatch)中使用 await
unicorn/no-console-spaceserror-禁止在 console.log 等方法的参数中添加多余空格
unicorn/no-document-cookieerror-禁止直接使用 document.cookie,应使用封装好的方法
unicorn/no-empty-fileerror-禁止空文件
unicorn/no-for-looperror-禁止使用 for 循环,推荐使用高阶数组方法或 for...of
unicorn/no-hex-escapeerror-禁止在字符串中使用十六进制转义序列(应使用 Unicode 转义)
unicorn/no-instanceof-builtinserror-禁止对内置类型(如 Array, error)使用 instanceof(在不同 realm 中可能失效)
unicorn/no-invalid-fetch-optionserror-禁止在 fetch 中使用无效的选项(例如 bodyGET 一起用)
unicorn/no-invalid-remove-event-listenererror-禁止无效的 removeEventListener(传递的函数必须与添加时相同)
unicorn/no-keyword-prefixoff-禁止使用关键字作为变量名的前缀(例如 var classValue
unicorn/no-lonely-iferror-禁止孤独的 if 语句作为 else 块(应使用 else if
unicorn/no-magic-array-flat-deptherror-禁止在 Array.prototype.flat 中使用魔数(magic number)作为深度参数
unicorn/no-named-defaulterror-禁止导入命名默认导出(例如 import {default as foo} from 'bar'
unicorn/no-negated-conditionerror-禁止否定的条件(应反转 ifelse 块来避免否定)
unicorn/no-negation-in-equality-checkerror-禁止在相等检查中使用否定(例如 !== 代替 !(==)
unicorn/no-nested-ternaryerror-禁止嵌套的三元表达式
unicorn/no-new-arrayerror-禁止使用 new Array(),推荐使用字面量 []
unicorn/no-new-buffererror-禁止使用 new Buffer()(出于安全考虑)
unicorn/no-nullerror-禁止使用 null,推荐使用 undefined
unicorn/no-object-as-default-parametererror-禁止使用对象作为默认参数(会导致共享引用问题)
unicorn/no-process-exiterror-禁止使用 process.exit(),应让进程正常退出
unicorn/no-single-promise-in-promise-methodserror-禁止在 Promise.all 等方法中传递单个 Promise(无需包装)
unicorn/no-static-only-classerror-禁止只有静态方法的类(应使用普通对象)
unicorn/no-thenableerror-禁止使用 thenable 对象(非 Promise 但有 then 方法)
unicorn/no-this-assignmenterror-禁止将 this 赋值给变量(应使用箭头函数或绑定)
unicorn/no-typeof-undefinederror-禁止 typeof undefined(总是返回 'undefined',是多余的)
unicorn/no-unnecessary-array-flat-deptherror-禁止在 Array.prototype.flat 中使用不必要的深度参数(深度为 1 时可省略)
unicorn/no-unnecessary-array-splice-counterror-禁止在 Array.prototype.splice 中使用不必要的计数参数(计数为 0 时可省略)
unicorn/no-unnecessary-awaiterror-禁止不必要的 await(在非 Promise 前或可并行操作时)
unicorn/no-unnecessary-polyfillserror-禁止不必要的 polyfills(针对现代浏览器或环境)
unicorn/no-unnecessary-slice-enderror-禁止在 Array.prototype.slice 中使用不必要的结束参数(例如 array.length, Infinity
unicorn/no-unreadable-array-destructuringerror-禁止难以阅读的数组解构(例如过深的嵌套)
unicorn/no-unreadable-iifeerror-禁止难以阅读的立即调用函数表达式(IIFE)
unicorn/no-unused-propertiesoff-禁止未使用的对象属性
unicorn/no-useless-error-capture-stack-traceerror-禁止无用的错误堆栈捕获(例如在 Error.captureStackTrace 中传递自身)
unicorn/no-useless-fallback-in-spreaderror-禁止在展开运算符中使用无用的回退
unicorn/no-useless-length-checkerror-禁止无用的长度检查(例如 array.length === 0 检查空数组)
unicorn/no-useless-promise-resolve-rejecterror-禁止在 Promise 中无使用 resolvereject(例如 new Promise((resolve) => resolve())
unicorn/no-useless-spreaderror-禁止无用的展开运算符(例如 [...array]array 已是数组时)
unicorn/no-useless-switch-caseerror-禁止无用的 switch case(例如多个 case 执行相同操作且无 break
unicorn/no-useless-undefinederror-禁止返回无用的 undefined(函数默认返回 undefined
unicorn/no-zero-fractionserror-禁止数字中无用的小数部分(例如 1.0 应写为 1
unicorn/number-literal-caseerror-强制数字字面量使用小写字母(例如 0x10 而不是 0X10
unicorn/numeric-separators-styleerror-强制数值分隔符使用一致的风格(例如 1_000_000
unicorn/prefer-add-event-listenererror-强制使用 addEventListener 而不是 onclick 等属性
unicorn/prefer-array-finderror-强制使用 Array.prototype.find 来查找数组元素
unicorn/prefer-array-flaterror-强制使用 Array.prototype.flat 而不是递归展开或其它方法
unicorn/prefer-array-flat-maperror-强制使用 Array.prototype.flatMap 而不是 map 后接 flat
unicorn/prefer-array-index-oferror-强制使用 Array.prototype.indexOf 而不是循环查找索引
unicorn/prefer-array-someerror-强制使用 Array.prototype.some 而不是循环检查存在性
unicorn/prefer-aterror-强制使用 .at() 方法进行负索引访问(例如 array[-1] 不行,但 array.at(-1) 可以)
unicorn/prefer-bigint-literalserror-强制使用 BigInt 字面量(例如 1n)而不是 BigInt(1)
unicorn/prefer-blob-reading-methodserror-强制使用 Blob 的读取方法(例如 text(), arrayBuffer())而不是 FileReader
unicorn/prefer-class-fieldserror-强制使用类字段语法而不是在构造函数中赋值
unicorn/prefer-classlist-toggleerror-强制使用 classList.toggle 的第二个参数(强制添加/移除)而不是条件判断
unicorn/prefer-code-pointerror-强制使用 String.prototype.codePointAtString.fromCodePoint 来处理 Unicode
unicorn/prefer-date-nowerror-强制使用 Date.now() 而不是 new Date().getTime()
unicorn/prefer-default-parameterserror-强制使用默认参数而不是在函数体内设置默认值
unicorn/prefer-dom-node-appenderror-强制使用 Node.append 而不是 Node.appendChild(支持多个参数)
unicorn/prefer-dom-node-dataseterror-强制使用 dataset 来访问和设置 data-* 属性
unicorn/prefer-dom-node-removeerror-强制使用 ChildNode.remove 而不是 parentNode.removeChild
unicorn/prefer-dom-node-text-contenterror-强制使用 Node.textContent 而不是 Node.innerText(性能更好,更可预测)
unicorn/prefer-event-targeterror-强制使用 EventTarget 而不是 EventEmitter(用于自定义事件)
unicorn/prefer-export-fromerror-强制使用 export {foo} from 'bar' 而不是先导入再导出
unicorn/prefer-global-thiserror-强制使用 globalThis 来访问全局对象(而不是 window, global 等)
unicorn/prefer-import-meta-propertiesoff-强制使用 import.meta 对象的属性(如 import.meta.url)而不是传统的替代方案
unicorn/prefer-includeserror-强制使用 String.prototype.includes 而不是 indexOf !== -1
unicorn/prefer-json-parse-bufferoff-强制使用 JSON.parse 时传入 Buffer 而不是字符串(性能考虑)
unicorn/prefer-keyboard-event-keyerror-强制使用 KeyboardEvent.key 而不是 keyCodewhich(已废弃)
unicorn/prefer-logical-operator-over-ternaryerror-强制使用逻辑运算符(&&, `
unicorn/prefer-math-min-maxerror-强制使用 Math.minMath.max 而不是手写条件判断
unicorn/prefer-math-truncerror-强制使用 Math.trunc 来取整而不是 bitwise operators 或其它方法
unicorn/prefer-modern-dom-apiserror-强制使用现代 DOM API(例如 DocumentFragment 而不是 innerHTML
unicorn/prefer-modern-math-apiserror-强制使用现代的 Math API(例如 Math.clz32
unicorn/prefer-moduleerror-强制使用 ES 模块(import/export)而不是 CommonJS(require/module.exports
unicorn/prefer-native-coercion-functionserror-强制使用原生类型转换函数(例如 String(x), Number(x))而不是 x.toString(), +x
unicorn/prefer-negative-indexerror-强制使用负索引来从数组末尾访问元素(结合 .at()slice
unicorn/prefer-node-protocolerror-强制在导入 Node.js 内置模块时使用 node: 协议(例如 import fs from 'node:fs'
unicorn/prefer-number-propertieserror-强制使用 Number 上的属性(例如 Number.isNaN 而不是 isNaN
unicorn/prefer-object-from-entrieserror-强制使用 Object.fromEntries 来将键值对列表转换为对象
unicorn/prefer-optional-catch-bindingerror-强制在 catch 子句中省略不必要的参数(当不需要错误对象时)
unicorn/prefer-prototype-methodserror-强制在原型上调用方法而不是在实例上(例如 Array.prototype.slice.call
unicorn/prefer-query-selectorerror-强制使用 document.querySelectordocument.querySelectorAll 而不是 getElementById, getElementsByClassName
unicorn/prefer-reflect-applyerror-强制使用 Reflect.apply 而不是 Function.prototype.apply
unicorn/prefer-regexp-testerror-强制使用 RegExp.prototype.test 而不是 String.prototype.match 来检查匹配
unicorn/prefer-set-haserror-强制使用 Set.prototype.has 来检查 Set 中是否存在某个值(而不是数组的 includes
unicorn/prefer-set-sizeerror-强制使用 Set.prototype.size 而不是将 Set 转换为数组再取长度
unicorn/prefer-single-callerror-强制合并多次数组方法调用为单次调用(例如多次 push 改为一次 push 多个元素)
unicorn/prefer-spreaderror-强制使用展开运算符(...)而不是 Function.prototype.apply 来传递数组参数
unicorn/prefer-string-rawerror-强制使用 String.raw 来获取模板字符串的原始字符串
unicorn/prefer-string-replace-allerror-强制使用 String.prototype.replaceAll 而不是正则表达式带 g 标志的 replace
unicorn/prefer-string-sliceerror-强制使用 String.prototype.slice 而不是 substringsubstr(行为更一致)
unicorn/prefer-string-starts-ends-witherror-强制使用 String.prototype.startsWithendsWith 而不是 indexOf 或正则表达式
unicorn/prefer-string-trim-start-enderror-强制使用 String.prototype.trimStarttrimEnd 而不是 trimLefttrimRight(标准名称)
unicorn/prefer-structured-cloneerror-强制使用 structuredClone 而不是其他深度克隆方法(如 JSON.parse(JSON.stringify())
unicorn/prefer-switcherror-强制使用 switch 语句而不是多个 if-else if 语句
unicorn/prefer-ternaryerror-强制使用三元运算符而不是简单的 if-else 语句
unicorn/prefer-top-level-awaiterror-强制在模块顶层使用 await 而不是包裹在 async 函数中(需在 ES 模块中)
unicorn/prefer-type-errorerror-强制抛出 TypeError 而不是 error 用于类型错误
unicorn/prevent-abbreviationserror-防止使用缩写(例如 e 代替 event, cb 代替 callback
unicorn/relative-url-styleerror-强制相对 URL 使用一致的风格(例如 ./ 开头或不开头)
unicorn/require-array-join-separatorerror-强制 Array.prototype.join 方法提供分隔符参数(显式传递空字符串如果需要)
unicorn/require-module-attributeserror-强制导入模块时声明属性(例如 import json from './data.json' assert {type: 'json'};
unicorn/require-module-specifierserror-强制模块标识符使用特定风格(例如相对路径还是绝对路径)
unicorn/require-number-to-fixed-digits-argumenterror-强制 Number.prototype.toFixed 提供参数(避免浏览器差异)
unicorn/require-post-message-target-originoff-强制 window.postMessage 提供 targetOrigin 参数(安全考虑)
unicorn/string-contentoff-强制字符串内容使用特定风格(例如禁止或要求使用某些字符)
unicorn/switch-case-braceserror-强制 switch case 使用大括号
unicorn/template-indenterror-强制模板字符串的缩进保持一致
unicorn/text-encoding-identifier-caseerror-强制文本编码标识符使用特定的大小写(例如 'utf-8' 而不是 'utf8'
unicorn/throw-new-errorerror-强制抛出错误时使用 new(例如 throw new Error() 而不是 throw Error()

Promise

eslint-plugin-promise 专注于 Promise 的规则,用于确保 Promise 的正确使用,例如检查 then / catch 的链式调用、防止未处理的 Promise 拒绝等,有助于编写更健壮的异步代码。

// .eslintrc.js

module.exports = {
extends: [
'plugin:promise/recommended',
],
};
规则名称错误级别配置选项描述
promise/always-returnerror-要求在 Promise 链中的 then() 方法里必须返回一个值,以确保函数有返回值,避免意外错误
promise/no-return-wraperror-禁止不必要的包装返回值,例如直接返回一个Promise或使用Promise.resolve/Promise.reject包装值,应直接返回值或错误
promise/param-nameserror-要求 Promise 的 executor 函数的参数名必须为 resolvereject,以防止参数名拼写错误导致的问题
promise/catch-or-returnerrorallowFinally, allowThen, terminationMethod要求Promise必须使用catch()方法处理错误或被返回以便在其他地方处理,以避免未处理的Promise拒绝
promise/no-nativeoff-禁止使用原生的 Promise 全局对象,强制使用如 bluebird 这样的库。通常在你不使用原生 Promise 时开启
promise/no-nestingoff-尽可能禁止不必要的 Promise 嵌套,建议使用 async/await 或扁平化 Promise 链来简化代码
promise/no-promise-in-callbackoff-警告在回调函数中返回 Promise 的情况,这可能导致 Promise 被忽略且错误未被处理,常见于类似 then() 的回调中
promise/no-callback-in-promiseoff-不建议在 Promise 中使用回调函数模式(即 donenext 等参数),因为这可能绕过 Promise 的错误捕获机制
promise/avoid-newoff-禁止使用 new Promise 构造函数,建议在可能的情况下使用Promise 的工厂函数或 async 函数
promise/no-new-staticserror-禁止调用 Promise 的静态方法(如Promise.resolvePromise.reject)时使用 new 操作符(例如 new Promise.resolve(...) ),这是不必要的
promise/no-return-in-finallyoff-禁止在 Promise.prototype.finally() 方法中返回值,因为 finally 处理程序中的返回值会被忽略,不会影响 Promise 的最终状态
promise/valid-paramsoff-确保 Promise 的静态方法(如 all, race, allSettled, any )接收的参数是有效的(例如,是一个可迭代对象)

RegExp

eslint-plugin-regexp 旨在帮助你编写更安全、更高效的正则表达式。它会检查正则表达式中的常见问题。

// .eslintrc.js

module.exports = {
extends: [
'plugin:regexp/recommended',
],
};
规则名称错误级别配置选项描述
no-control-regexerror-禁止在正则表达式中使用控制字符相关的语法(如\x08),因为这些字符很少使用,很可能是错误
no-misleading-character-classerror-禁止在字符类中出现可能引起误解的字符,例如在 [âœ] 中,✠可能被误解为单个字符而非[â, œ]。该规则会检查此类问题并提供建议
no-regex-spaceserror-禁止在正则表达式字面量中使用多个空格,以提高可读性。建议使用明确的量词,如 {3},来替代连续的空格
prefer-regex-literalserror-建议使用正则表达式字面量(如 /abc/)而非 RegExp 构造函数,除非需要动态构建模式,以使代码更简洁
regexp/confusing-quantifierwarn-检测可能造成混淆的量词使用
regexp/control-character-escapeerror-检查控制字符转义的使用
regexp/match-anyerror-确保匹配任意字符的表达式(如 .[^])使用得当
regexp/negationerror-检查字符类否定的使用
regexp/no-contradiction-with-assertionerror-检测与断言矛盾的表达式
regexp/no-dupe-characters-character-classerror-禁止字符类中出现重复的字符
regexp/no-dupe-disjunctionserror-禁止在析取(或操作)中出现重复的子表达式
regexp/no-empty-alternativewarn-警告存在空的选项
regexp/no-empty-capturing-grouperror-禁止空的捕获组
regexp/no-empty-character-classerror-禁止空的字符类
regexp/no-empty-grouperror-禁止空的组
regexp/no-empty-lookarounds-assertionerror-禁止空的环视断言
regexp/no-empty-string-literalerror-禁止空字符串字面量
regexp/no-escape-backspaceerror-检查不必要的反斜杠转义退格符
regexp/no-extra-lookaround-assertionserror-检测多余的环视断言
regexp/no-invalid-regexperror-禁止无效的正则表达式模式
regexp/no-invisible-charactererror-禁止不可见字符
regexp/no-lazy-endswarn-警告在字符串末尾使用惰性量词,这通常是低效的
regexp/no-legacy-featureserror-禁止使用已被新语法替代的旧式正则表达式特性
regexp/no-misleading-capturing-grouperror-检测可能引起误解的捕获组
regexp/no-misleading-unicode-charactererror-禁止使用可能引起误解的Unicode字符
regexp/no-missing-g-flagerror-在需要全局匹配时,检查是否缺失了 g 标志
regexp/no-non-standard-flagerror-禁止使用非标准的正则表达式标志
regexp/no-obscure-rangeerror-检测晦涩难懂的字符范围
regexp/no-optional-assertionerror-禁止可选的断言
regexp/no-potentially-useless-backreferencewarn-警告可能无用的反向引用
regexp/no-super-linear-backtrackingerror-检测可能导致超线性回溯的模式,这会引起严重的性能问题(ReDoS攻击)
regexp/no-trivially-nested-assertionerror-禁止浅层嵌套的断言
regexp/no-trivially-nested-quantifiererror-禁止浅层嵌套的量词
regexp/no-unused-capturing-grouperror-检测未被使用的捕获组,建议改为非捕获组 (?:...)
regexp/no-useless-backreferenceerror-禁止无用的反向引用
regexp/no-zero-quantifiererror-禁止最大匹配次数为0的量词(如 a{0}),因为它们不会匹配任何内容
regexp/no-useless-assertionserror-禁止无用的断言
regexp/no-useless-character-classerror-禁止不必要的字符类,例如当类中只有一个字符时 [a]
regexp/no-useless-dollar-replacementserror-禁止在替换字符串中使用无用的 $ 替换
regexp/no-useless-escapeerror-禁止不必要的转义
regexp/no-useless-flagwarn-警告无用的正则表达式标志
regexp/no-useless-lazyerror-禁止在不需要贪婪或惰性匹配的量词上使用惰性量词(?
regexp/no-useless-non-capturing-grouperror-禁止无用的非捕获组 (?:...)
regexp/no-useless-quantifiererror-禁止无用的量词
regexp/no-useless-rangeerror-禁止无用的范围量词
regexp/no-useless-set-operanderror-禁止在集合操作中使用无用的操作数
regexp/no-useless-string-literalerror-禁止无用的字符串字面量
regexp/no-useless-two-nums-quantifiererror-禁止使用两个数字表示的无用量词(如 a{1,1}),建议简化为 a{1}
regexp/optimal-lookaround-quantifierwarn-建议优化环视断言中的量词使用
regexp/optimal-quantifier-concatenationerror-优化量词的连接方式
regexp/prefer-character-classerror-建议使用字符类而非析取
regexp/prefer-derror-建议使用 \d 来匹配数字
regexp/prefer-plus-quantifiererror-建议使用 + 量词而非 {1,}
regexp/prefer-predefined-assertionerror-建议使用预定义的断言(如 \b)而非等效的复杂表达式
regexp/prefer-question-quantifiererror-建议使用 ? 量词而非 {0,1}
regexp/prefer-rangeerror-建议使用范围表示法(如 [0-9]
regexp/prefer-set-operationerror-建议使用集合操作来简化字符类
regexp/prefer-star-quantifiererror-建议使用 * 量词而非 {0,}
regexp/prefer-unicode-codepoint-escapeserror-建议优先使用Unicode码点转义(如 \u{1F600}
regexp/prefer-werror-建议使用 \w 来匹配单词字符
regexp/simplify-set-operationserror-简化字符类中的集合操作
regexp/sort-flagserror-要求对正则表达式的标志进行排序,以保持一致性
regexp/use-ignore-caseerror-在忽略大小写匹配时,建议使用 i 标志
regexp/stricterror-这是一个预设规则,旨在执行严格的检查

JSDoc

eslint-plugin-jsdoc 是针对 JSDoc 注释的规则集,专门为 JavaScript/TypeScript 设计。它强制要求函数、类等代码结构有清晰的文档注释,并检查注释的格式和类型信息是否与代码一致,从而提高代码的可维护性和文档化水平。

// .eslintrc.js

module.exports = {
extends: [
'plugin:jsdoc/recommended-typescript',
],
};
规则名称错误级别配置选项描述
jsdoc/check-accesswarn-检查 JSDoc 注释中 @access 标签的使用是否正确
jsdoc/check-alignmentwarn-检查 JSDoc 注释中星号 (*) 的缩进和对齐方式
jsdoc/check-examplesoff-检查 @example 标签中的代码片段是否符合语法规范
jsdoc/check-indentationoff-检查 JSDoc 注释内部的缩进是否一致
jsdoc/check-line-alignmentoff-检查 JSDoc 注释标签是否对齐
jsdoc/check-param-nameswarn-检查 @param 标签指定的参数名称是否与函数定义中的参数名称匹配
jsdoc/check-property-nameswarn-检查 @property 标签指定的属性名称是否与对象定义中的属性名称匹配
jsdoc/check-syntaxoff-检查 JSDoc 注释中是否存在基本的语法错误
jsdoc/check-template-namesoff-检查 JSDoc 注释中使用的模板标签名称(如 @template)是否有效
jsdoc/check-typeswarn-检查 JSDoc 注释中类型注释(如 {string}, {number})的语法和有效性
jsdoc/check-valueswarn-检查 JSDoc 注释中特定标签(如 @version, @since)的值是否符合预期格式
jsdoc/convert-to-jsdoc-commentsoff-尝试将普通的多行注释 (/* ... */) 转换为 JSDoc 注释 (/** ... */)
jsdoc/empty-tagswarn-检查 JSDoc 注释中不应包含内容的标签(如 @abstract, @async)后面是否确实没有内容
jsdoc/implements-on-classeswarn-检查 @implements 标签是否只用于类(Class)上
jsdoc/imports-as-dependenciesoff-检查导入语句是否被正确标记为依赖
jsdoc/informative-docsoff-鼓励或要求 JSDoc 注释提供更具信息性的描述
jsdoc/lines-before-blockoff-强制或禁止在 JSDoc 注释块之前有空行
jsdoc/match-descriptionoff-强制 JSDoc 注释的描述部分符合特定的正则表达式模式
jsdoc/match-nameoff-强制 JSDoc 注释所附着的代码元素的名称符合特定的正则表达式模式
jsdoc/multiline-blockswarn-强制多行 JSDoc 注释的特定格式
jsdoc/no-bad-blocksoff-禁止在注释中使用可能被误解析为 JSDoc 注释的特定模式或标签
jsdoc/no-blank-block-descriptionsoff-禁止 JSDoc 注释块的顶部的描述部分为空
jsdoc/no-blank-blocksoff-禁止出现完全空白的 JSDoc 注释块
jsdoc/no-defaultswarn-禁止在 @param@property 标签中指定默认值
jsdoc/no-missing-syntaxoff-检查代码中是否缺少预期的 JSDoc 注释
jsdoc/no-multi-asteriskswarn-禁止在 JSDoc 注释中使用多个连续的星号(**
jsdoc/no-restricted-syntaxoff-允许定义并禁止使用特定的 JSDoc 标签或语法模式
jsdoc/reject-any-typewarn-禁止在 JSDoc 类型注释中使用 any* 这种不明确的类型
jsdoc/reject-function-typewarn-禁止在 JSDoc 类型注释中使用 Function 这种不明确的函数类型
jsdoc/require-asterisk-prefixoff-强制 JSDoc 注释中除第一行和最后一行外,每一行都以一个星号 (*) 开头
jsdoc/require-descriptionoff-强制 JSDoc 注释必须包含描述文本(在标签之前)
jsdoc/require-description-complete-sentenceoff-强制 JSDoc 注释中的描述部分必须是一个完整的句子
jsdoc/require-exampleoff-强制要求 JSDoc 注释中包含 @example 标签
jsdoc/require-file-overviewoff-强制在每个文件顶部有一个特定的 JSDoc 注释,用于提供文件概述
jsdoc/require-hyphen-before-param-descriptionoff-强制在 @param 标签的描述文本前必须有一个连字符(-)或空格等
jsdoc/require-jsdocwarn-强制为特定的代码结构(如函数、类、方法)添加 JSDoc 注释
jsdoc/require-next-typewarn-检查迭代器next方法的返回类型
jsdoc/require-paramwarn-强制为函数声明或表达式中的每一个参数添加 @param 标签
jsdoc/require-param-descriptionwarn-强制每一个 @param 标签都必须包含对该参数的描述
jsdoc/require-param-namewarn-强制每一个 @param 标签都必须指定参数的名称
jsdoc/require-propertywarn-强制为使用 @typedef 定义的类型的每一个属性在 JSDoc 注释中添加 @property 标签
jsdoc/require-property-descriptionwarn-强制每一个 @property 标签都必须包含对该属性的描述
jsdoc/require-property-namewarn-强制每一个 @property 标签都必须指定属性的名称
jsdoc/require-returnswarn-强制为有返回值的函数(非 void)添加 @returns 标签
jsdoc/require-returns-checkwarn-强制如果函数返回一个值(非 void),则必须存在 @returns 标签
jsdoc/require-returns-descriptionwarn-强制 @returns 标签必须包含对返回值的描述
jsdoc/require-templateoff-强制使用模板标签
jsdoc/require-throwsoff-强制为函数中可能抛出错误的情况添加 @throws 标签
jsdoc/require-throws-typewarn-强制 @throws 标签必须指定所抛出错误的类型
jsdoc/require-yieldswarn-强制生成器函数或使用 yield 的函数添加 @yields 标签
jsdoc/require-yields-checkwarn-强制如果函数中有 yield 语句,则必须存在 @yields 标签
jsdoc/require-yields-typewarn-强制 @yields 标签必须指定产出值的类型
jsdoc/sort-tagsoff-强制 JSDoc 注释中的标签按特定的顺序排列
jsdoc/tag-lineswarn-强制规定每个 JSDoc 标签之前或之后应有多少空行
jsdoc/text-escapingoff-检查文本转义是否正确
jsdoc/type-formattingoff-强制 JSDoc 类型注释的格式
jsdoc/valid-typeswarn-验证 JSDoc 注释中特定标签(如 @typedef, @callback)后的类型声明是否符合语法规范
jsdoc/check-tag-nameswarn{typed: true}检查 JSDoc 注释中所有标签的名称是否拼写正确且是已知的标准标签或配置允许的标签
jsdoc/no-typeswarn-禁止在 JSDoc 注释中使用类型注解(如 {string}),鼓励使用 TypeScript 或其他类型系统
jsdoc/no-undefined-typesoff-检查 JSDoc 注释中使用的类型名称是否已定义或是已知的类型(如内置类型、全局类型或导入的类型)
jsdoc/require-param-typeoff-强制每一个 @param 标签都必须指定参数的类型
jsdoc/require-property-typeoff-强制每一个 @property 标签都必须指定属性的类型
jsdoc/require-returns-typeoff-强制 @returns 标签必须指定返回值的类型

ESLint 注释

@eslint-community/eslint-plugin-eslint-comments 用于规范代码中 ESLint 注释(例如 /* eslint-disable */)的使用,防止不必要的禁用规则注释,确保规则禁用的原因清晰。

// .eslintrc.js

module.exports = {
extends: [
'plugin:@eslint-community/eslint-comments/recommended',
],
};
规则名称错误级别配置选项描述
@eslint-community/eslint-comments/disable-enable-pairerror-要求 /* eslint-disable */ 注释必须有一个对应的 /* eslint-enable */ 注释成对出现,防止规则被意外地全局禁用
@eslint-community/eslint-comments/no-aggregating-enableerror-禁止使用一个 /* eslint-enable */ 注释来同时启用多个之前被禁用的规则,要求逐一明确启用
@eslint-community/eslint-comments/no-duplicate-disableerror-禁止对同一个规则进行重复的禁用注释,避免冗余
@eslint-community/eslint-comments/no-unlimited-disableerror-禁止不使用任何规则名参数的 /* eslint-disable */ 注释(即禁用所有规则),要求禁用注释必须明确指定要禁用的规则列表,防止无意中关闭所有检查
@eslint-community/eslint-comments/no-unused-enableerror-禁止存在没有对应禁用注释的 /* eslint-enable */ 注释,确保启用的有效性