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-pairs | off | - | 强制 getter/setter 成对出现 |
| array-callback-return | error | { allowImplicit: true } | 强制数组方法的回调函数中有 return 语句 |
| block-scoped-var | error | - | 禁止在块作用域外使用变量 |
| complexity | off | 20 | 限制代码圈复杂度 |
| class-methods-use-this | error | { exceptMethods: [] } | 强制类方法使用 this |
| consistent-return | error | - | 要求函数返回类型一致 |
| curly | error | multi-line | 强制使用大括号控制流语句 |
| default-case | error | { commentPattern: '^no default$' } | 要求 switch 语句必须有 default |
| default-case-last | error | - | 要求 default case 放在最后 |
| default-param-last | error | - | 要求默认参数放在最后 |
| dot-notation | error | { allowKeywords: true } | 强制使用点号访问属性 |
| dot-location | error | property | 强制点号与属性同一行 |
| eqeqeq | error | always, { null: 'ignore' } | 强制使用 === 和 !== |
| grouped-accessor-pairs | error | - | 要求 getter/setter 成组出现 |
| guard-for-in | error | - | 要求 for-in 循环包含 if 语句 |
| max-classes-per-file | error | 1 | 限制每个文件的类数量 |
| no-alert | warn | - | 禁止使用 alert |
| no-caller | error | - | 禁止使用 arguments.caller/callee |
| no-case-declarations | error | - | 禁止 case 语句中声明变量 |
| no-constructor-return | error | - | 禁止构造函数返回值 |
| no-div-regex | off | - | 禁止在正则表达式中使用除法符号 |
| no-else-return | error | { allowElseIf: false } | 禁止在 else 前有 return |
| no-empty-function | error | { allow: [...] } | 禁止空函数 |
| no-empty-pattern | error | - | 禁止空解构模式 |
| no-empty-static-block | off | - | 禁止空静态块 |
| no-eq-null | off | - | 禁止与 null 比较 |
| no-eval | error | - | 禁止使用 eval |
| no-extend-native | error | - | 禁止扩展原生对象 |
| no-extra-bind | error | - | 禁止不必要的 bind |
| no-extra-label | error | - | 禁止不必要的标签 |
| no-fallthrough | error | - | 禁止 case 穿透 |
| no-floating-decimal | error | - | 禁止浮点小数 |
| no-global-assign | error | { exceptions: [] } | 禁止覆盖原生对象 |
| no-native-reassign | off | - | 禁止覆盖原生对象 (已弃用) |
| no-implicit-coercion | off | { boolean: false, ... } | 禁止隐式类型转换 |
| no-implicit-globals | off | - | 禁止隐式全局变量 |
| no-implied-eval | error | - | 禁止隐式 eval |
| no-invalid-this | off | - | 禁止无效的 this 上下文 |
| no-iterator | error | - | 禁止使用 __iterator__ |
| no-labels | error | { allowLoop: false, ... } | 禁止标签语句 |
| no-lone-blocks | error | - | 禁止不必要的嵌套块 |
| no-loop-func | error | - | 禁止循环中创建函数 |
| no-magic-numbers | off | { ignore: [], ... } | 禁止魔法数字 |
| no-multi-spaces | error | { ignoreEOLComments: false } | 禁止多个空格 |
| no-multi-str | error | - | 禁止多行字符串 |
| no-new | error | - | 禁止 new 操作符副作用 |
| no-new-func | error | - | 禁止 new Function |
| no-new-wrappers | error | - | 禁止 new 包装对象 |
| no-nonoctal-decimal-escape | error | - | 禁止非八进制十进制转义 |
| no-object-constructor | off | - | 禁止 Object 构造函数 |
| no-octal | error | - | 禁止八进制字面量 |
| no-octal-escape | error | - | 禁止八进制转义序列 |
| no-param-reassign | error | { props: true, ... } | 禁止参数重新赋值 |
| no-proto | error | - | 禁止 __proto__ |
| no-redeclare | error | - | 禁止重复声明变量 |
| no-restricted-properties | error | 多对象配置 | 限制特定对象属性 |
| no-return-assign | error | always | 禁止 return 中赋值 |
| no-return-await | error | - | 禁止不必要的 return await |
| no-script-url | error | - | 禁止 javascript: URL |
| no-self-assign | error | { props: true } | 禁止自我赋值 |
| no-self-compare | error | - | 禁止自我比较 |
| no-sequences | error | - | 禁止逗号操作符 |
| no-throw-literal | error | - | 禁止抛出字面量 |
| no-unmodified-loop-condition | off | - | 禁止未修改的循环条件 |
| no-unused-expressions | error | { allowShortCircuit: false, ... } | 禁止未使用的表达式 |
| no-unused-labels | error | - | 禁止未使用的标签 |
| no-useless-call | off | - | 禁止不必要的 .call/.apply |
| no-useless-catch | error | - | 禁止不必要的 catch |
| no-useless-concat | error | - | 禁止不必要的字符串连接 |
| no-useless-escape | error | - | 禁止不必要的转义 |
| no-useless-return | error | - | 禁止不必要的 return |
| no-void | error | - | 禁止 void 操作符 |
| no-warning-comments | off | { terms: [...], location: 'start' } | 禁止警告注释 |
| no-with | error | - | 禁止 with 语句 |
| prefer-promise-reject-errors | error | { allowEmptyReject: true } | 要求 Promise reject 错误对象 |
| prefer-named-capture-group | off | - | 建议命名捕获组 |
| prefer-object-has-own | off | - | 建议使用 Object.hasOwn() |
| prefer-regex-literals | error | { disallowRedundantWrapping: true } | 建议使用正则字面量 |
| radix | error | - | 要求 parseInt 使用基数 |
| require-await | off | - | 要求 async 函数有 await |
| require-unicode-regexp | off | - | 要求正则表达式使用 u 标志 |
| vars-on-top | error | - | 要求变量声明在作用域顶部 |
| wrap-iife | error | outside, { functionPrototypeMethods: false } | 要求立即执行函数使用括号包裹 |
| yoda | error | - | 禁止 Yoda 条件 |
错误
| 规则名 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| for-direction | error | - | 防止循环条件错误导致无限循环 |
| getter-return | error | { allowImplicit: true } | 强制 getter 函数必须有返回值 |
| no-async-promise-executor | error | - | 禁止使用异步函数作为 Promise 执行器 |
| no-await-in-loop | error | - | 禁止在循环内使用 await |
| no-compare-neg-zero | error | - | 禁止与 -0 进行比较 |
| no-cond-assign | error | "always" | 禁止在条件语句中进行赋值操作 |
| no-console | warn | - | 警告使用 console 语句 |
| no-constant-binary-expression | off | - | 关闭检测常量二进制表达式问题 |
| no-constant-condition | warn | - | 警告在条件中使用常量值 |
| no-control-regex | error | - | 禁止在正则表达式中使用控制字符 |
| no-debugger | error | - | 禁止使用 debugger 语句 |
| no-dupe-args | error | - | 禁止函数参数重复定义 |
| no-dupe-else-if | error | - | 禁止 else if 中有重复条件 |
| no-dupe-keys | error | - | 禁止对象字面量中重复的键 |
| no-duplicate-case | error | - | 禁止 switch 语句中有重复的 case |
| no-empty | error | - | 禁止空块语句 |
| no-empty-character-class | error | - | 禁止正则表达式中出现空字符类 |
| no-ex-assign | error | - | 禁止对 catch 子句中的异常重新赋值 |
| no-extra-boolean-cast | error | - | 禁止不必要的布尔类型转换 |
| no-extra-parens | off | "all", { conditionalAssign: true, nestedBinaryExpressions: false, returnAssign: false, ignoreJSX: "all", enforceForArrowConditionals: false } | 关闭检测多余的括号(配置保留) |
| no-extra-semi | error | - | 禁止不必要的分号 |
| no-func-assign | error | - | 禁止对函数声明重新赋值 |
| no-import-assign | error | - | 禁止对导入的绑定进行赋值 |
| no-inner-declarations | error | - | 禁止在嵌套块中声明函数或变量 |
| no-invalid-regexp | error | - | 禁止无效的正则表达式 |
| no-irregular-whitespace | error | - | 禁止不规则的空白字符 |
| no-loss-of-precision | error | - | 禁止数字精度丢失 |
| no-misleading-character-class | error | - | 禁止正则中可能导致误解的字符类 |
| no-obj-calls | error | - | 禁止将全局对象当作函数调用 |
| no-new-native-nonconstructor | off | - | 关闭检测无法作为构造函数的内置对象调用 |
| no-promise-executor-return | error | - | 禁止在 Promise 执行器中返回值 |
| no-prototype-builtins | error | - | 禁止直接调用对象的原型方法(如 hasOwnProperty) |
| no-regex-spaces | error | - | 禁止正则表达式中出现多个空格 |
| no-setter-return | error | - | 强制 setter 函数必须有返回值 |
| no-sparse-arrays | error | - | 禁止稀疏数组(如 [1,,2]) |
| no-template-curly-in-string | error | - | 禁止字符串中出现模板字面量占位符语法 |
| no-unexpected-multiline | error | - | 禁止出现意外的多行表达式 |
| no-unreachable | error | - | 禁止不可达的代码 |
| no-unreachable-loop | error | { ignore: [] } | 禁止无法正常退出的循环 |
| no-unsafe-finally | error | - | 禁止在 finally 块中使用控制流语句 |
| no-unsafe-negation | error | - | 禁止不安全的取反操作(如 !obj in expr) |
| no-unsafe-optional-chaining | error | { disallowArithmeticOperators: true } | 禁止不安全的可选链操作 |
| no-unused-private-class-members | off | - | 关闭检测未使用的私有类成员 |
| no-useless-backreference | error | - | 禁止正则表达式中无用的反向引用 |
| no-negated-in-lhs | off | - | 关闭检测 in 操作符的左操作符是否被取反(已弃用) |
| require-atomic-updates | off | - | 关闭要求异步操作必须是原子性的 |
| use-isnan | error | - | 必须使用 isNaN() 检查 NaN |
| valid-jsdoc | off | - | 关闭强制有效的 JSDoc 注释 |
| valid-typeof | error | { requireStringLiterals: true } | 强制 typeof 操作符必须与有效字符串字面量比较 |
node.js
| 规则名称 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| callback-return | off | - | 强制在回调函数中使用 return 语句 |
| global-require | error | - | 强制 require() 在模块顶部调用 |
| handle-callback-err | off | - | 强制回调函数的错误处理 |
| no-buffer-constructor | error | - | 禁用 Buffer() 构造函数(已废弃,建议迁移到 eslint-plugin-node) |
| no-mixed-requires | off | [false] | 禁止混合常规变量和模块引入 |
| no-new-require | error | - | 禁止对 require 使用 new |
| no-path-concat | error | - | 禁止路径字符串拼接 |
| no-process-env | off | - | 禁用 process.env |
| no-process-exit | off | - | 禁用 process.exit() |
| no-restricted-modules | off | - | 禁止使用特定模块(已废弃,建议改用 no-restricted-imports) |
| no-sync | off | - | 禁用同步方法 |
风格
| 规则名 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| array-bracket-newline | off | 'consistent' | 强制数组括号的换行风格一致 |
| array-element-newline | off | { multiline: true, minItems: 3 } | 多行或超过最小项时换行(已关闭) |
| array-bracket-spacing | error | 'never' | 禁止数组括号内添加空格 |
| block-spacing | error | 'always' | 强制块内保留空格 |
| brace-style | error | '1tbs', { allowSingleLine: true } | 大括号风格为 1tbs,允许单行块 |
| camelcase | error | { properties: 'never', ignoreDestructuring: false } | 强制驼峰命名(属性名除外) |
| capitalized-comments | off | 'never', { line: {...}, block: {...} } | 关闭注释首字母大写检查 |
| comma-dangle | error | { arrays: 'always-multiline', ... } | 多行结构强制拖尾逗号 |
| comma-spacing | error | { before: false, after: true } | 逗号后必须空格,前无空格 |
| comma-style | error | 'last', { exceptions: {...} } | 逗号置于行尾 |
| computed-property-spacing | error | 'never' | 禁止计算属性内空格 |
| consistent-this | off | - | 关闭强制统一 this 别名 |
| eol-last | error | 'always' | 文件末尾强制换行 |
| function-call-argument-newline | error | 'consistent' | 函数调用参数换行风格一致 |
| func-call-spacing | error | 'never' | 禁止函数名与括号间空格 |
| func-name-matching | off | 'always', { includeCommonJSModuleExports: false } | 关闭函数名匹配检查 |
| func-names | warn | - | 警告未命名函数表达式 |
| func-style | off | 'expression' | 关闭函数声明风格检查 |
| function-paren-newline | error | 'multiline-arguments' | 多行参数时括号换行 |
| id-denylist | off | - | 关闭标识符黑名单检查 |
| id-length | off | - | 关闭标识符长度检查 |
| id-match | off | - | 关闭标识符命名模式检查 |
| implicit-arrow-linebreak | error | 'beside' | 箭头函数返回值与箭头同行 |
| indent | error | 2, { SwitchCase: 1, ... } | 强制2空格缩进,处理不同语法结构 |
| jsx-quotes | off | 'prefer-double' | 关闭JSX引号偏好检查 |
| key-spacing | error | { beforeColon: false, afterColon: true } | 键后必须空格,键前无空格 |
| keyword-spacing | error | { before: true, after: true, ... } | 关键字前后保留空格 |
| line-comment-position | off | { position: 'above' } | 关闭行注释位置检查 |
| linebreak-style | error | 'unix' | 强制Unix换行符(\n) |
| lines-between-class-members | error | 'always', { exceptAfterSingleLine: false } | 类成员间保留空行 |
| lines-around-comment | off | - | 关闭注释周围空行检查 |
| lines-around-directive | error | { before: 'always', after: 'always' } | 指令周围保留空行 |
| logical-assignment-operators | off | 'always', { enforceForIfStatements: true } | 关闭逻辑赋值操作符检查 |
| max-depth | off | 4 | 关闭代码块嵌套深度检查 |
| max-len | error | 100, 2, { ignoreUrls: true, ... } | 单行最大长度100字符 |
| max-lines | off | { max: 300, ... } | 关闭文件最大行数检查 |
| max-lines-per-function | off | { max: 50, ... } | 关闭函数最大行数检查 |
| max-nested-callbacks | off | - | 关闭回调嵌套深度检查 |
| max-params | off | 3 | 关闭函数最大参数检查 |
| max-statements | off | 10 | 关闭函数最大语句数检查 |
| max-statements-per-line | off | { max: 1 } | 关闭单行最大语句数检查 |
| multiline-comment-style | off | 'starred-block' | 关闭多行注释风格检查 |
| multiline-ternary | off | 'never' | 关闭多行三元表达式检查 |
| new-cap | error | { newIsCap: true, capIsNew: false, ... } | 构造函数首字母大写限制 |
| new-parens | error | - | 构造函数调用必须带括号 |
| newline-after-var | off | - | 关闭变量声明后空行检查 |
| newline-before-return | off | - | 关闭return前空行检查 |
| newline-per-chained-call | error | { ignoreChainWithDepth: 4 } | 链式调用超过4层需换行 |
| no-array-constructor | error | - | 禁止使用数组构造函数 |
| no-bitwise | error | - | 禁止位运算符 |
| no-continue | error | - | 禁止使用continue |
| no-inline-comments | off | - | 关闭禁止行内注释检查 |
| no-lonely-if | error | - | 禁止单独的if语句 |
| no-mixed-operators | error | { groups: [...], allowSamePrecedence: false } | 禁止混合使用不同优先级操作符 |
| no-mixed-spaces-and-tabs | error | - | 禁止混用空格和制表符 |
| no-multi-assign | error | - | 禁止连续赋值 |
| no-multiple-empty-lines | error | { max: 1, maxBOF: 0, maxEOF: 0 } | 最多1个连续空行 |
| no-negated-condition | off | - | 关闭禁止否定条件检查 |
| no-nested-ternary | error | - | 禁止嵌套三元表达式 |
| no-new-object | error | - | 禁止使用new Object |
| no-plusplus | error | - | 禁止++和-- |
| no-restricted-syntax | error | ForInStatement, ForOfStatement, ... | 禁用特定语法结构(如for-in) |
| no-spaced-func | off | - | 关闭禁止函数名与括号间空格检查 |
| no-tabs | error | - | 禁止使用制表符 |
| no-ternary | off | - | 关闭禁止三元操作符检查 |
| no-trailing-spaces | error | { skipBlankLines: false, ... } | 禁止行尾空格 |
| no-underscore-dangle | error | { allow: [], ... } | 禁止下划线开头或结尾的标识符 |
| no-unneeded-ternary | error | { defaultAssignment: false } | 禁止不必要的三元表达式 |
| no-whitespace-before-property | error | - | 禁止属性前的空格 |
| nonblock-statement-body-position | error | 'beside', { overrides: {} } | 单行非块语句与声明同行 |
| object-curly-spacing | error | 'always' | 对象花括号内保留空格 |
| object-curly-newline | error | { ObjectExpression: { minProperties: 4, ... } } | 对象属性超过3项时换行 |
| object-property-newline | error | { allowAllPropertiesOnSameLine: true } | 允许对象属性同行 |
| one-var | error | 'never' | 禁止合并变量声明 |
| one-var-declaration-per-line | error | 'always' | 每行一个变量声明 |
| operator-assignment | error | 'always' | 强制操作符简写形式 |
| operator-linebreak | error | 'before', { overrides: { '=': 'none' } } | 操作符置于行首(等号例外) |
| padded-blocks | error | 'never', { allowSingleLineBlocks: true } | 块内不填充空行(单行块允许) |
| padding-line-between-statements | off | - | 关闭语句间空行检查 |
| prefer-exponentiation-operator | error | - | 使用指数操作符替代Math.pow |
| prefer-object-spread | error | - | 使用对象展开替代Object.assign |
| quote-props | error | 'as-needed', { keywords: false, ... } | 按需引用对象属性名 |
| quotes | error | 'single', { avoidEscape: true } | 使用单引号(允许转义) |
| require-jsdoc | off | - | 关闭JSDoc注释检查 |
| semi | error | 'always' | 强制分号结尾 |
| semi-spacing | error | { before: false, after: true } | 分号后空格,前无空格 |
| semi-style | error | 'last' | 分号置于行尾 |
| sort-keys | off | 'asc', { caseSensitive: false, ... } | 关闭对象键排序检查 |
| sort-vars | off | - | 关闭变量排序检查 |
| space-before-blocks | error | - | 块前必须空格 |
| space-before-function-paren | error | { anonymous: 'always', named: 'never', ... } | 函数名后无空格,匿名函数后有空格 |
| space-in-parens | error | 'never' | 括号内不保留空格 |
| space-infix-ops | error | - | 中缀操作符周围保留空格 |
| space-unary-ops | error | { words: true, nonwords: false } | 单词类一元操作符后空格 |
| spaced-comment | error | 'always', { line: {...}, block: {...} } | 注释前保留空格 |
| switch-colon-spacing | error | { after: true, before: false } | switch case冒号后空格,前无空格 |
| template-tag-spacing | error | 'never' | 模板标签后无空格 |
| unicode-bom | error | 'never' | 禁止Unicode BOM头 |
| wrap-regex | off | - | 关闭正则表达式括号包裹检查 |
变量
| 规则名称 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| init-declarations | off | - | 强制或禁止变量声明时初始化 |
| no-catch-shadow | off | - | 禁止catch子句参数与外部变量同名 |
| no-delete-var | error | - | 禁止使用delete删除变量 |
| no-label-var | error | - | 禁止标签与变量同名 |
| no-restricted-globals | error | [ { name: "isFinite", message: "Use Number.isFinite instead" }, { name: "isNaN", message: "Use Number.isNaN instead" }, "addEventListener", "blur", ..., "top" ] | 禁止使用特定全局变量并提供替代建议 |
| no-shadow | error | - | 禁止变量声明覆盖外部作用域变量 |
| no-shadow-restricted-names | error | - | 禁止覆盖受限标识符(如undefined) |
| no-undef | error | - | 禁止使用未声明变量 |
| no-undef-init | error | - | 禁止初始化变量为undefined |
| no-undefined | off | - | 禁止使用undefined变量 |
| no-unused-vars | error | { vars: "all", args: "after-used", ignoreRestSiblings: true } | 禁止未使用变量,可配置检测范围 |
| no-use-before-define | error | { 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-style | error | "as-needed", { requireReturnForObjectLiteral: false } | 要求箭头函数体在可能的情况下省略大括号 |
| arrow-parens | error | "always" | 要求箭头函数参数始终使用括号 |
| arrow-spacing | error | { before: true, after: true } | 强制箭头函数的箭头前后空格一致性 |
| constructor-super | error | - | 禁止在构造函数中在调用 super() 之前使用 this/super |
| generator-star-spacing | error | { before: false, after: true } | 强制 generator 函数中星号周围空格的一致性 |
| no-class-assign | error | - | 禁止修改类声明 |
| no-confusing-arrow | error | { allowParens: true } | 禁止可能与比较操作符混淆的箭头函数语法 |
| no-const-assign | error | - | 禁止修改 const 声明的变量 |
| no-dupe-class-members | error | - | 禁止类成员中的重复名称 |
| no-duplicate-imports | off | - | 禁止重复模块导入(已禁用) |
| no-new-symbol | error | - | 禁止使用 new 操作符创建 Symbol 实例 |
| no-restricted-exports | error | { restrictedNamedExports: ["default", "then"] } | 限制指定的命名导出 |
| no-restricted-imports | off | { paths: [], patterns: [] } | 限制指定的模块导入(已禁用) |
| no-this-before-super | error | - | 禁止在构造函数中在 super() 调用前使用 this |
| no-useless-computed-key | error | - | 禁止不必要的计算属性键 |
| no-useless-constructor | error | - | 禁止不必要的构造函数 |
| no-useless-rename | error | { ignoreDestructuring: false, ignoreImport: false, ignoreExport: false } | 禁止不必要的重命名解构 |
| no-var | error | - | 要求使用 let 或 const 代替 var |
| object-shorthand | error | "always", { ignoreConstructors: false, avoidQuotes: true } | 强制对象字面量简写语法 |
| prefer-arrow-callback | error | { allowNamedFunctions: false, allowUnboundThis: true } | 要求回调函数使用箭头函数 |
| prefer-const | error | { destructuring: "any", ignoreReadBeforeAssign: true } | 要求使用 const 声明不会被重新赋值的变量 |
| prefer-destructuring | error | { VariableDeclarator: { array: false, object: true }, AssignmentExpression: { array: true, object: false } }, { enforceForRenamedProperties: false } | 强制使用解构赋值 |
| prefer-numeric-literals | error | - | 禁用 parseInt() 而使用二进制、八进制和十六进制字面量 |
| prefer-reflect | off | - | 要求使用 Reflect 方法(已禁用) |
| prefer-rest-params | error | - | 要求使用 rest 参数代替 arguments |
| prefer-spread | error | - | 要求使用扩展运算符代替 .apply() |
| prefer-template | error | - | 要求使用模板字面量代替字符串拼接 |
| require-yield | error | - | 要求 generator 函数内包含 yield 语句 |
| rest-spread-spacing | error | "never" | 强制剩余和扩展运算符周围空格的一致性 |
| sort-imports | off | { ignoreCase: false, ignoreDeclarationSort: false, ignoreMemberSort: false, memberSyntaxSortOrder: ["none", "all", "multiple", "single"] } | 强制导入声明排序(已禁用) |
| symbol-description | error | - | 要求 Symbol 描述参数 |
| template-curly-spacing | error | - | 强制模板字符串中花括号内的空格 |
| yield-star-spacing | error | "after" | 强制 yield* 表达式中星号周围空格 |
模块导入
| 规则名 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| import/no-unresolved | error | { commonjs: true, caseSensitive: true } | 确保导入的模块路径可解析 |
| import/named | error | - | 验证命名导出是否存在 |
| import/default | off | - | 关闭默认导出的验证 |
| import/namespace | off | - | 关闭命名空间导入验证 |
| import/export | error | - | 确保文件内所有导出语法有效 |
| import/no-named-as-default | error | - | 禁止将命名导出与默认导出混淆 |
| import/no-named-as-default-member | error | - | 禁止通过默认导出访问命名导出 |
| import/no-deprecated | off | - | 关闭对废弃模块的检查 |
| import/no-extraneous-dependencies | error | { devDependencies: [...], optionalDependencies: false } | 禁止引入无关依赖 |
| import/no-mutable-exports | error | - | 禁止导出可变变量 |
| import/no-commonjs | off | - | 关闭对 CommonJS 语法的限制 |
| import/no-amd | error | - | 禁止 AMD 语法 |
| import/no-nodejs-modules | off | - | 关闭对 Node.js 内置模块的限制 |
| import/first | error | - | 确保所有导入语句在模块顶部 |
| import/imports-first | off | - | 关闭“导入必须置顶”的旧规则 |
| import/no-duplicates | error | - | 禁止重复导入同一模块 |
| import/no-namespace | off | - | 关闭对命名空间导入的限制 |
| import/extensions | error | 'ignorePackages', { js: 'never', mjs: 'never', jsx: 'never' } | 强制文件扩展名规范 |
| import/order | error | { groups: [['builtin', 'external', 'internal']] } | 控制导入顺序和分组 |
| import/newline-after-import | error | - | 导入语句后需有空行 |
| import/prefer-default-export | error | - | 建议使用默认导出 |
| import/no-restricted-paths | off | - | 关闭对特定路径的限制 |
| import/max-dependencies | off | { max: 10 } | 限制单个文件的最大依赖数 |
| import/no-absolute-path | error | - | 禁止使用绝对路径导入 |
| import/no-dynamic-require | error | - | 禁止动态 require() 语法 |
| import/no-internal-modules | off | { allow: [] } | 禁止导入内部模块 |
| import/unambiguous | off | - | 关闭对非 ES 模块的检测 |
| import/no-webpack-loader-syntax | error | - | 禁止 Webpack 特有的加载语法 |
| import/no-unassigned-import | off | - | 禁止未赋值的导入 |
| import/no-named-default | error | - | 禁止命名默认导出别名 |
| import/no-anonymous-default-export | off | { allowArray: false, ... } | 禁止匿名默认导出 |
| import/exports-last | off | - | 确保导出语句在文件末尾 |
| import/group-exports | off | - | 强制分组导出语句 |
| import/no-default-export | off | - | 禁止默认导出 |
| import/no-named-export | off | - | 禁止命名导出 |
| import/no-self-import | error | - | 禁止模块导入自身 |
| import/no-cycle | error | { maxDepth: '∞' } | 禁止模块循环依赖 |
| import/no-useless-path-segments | error | { commonjs: true } | 禁止冗余路径片段 |
| import/dynamic-import-chunkname | off | { webpackChunknameFormat: '[0-9a-zA-Z-_/.]+' } | 控制动态导入的代码块命名 |
| import/no-relative-parent-imports | off | - | 禁止相对父级目录导入 |
| import/no-unused-modules | off | { missingExports: true, ... } | 检测未使用的模块 |
| import/no-import-module-exports | error | { exceptions: [] } | 禁止混合 import 和 module.exports |
| import/no-relative-packages | error | - | 禁止相对路径导入其他包 |
严格模式
| 规则名 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| strict | error | '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-regex | off | - | 强制正则表达式更加优化和简洁 |
| unicorn/catch-error-name | error | - | 强制错误变量在 catch 子句中命名为 error |
| unicorn/consistent-assert | error | - | 强制使用一致的断言方法(例如 assert.strictEqual 而不是 assert.equal) |
| unicorn/consistent-date-clone | error | - | 强制使用一致的方法来克隆日期(例如 new Date(date)) |
| unicorn/consistent-destructuring | off | - | 强制使用一致的结构分配方式 |
| unicorn/consistent-empty-array-spread | error | - | 强制空数组展开时使用一致的方式 |
| unicorn/consistent-existence-index-check | error | - | 强制使用一致的索引存在性检查(例如 index in array 而不是 array[index] !== undefined) |
| unicorn/consistent-function-scoping | error | - | 强制将没有依赖外部变量的函数提升到更高作用域 |
| unicorn/custom-error-definition | off | - | 强制自定义错误必须继承自 error |
| unicorn/empty-brace-spaces | error | - | 强制在花括号内使用一致的空格 |
| unicorn/error-message | error | - | 强制在 throw 语句中提供错误信息 |
| unicorn/escape-case | error | - | 强制转义序列使用大写或小写(例如 \n 而不是 \N) |
| unicorn/expiring-todo-comments | error | - | 强制 TODO 注释包含过期日期 |
| unicorn/explicit-length-check | error | - | 强制显式地检查长度(例如 array.length > 0 而不是 array.length) |
| unicorn/filename-case | error | - | 强制文件名使用特定的大小写风格(如驼峰、短横线等) |
| unicorn/import-style | error | - | 强制使用特定的导入风格(例如只使用默认导入或命名导入) |
| unicorn/new-for-builtins | error | - | 强制对内置对象使用 new(例如 new Error() 而不是 Error()) |
| unicorn/no-abusive-eslint-disable | error | - | 禁止滥用 eslint-disable 注释 |
| unicorn/no-accessor-recursion | error | - | 禁止在访问器中递归调用自身(会导致栈溢出) |
| unicorn/no-anonymous-default-export | error | - | 禁止匿名默认导出 |
| unicorn/no-array-callback-reference | error | - | 禁止在数组方法中传递回调函数的引用(应使用内联函数) |
| unicorn/no-array-for-each | error | - | 禁止使用 Array.prototype.forEach,推荐使用 for...of |
| unicorn/no-array-method-this-argument | error | - | 禁止在数组方法中使用 this 参数(应使用箭头函数) |
| unicorn/no-array-reduce | error | - | 尽量避免使用 Array.prototype.reduce,除非有特定需求 |
| unicorn/no-array-reverse | error | - | 禁止使用 Array.prototype.reverse,除非非常必要 |
| unicorn/no-array-sort | error | - | 禁止使用 Array.prototype.sort 而不带比较函数(对数字排序会出错) |
| unicorn/no-await-expression-member | error | - | 禁止在 await 表达式后直接使用成员表达式(可能导致意外行为) |
| unicorn/no-await-in-promise-methods | error | - | 禁止在 Promise 方法(如 then、catch)中使用 await |
| unicorn/no-console-spaces | error | - | 禁止在 console.log 等方法的参数中添加多余空格 |
| unicorn/no-document-cookie | error | - | 禁止直接使用 document.cookie,应使用封装好的方法 |
| unicorn/no-empty-file | error | - | 禁止空文件 |
| unicorn/no-for-loop | error | - | 禁止使用 for 循环,推荐使用高阶数组方法或 for...of |
| unicorn/no-hex-escape | error | - | 禁止在字符串中使用十六进制转义序列(应使用 Unicode 转义) |
| unicorn/no-instanceof-builtins | error | - | 禁止对内置类型(如 Array, error)使用 instanceof(在不同 realm 中可能失效) |
| unicorn/no-invalid-fetch-options | error | - | 禁止在 fetch 中使用无效的选项(例如 body 与 GET 一起用) |
| unicorn/no-invalid-remove-event-listener | error | - | 禁止无效的 removeEventListener(传递的函数必须与添加时相同) |
| unicorn/no-keyword-prefix | off | - | 禁止使用关键字作为变量名的前缀(例如 var classValue) |
| unicorn/no-lonely-if | error | - | 禁止孤独的 if 语句作为 else 块(应使用 else if) |
| unicorn/no-magic-array-flat-depth | error | - | 禁止在 Array.prototype.flat 中使用魔数(magic number)作为深度参数 |
| unicorn/no-named-default | error | - | 禁止导入命名默认导出(例如 import {default as foo} from 'bar') |
| unicorn/no-negated-condition | error | - | 禁止否定的条件(应反转 if 和 else 块来避免否定) |
| unicorn/no-negation-in-equality-check | error | - | 禁止在相等检查中使用否定(例如 !== 代替 !(==)) |
| unicorn/no-nested-ternary | error | - | 禁止嵌套的三元表达式 |
| unicorn/no-new-array | error | - | 禁止使用 new Array(),推荐使用字面量 [] |
| unicorn/no-new-buffer | error | - | 禁止使用 new Buffer()(出于安全考虑) |
| unicorn/no-null | error | - | 禁止使用 null,推荐使用 undefined |
| unicorn/no-object-as-default-parameter | error | - | 禁止使用对象作为默认参数(会导致共享引用问题) |
| unicorn/no-process-exit | error | - | 禁止使用 process.exit(),应让进程正常退出 |
| unicorn/no-single-promise-in-promise-methods | error | - | 禁止在 Promise.all 等方法中传递单个 Promise(无需包装) |
| unicorn/no-static-only-class | error | - | 禁止只有静态方法的类(应使用普通对象) |
| unicorn/no-thenable | error | - | 禁止使用 thenable 对象(非 Promise 但有 then 方法) |
| unicorn/no-this-assignment | error | - | 禁止将 this 赋值给变量(应使用箭头函数或绑定) |
| unicorn/no-typeof-undefined | error | - | 禁止 typeof undefined(总是返回 'undefined',是多余的) |
| unicorn/no-unnecessary-array-flat-depth | error | - | 禁止在 Array.prototype.flat 中使用不必要的深度参数(深度为 1 时可省略) |
| unicorn/no-unnecessary-array-splice-count | error | - | 禁止在 Array.prototype.splice 中使用不必要的计数参数(计数为 0 时可省略) |
| unicorn/no-unnecessary-await | error | - | 禁止不必要的 await(在非 Promise 前或可并行操作时) |
| unicorn/no-unnecessary-polyfills | error | - | 禁止不必要的 polyfills(针对现代浏览器或环境) |
| unicorn/no-unnecessary-slice-end | error | - | 禁止在 Array.prototype.slice 中使用不必要的结束参数(例如 array.length, Infinity) |
| unicorn/no-unreadable-array-destructuring | error | - | 禁止难以阅读的数组解构(例如过深的嵌套) |
| unicorn/no-unreadable-iife | error | - | 禁止难以阅读的立即调用函数表达式(IIFE) |
| unicorn/no-unused-properties | off | - | 禁止未使用的对象属性 |
| unicorn/no-useless-error-capture-stack-trace | error | - | 禁止无用的错误堆栈捕获(例如在 Error.captureStackTrace 中传递自身) |
| unicorn/no-useless-fallback-in-spread | error | - | 禁止在展开运算符中使用无用的回退 |
| unicorn/no-useless-length-check | error | - | 禁止无用的长度检查(例如 array.length === 0 检查空数组) |
| unicorn/no-useless-promise-resolve-reject | error | - | 禁止在 Promise 中无使用 resolve 或 reject(例如 new Promise((resolve) => resolve())) |
| unicorn/no-useless-spread | error | - | 禁止无用的展开运算符(例如 [...array] 当 array 已是数组时) |
| unicorn/no-useless-switch-case | error | - | 禁止无用的 switch case(例如多个 case 执行相同操作且无 break) |
| unicorn/no-useless-undefined | error | - | 禁止返回无用的 undefined(函数默认返回 undefined) |
| unicorn/no-zero-fractions | error | - | 禁止数字中无用的小数部分(例如 1.0 应写为 1) |
| unicorn/number-literal-case | error | - | 强制数字字面量使用小写字母(例如 0x10 而不是 0X10) |
| unicorn/numeric-separators-style | error | - | 强制数值分隔符使用一致的风格(例如 1_000_000) |
| unicorn/prefer-add-event-listener | error | - | 强制使用 addEventListener 而不是 onclick 等属性 |
| unicorn/prefer-array-find | error | - | 强制使用 Array.prototype.find 来查找数组元素 |
| unicorn/prefer-array-flat | error | - | 强制使用 Array.prototype.flat 而不是递归展开或其它方法 |
| unicorn/prefer-array-flat-map | error | - | 强制使用 Array.prototype.flatMap 而不是 map 后接 flat |
| unicorn/prefer-array-index-of | error | - | 强制使用 Array.prototype.indexOf 而不是循环查找索引 |
| unicorn/prefer-array-some | error | - | 强制使用 Array.prototype.some 而不是循环检查存在性 |
| unicorn/prefer-at | error | - | 强制使用 .at() 方法进行负索引访问(例如 array[-1] 不行,但 array.at(-1) 可以) |
| unicorn/prefer-bigint-literals | error | - | 强制使用 BigInt 字面量(例如 1n)而不是 BigInt(1) |
| unicorn/prefer-blob-reading-methods | error | - | 强制使用 Blob 的读取方法(例如 text(), arrayBuffer())而不是 FileReader |
| unicorn/prefer-class-fields | error | - | 强制使用类字段语法而不是在构造函数中赋值 |
| unicorn/prefer-classlist-toggle | error | - | 强制使用 classList.toggle 的第二个参数(强制添加/移除)而不是条件判断 |
| unicorn/prefer-code-point | error | - | 强制使用 String.prototype.codePointAt 和 String.fromCodePoint 来处理 Unicode |
| unicorn/prefer-date-now | error | - | 强制使用 Date.now() 而不是 new Date().getTime() |
| unicorn/prefer-default-parameters | error | - | 强制使用默认参数而不是在函数体内设置默认值 |
| unicorn/prefer-dom-node-append | error | - | 强制使用 Node.append 而不是 Node.appendChild(支持多个参数) |
| unicorn/prefer-dom-node-dataset | error | - | 强制使用 dataset 来访问和设置 data-* 属性 |
| unicorn/prefer-dom-node-remove | error | - | 强制使用 ChildNode.remove 而不是 parentNode.removeChild |
| unicorn/prefer-dom-node-text-content | error | - | 强制使用 Node.textContent 而不是 Node.innerText(性能更好,更可预测) |
| unicorn/prefer-event-target | error | - | 强制使用 EventTarget 而不是 EventEmitter(用于自定义事件) |
| unicorn/prefer-export-from | error | - | 强制使用 export {foo} from 'bar' 而不是先导入再导出 |
| unicorn/prefer-global-this | error | - | 强制使用 globalThis 来访问全局对象(而不是 window, global 等) |
| unicorn/prefer-import-meta-properties | off | - | 强制使用 import.meta 对象的属性(如 import.meta.url)而不是传统的替代方案 |
| unicorn/prefer-includes | error | - | 强制使用 String.prototype.includes 而不是 indexOf !== -1 |
| unicorn/prefer-json-parse-buffer | off | - | 强制使用 JSON.parse 时传入 Buffer 而不是字符串(性能考虑) |
| unicorn/prefer-keyboard-event-key | error | - | 强制使用 KeyboardEvent.key 而不是 keyCode 或 which(已废弃) |
| unicorn/prefer-logical-operator-over-ternary | error | - | 强制使用逻辑运算符(&&, ` |
| unicorn/prefer-math-min-max | error | - | 强制使用 Math.min 和 Math.max 而不是手写条件判断 |
| unicorn/prefer-math-trunc | error | - | 强制使用 Math.trunc 来取整而不是 bitwise operators 或其它方法 |
| unicorn/prefer-modern-dom-apis | error | - | 强制使用现代 DOM API(例如 DocumentFragment 而不是 innerHTML) |
| unicorn/prefer-modern-math-apis | error | - | 强制使用现代的 Math API(例如 Math.clz32) |
| unicorn/prefer-module | error | - | 强制使用 ES 模块(import/export)而不是 CommonJS(require/module.exports) |
| unicorn/prefer-native-coercion-functions | error | - | 强制使用原生类型转换函数(例如 String(x), Number(x))而不是 x.toString(), +x |
| unicorn/prefer-negative-index | error | - | 强制使用负索引来从数组末尾访问元素(结合 .at() 或 slice) |
| unicorn/prefer-node-protocol | error | - | 强制在导入 Node.js 内置模块时使用 node: 协议(例如 import fs from 'node:fs') |
| unicorn/prefer-number-properties | error | - | 强制使用 Number 上的属性(例如 Number.isNaN 而不是 isNaN) |
| unicorn/prefer-object-from-entries | error | - | 强制使用 Object.fromEntries 来将键值对列表转换为对象 |
| unicorn/prefer-optional-catch-binding | error | - | 强制在 catch 子句中省略不必要的参数(当不需要错误对象时) |
| unicorn/prefer-prototype-methods | error | - | 强制在原型上调用方法而不是在实例上(例如 Array.prototype.slice.call) |
| unicorn/prefer-query-selector | error | - | 强制使用 document.querySelector 和 document.querySelectorAll 而不是 getElementById, getElementsByClassName 等 |
| unicorn/prefer-reflect-apply | error | - | 强制使用 Reflect.apply 而不是 Function.prototype.apply |
| unicorn/prefer-regexp-test | error | - | 强制使用 RegExp.prototype.test 而不是 String.prototype.match 来检查匹配 |
| unicorn/prefer-set-has | error | - | 强制使用 Set.prototype.has 来检查 Set 中是否存在某个值(而不是数组的 includes) |
| unicorn/prefer-set-size | error | - | 强制使用 Set.prototype.size 而不是将 Set 转换为数组再取长度 |
| unicorn/prefer-single-call | error | - | 强制合并多次数组方法调用为单次调用(例如多次 push 改为一次 push 多个元素) |
| unicorn/prefer-spread | error | - | 强制使用展开运算符(...)而不是 Function.prototype.apply 来传递数组参数 |
| unicorn/prefer-string-raw | error | - | 强制使用 String.raw 来获取模板字符串的原始字符串 |
| unicorn/prefer-string-replace-all | error | - | 强制使用 String.prototype.replaceAll 而不是正则表达式带 g 标志的 replace |
| unicorn/prefer-string-slice | error | - | 强制使用 String.prototype.slice 而不是 substring 或 substr(行为更一致) |
| unicorn/prefer-string-starts-ends-with | error | - | 强制使用 String.prototype.startsWith 和 endsWith 而不是 indexOf 或正则表达式 |
| unicorn/prefer-string-trim-start-end | error | - | 强制使用 String.prototype.trimStart 和 trimEnd 而不是 trimLeft 和 trimRight(标准名称) |
| unicorn/prefer-structured-clone | error | - | 强制使用 structuredClone 而不是其他深度克隆方法(如 JSON.parse(JSON.stringify())) |
| unicorn/prefer-switch | error | - | 强制使用 switch 语句而不是多个 if-else if 语句 |
| unicorn/prefer-ternary | error | - | 强制使用三元运算符而不是简单的 if-else 语句 |
| unicorn/prefer-top-level-await | error | - | 强制在模块顶层使用 await 而不是包裹在 async 函数中(需在 ES 模块中) |
| unicorn/prefer-type-error | error | - | 强制抛出 TypeError 而不是 error 用于类型错误 |
| unicorn/prevent-abbreviations | error | - | 防止使用缩写(例如 e 代替 event, cb 代替 callback) |
| unicorn/relative-url-style | error | - | 强制相对 URL 使用一致的风格(例如 ./ 开头或不开头) |
| unicorn/require-array-join-separator | error | - | 强制 Array.prototype.join 方法提供分隔符参数(显式传递空字符串如果需要) |
| unicorn/require-module-attributes | error | - | 强制导入模块时声明属性(例如 import json from './data.json' assert {type: 'json'};) |
| unicorn/require-module-specifiers | error | - | 强制模块标识符使用特定风格(例如相对路径还是绝对路径) |
| unicorn/require-number-to-fixed-digits-argument | error | - | 强制 Number.prototype.toFixed 提供参数(避免浏览器差异) |
| unicorn/require-post-message-target-origin | off | - | 强制 window.postMessage 提供 targetOrigin 参数(安全考虑) |
| unicorn/string-content | off | - | 强制字符串内容使用特定风格(例如禁止或要求使用某些字符) |
| unicorn/switch-case-braces | error | - | 强制 switch case 使用大括号 |
| unicorn/template-indent | error | - | 强制模板字符串的缩进保持一致 |
| unicorn/text-encoding-identifier-case | error | - | 强制文本编码标识符使用特定的大小写(例如 'utf-8' 而不是 'utf8') |
| unicorn/throw-new-error | error | - | 强制抛出错误时使用 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-return | error | - | 要求在 Promise 链中的 then() 方法里必须返回一个值,以确保函数有返回值,避免意外错误 |
| promise/no-return-wrap | error | - | 禁止不必要的包装返回值,例如直接返回一个Promise或使用Promise.resolve/Promise.reject包装值,应直接返回值或错误 |
| promise/param-names | error | - | 要求 Promise 的 executor 函数的参数名必须为 resolve 和 reject,以防止参数名拼写错误导致的问题 |
| promise/catch-or-return | error | allowFinally, allowThen, terminationMethod | 要求Promise必须使用catch()方法处理错误或被返回以便在其他地方处理,以避免未处理的Promise拒绝 |
| promise/no-native | off | - | 禁止使用原生的 Promise 全局对象,强制使用如 bluebird 这样的库。通常在你不使用原生 Promise 时开启 |
| promise/no-nesting | off | - | 尽可能禁止不必要的 Promise 嵌套,建议使用 async/await 或扁平化 Promise 链来简化代码 |
| promise/no-promise-in-callback | off | - | 警告在回调函数中返回 Promise 的情况,这可能导致 Promise 被忽略且错误未被处理,常见于类似 then() 的回调中 |
| promise/no-callback-in-promise | off | - | 不建议在 Promise 中使用回调函数模式(即 done、next 等参数),因为这可能绕过 Promise 的错误捕获机制 |
| promise/avoid-new | off | - | 禁止使用 new Promise 构造函数,建议在可能的情况下使用Promise 的工厂函数或 async 函数 |
| promise/no-new-statics | error | - | 禁止调用 Promise 的静态方法(如Promise.resolve、Promise.reject)时使用 new 操作符(例如 new Promise.resolve(...) ),这是不必要的 |
| promise/no-return-in-finally | off | - | 禁止在 Promise.prototype.finally() 方法中返回值,因为 finally 处理程序中的返回值会被忽略,不会影响 Promise 的最终状态 |
| promise/valid-params | off | - | 确保 Promise 的静态方法(如 all, race, allSettled, any )接收的参数是有效的(例如,是一个可迭代对象) |
RegExp
eslint-plugin-regexp 旨在帮助你编写更安全、更高效的正则表达式。它会检查正则表达式中的常见问题。
// .eslintrc.js
module.exports = {
extends: [
'plugin:regexp/recommended',
],
};
JSDoc
eslint-plugin-jsdoc 是针对 JSDoc 注释的规则集,专门为 JavaScript/TypeScript 设计。它强制要求函数、类等代码结构有清晰的文档注释,并检查注释的格式和类型信息是否与代码一致,从而提高代码的可维护性和文档化水平。
// .eslintrc.js
module.exports = {
extends: [
'plugin:jsdoc/recommended-typescript',
],
};
| 规则名称 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| jsdoc/check-access | warn | - | 检查 JSDoc 注释中 @access 标签的使用是否正确 |
| jsdoc/check-alignment | warn | - | 检查 JSDoc 注释中星号 (*) 的缩进和对齐方式 |
| jsdoc/check-examples | off | - | 检查 @example 标签中的代码片段是否符合语法规范 |
| jsdoc/check-indentation | off | - | 检查 JSDoc 注释内部的缩进是否一致 |
| jsdoc/check-line-alignment | off | - | 检查 JSDoc 注释标签是否对齐 |
| jsdoc/check-param-names | warn | - | 检查 @param 标签指定的参数名称是否与函数定义中的参数名称匹配 |
| jsdoc/check-property-names | warn | - | 检查 @property 标签指定的属性名称是否与对象定义中的属性名称匹配 |
| jsdoc/check-syntax | off | - | 检查 JSDoc 注释中是否存在基本的语法错误 |
| jsdoc/check-template-names | off | - | 检查 JSDoc 注释中使用的模板标签名称(如 @template)是否有效 |
| jsdoc/check-types | warn | - | 检查 JSDoc 注释中类型注释(如 {string}, {number})的语法和有效性 |
| jsdoc/check-values | warn | - | 检查 JSDoc 注释中特定标签(如 @version, @since)的值是否符合预期格式 |
| jsdoc/convert-to-jsdoc-comments | off | - | 尝试将普通的多行注释 (/* ... */) 转换为 JSDoc 注释 (/** ... */) |
| jsdoc/empty-tags | warn | - | 检查 JSDoc 注释中不应包含内容的标签(如 @abstract, @async)后面是否确实没有内容 |
| jsdoc/implements-on-classes | warn | - | 检查 @implements 标签是否只用于类(Class)上 |
| jsdoc/imports-as-dependencies | off | - | 检查导入语句是否被正确标记为依赖 |
| jsdoc/informative-docs | off | - | 鼓励或要求 JSDoc 注释提供更具信息性的描述 |
| jsdoc/lines-before-block | off | - | 强制或禁止在 JSDoc 注释块之前有空行 |
| jsdoc/match-description | off | - | 强制 JSDoc 注释的描述部分符合特定的正则表达式模式 |
| jsdoc/match-name | off | - | 强制 JSDoc 注释所附着的代码元素的名称符合特定的正则表达式模式 |
| jsdoc/multiline-blocks | warn | - | 强制多行 JSDoc 注释的特定格式 |
| jsdoc/no-bad-blocks | off | - | 禁止在注释中使用可能被误解析为 JSDoc 注释的特定模式或标签 |
| jsdoc/no-blank-block-descriptions | off | - | 禁止 JSDoc 注释块的顶部的描述部分为空 |
| jsdoc/no-blank-blocks | off | - | 禁止出现完全空白的 JSDoc 注释块 |
| jsdoc/no-defaults | warn | - | 禁止在 @param 或 @property 标签中指定默认值 |
| jsdoc/no-missing-syntax | off | - | 检查代码中是否缺少预期的 JSDoc 注释 |
| jsdoc/no-multi-asterisks | warn | - | 禁止在 JSDoc 注释中使用多个连续的星号(**) |
| jsdoc/no-restricted-syntax | off | - | 允许定义并禁止使用特定的 JSDoc 标签或语法模式 |
| jsdoc/reject-any-type | warn | - | 禁止在 JSDoc 类型注释中使用 any 或 * 这种不明确的类型 |
| jsdoc/reject-function-type | warn | - | 禁止在 JSDoc 类型注释中使用 Function 这种不明确的函数类型 |
| jsdoc/require-asterisk-prefix | off | - | 强制 JSDoc 注释中除第一行和最后一行外,每一行都以一个星号 (*) 开头 |
| jsdoc/require-description | off | - | 强制 JSDoc 注释必须包含描述文本(在标签之前) |
| jsdoc/require-description-complete-sentence | off | - | 强制 JSDoc 注释中的描述部分必须是一个完整的句子 |
| jsdoc/require-example | off | - | 强制要求 JSDoc 注释中包含 @example 标签 |
| jsdoc/require-file-overview | off | - | 强制在每个文件顶部有一个特定的 JSDoc 注释,用于提供文件概述 |
| jsdoc/require-hyphen-before-param-description | off | - | 强制在 @param 标签的描述文本前必须有一个连字符(-)或空格等 |
| jsdoc/require-jsdoc | warn | - | 强制为特定的代码结构(如函数、类、方法)添加 JSDoc 注释 |
| jsdoc/require-next-type | warn | - | 检查迭代器next方法的返回类型 |
| jsdoc/require-param | warn | - | 强制为函数声明或表达式中的每一个参数添加 @param 标签 |
| jsdoc/require-param-description | warn | - | 强制每一个 @param 标签都必须包含对该参数的描述 |
| jsdoc/require-param-name | warn | - | 强制每一个 @param 标签都必须指定参数的名称 |
| jsdoc/require-property | warn | - | 强制为使用 @typedef 定义的类型的每一个属性在 JSDoc 注释中添加 @property 标签 |
| jsdoc/require-property-description | warn | - | 强制每一个 @property 标签都必须包含对该属性的描述 |
| jsdoc/require-property-name | warn | - | 强制每一个 @property 标签都必须指定属性的名称 |
| jsdoc/require-returns | warn | - | 强制为有返回值的函数(非 void)添加 @returns 标签 |
| jsdoc/require-returns-check | warn | - | 强制如果函数返回一个值(非 void),则必须存在 @returns 标签 |
| jsdoc/require-returns-description | warn | - | 强制 @returns 标签必须包含对返回值的描述 |
| jsdoc/require-template | off | - | 强制使用模板标签 |
| jsdoc/require-throws | off | - | 强制为函数中可能抛出错误的情况添加 @throws 标签 |
| jsdoc/require-throws-type | warn | - | 强制 @throws 标签必须指定所抛出错误的类型 |
| jsdoc/require-yields | warn | - | 强制生成器函数或使用 yield 的函数添加 @yields 标签 |
| jsdoc/require-yields-check | warn | - | 强制如果函数中有 yield 语句,则必须存在 @yields 标签 |
| jsdoc/require-yields-type | warn | - | 强制 @yields 标签必须指定产出值的类型 |
| jsdoc/sort-tags | off | - | 强制 JSDoc 注释中的标签按特定的顺序排列 |
| jsdoc/tag-lines | warn | - | 强制规定每个 JSDoc 标签之前或之后应有多少空行 |
| jsdoc/text-escaping | off | - | 检查文本转义是否正确 |
| jsdoc/type-formatting | off | - | 强制 JSDoc 类型注释的格式 |
| jsdoc/valid-types | warn | - | 验证 JSDoc 注释中特定标签(如 @typedef, @callback)后的类型声明是否符合语法规范 |
| jsdoc/check-tag-names | warn | {typed: true} | 检查 JSDoc 注释中所有标签的名称是否拼写正确且是已知的标准标签或配置允许的标签 |
| jsdoc/no-types | warn | - | 禁止在 JSDoc 注释中使用类型注解(如 {string}),鼓励使用 TypeScript 或其他类型系统 |
| jsdoc/no-undefined-types | off | - | 检查 JSDoc 注释中使用的类型名称是否已定义或是已知的类型(如内置类型、全局类型或导入的类型) |
| jsdoc/require-param-type | off | - | 强制每一个 @param 标签都必须指定参数的类型 |
| jsdoc/require-property-type | off | - | 强制每一个 @property 标签都必须指定属性的类型 |
| jsdoc/require-returns-type | off | - | 强制 @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-pair | error | - | 要求 /* eslint-disable */ 注释必须有一个对应的 /* eslint-enable */ 注释成对出现,防止规则被意外地全局禁用 |
| @eslint-community/eslint-comments/no-aggregating-enable | error | - | 禁止使用一个 /* eslint-enable */ 注释来同时启用多个之前被禁用的规则,要求逐一明确启用 |
| @eslint-community/eslint-comments/no-duplicate-disable | error | - | 禁止对同一个规则进行重复的禁用注释,避免冗余 |
| @eslint-community/eslint-comments/no-unlimited-disable | error | - | 禁止不使用任何规则名参数的 /* eslint-disable */ 注释(即禁用所有规则),要求禁用注释必须明确指定要禁用的规则列表,防止无意中关闭所有检查 |
| @eslint-community/eslint-comments/no-unused-enable | error | - | 禁止存在没有对应禁用注释的 /* eslint-enable */ 注释,确保启用的有效性 |