sindresorhus/eslint-plugin-unicorn · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈
eslint-plugin-unicorn

超过 300 条强大的 ESLint 规则
大多数规则针对 JavaScript 和 TypeScript,但在使用相应的 ESLint 语言插件时,部分规则也可用于检查 CSS、HTML、JSON 和 Markdown。
我们不接受拉取请求,因为 AI 生成的垃圾内容太多。
安装
npm install --save-dev eslint eslint-plugin-unicorn
需要 ESLint >=10.4、flat config 以及 ESM。
您可能想查看 XO,其中包含此插件。
用法
使用 preset config 或在 eslint.config.js 中配置每条规则。
如果您不使用预设,请设置与下方相同的 languageOptions。
import unicorn from 'eslint-plugin-unicorn';
import {defineConfig} from 'eslint/config';
import globals from 'globals';
export default defineConfig([
{
files: ['**/*.js'],
languageOptions: {
globals: globals.builtin,
},
plugins: {
unicorn,
},
rules: {
'unicorn/prefer-module': 'error',
'unicorn/…': 'error',
},
},
// …
]);
对于 TypeScript,将 Unicorn 的作用范围限定为 TypeScript 文件,并为同一配置对象配置一个 TypeScript 解析器:
import typescriptEslintParser from '@typescript-eslint/parser';
import unicorn from 'eslint-plugin-unicorn';
import {defineConfig} from 'eslint/config';
import globals from 'globals';
export default defineConfig([
{
files: ['**/*.ts'],
languageOptions: {
globals: globals.builtin,
parser: typescriptEslintParser,
},
plugins: {
unicorn,
},
rules: {
'unicorn/prefer-module': 'error',
'unicorn/…': 'error',
},
},
// …
]);
规则
💼 已启用 Configurations]。
✅ 已在 recommended configuration] 中设置。
☑️ 已在 unopinionated configuration] 中设置。
🔧 可通过 --fix CLI 选项] 自动修复。
💡 可通过 编辑器建议] 手动修复。
💭 需要 类型信息]。
| Name | Description | 💼 | 🔧 | 💡 | 💭 |
|---|---|---|---|---|---|
| better-dom-traversing | 优先使用更好的 DOM 遍历 API。 | ✅ ☑️ | 💡 | ||
| catch-error-name | 在 catch 子句中强制使用特定的参数名。 | ✅ | 🔧 | ||
| class-reference-in-static-methods | 在静态方法中强制使用一致的类引用。 | ✅ | 💡 | ||
| comment-content | 强制使用更好的注释内容。 | 🔧 | |||
| consistent-arrow-return-style | 强制多行箭头函数体使用一致的返回风格。 | 🔧 | |||
| consistent-assert | 强制与 node:assert 保持一致的断言风格。 | ✅ | 🔧 | ||
| consistent-boolean-name | 强制布尔值命名保持一致。 | ✅ | 🔧 | 💡 | |
| consistent-class-member-order | 强制类成员顺序保持一致。 | ✅ | 💡 | ||
| consistent-compound-words | 强制标识符中复合词的拼写保持一致。 | ✅ ☑️ | 💡 | ||
| consistent-conditional-object-spread | 强制条件对象展开风格保持一致。 | ✅ | 🔧 | ||
| consistent-date-clone | 克隆时,建议直接将 Date 传递给构造函数。 | ✅ ☑️ | 🔧 | ||
| consistent-destructuring | 使用解构变量而非属性。 | 💡 | |||
| consistent-empty-array-spread | 在数组字面量中展开三元表达式时,建议保持一致的类型。 | ✅ | 🔧 | ||
| consistent-existence-index-check | 强制使用一致的样式进行元素存在性检查,涉及 indexOf()、lastIndexOf()、findIndex() 和 findLastIndex()。 | ✅ ☑️ | 🔧 | ||
| consistent-export-decorator-position | 强制导出类上的装饰器位置保持一致。 | ✅ ☑️ | 🔧 | ||
| consistent-function-scoping | 将函数定义移至尽可能高的作用域。 | ✅ | |||
| consistent-function-style | 按角色强制使用函数语法。 | 💡 | |||
| consistent-json-file-read | 在 JSON.parse() 之前强制使用一致的 JSON 文件读取方式。 | ✅ | 🔧 | ||
| consistent-optional-chaining | 对同一基对象的成员访问强制使用一致的可选链。 | ✅ ☑️ | 💡 | ||
| consistent-template-literal-escape | 强制在模板字符串中对 ${ 使用一致的转义风格。 | ✅ | 🔧 | ||
| consistent-tuple-labels | 强制为元组类型元素使用一致的标签。 | ✅ | |||
| custom-error-definition | 强制正确继承 Error。 | 🔧 | |||
| default-export-style | 强制一致的默认导出声明。 | ✅ | 🔧 | 💡 | |
| dom-node-dataset | 强制一致的 DOM 元素 dataset 访问样式。 | ✅ ☑️ | 🔧 | ||
| empty-brace-spaces | 强制花括号之间无空格。 | ✅ | 🔧 | ||
| error-message | 强制在创建内置错误时传递 message 值。 | ✅ ☑️ | |||
| escape-case | 要求转义序列使用大写或小写值。 | ✅ ☑️ | 🔧 | ||
| expiring-todo-comments | 为 TODO 注释添加过期条件。 | ✅ ☑️ | |||
| explicit-length-check | 强制显式比较值的 length 或 size 属性。 | ✅ | 🔧 | 💡 | |
| explicit-timer-delay | 强制或禁止为 setTimeout() 和 setInterval() 使用显式的 delay 参数。 | ✅ ☑️ | 🔧 | ||
| filename-case | 强制文件名和目录名的命名大小写风格。 | ✅ | |||
| id-match | 要求标识符匹配指定的正则表达式。 | ||||
| import-style | 强制每个模块使用特定的导入风格。 | ✅ ☑️ | |||
| isolated-functions | 防止在隔离函数作用域外使用变量。 | ✅ | |||
| iteration-fallback-style | 强制为可选循环源保持一致的风格。 | 🔧 | |||
| logical-assignment-operators | 要求或禁止使用逻辑赋值运算符简写 | ✅ | 🔧 | 💡 | |
| max-nested-calls | 限制嵌套调用的深度。 | ✅ | |||
| name-replacements | 强制替换变量、属性和文件名。 | ✅ | 🔧 | 💡 | |
| new-for-builtins | 强制正确使用 new 用于内置构造函数。 | ✅ ☑️ | 🔧 | 💡 | |
| no-abusive-eslint-disable | 强制在 eslint-disable 注释中指定要禁用的规则。 | ✅ ☑️ | |||
| no-accessor-recursion | 禁止在 getter 和 setter 中递归访问 this。 | ✅ ☑️ | |||
| no-accidental-bitwise-operator | 禁止在可能意图使用逻辑运算符的地方使用按位运算符。 | ✅ ☑️ | 💡 | ||
| no-anonymous-default-export | 禁止将匿名函数和类作为默认导出。 | ✅ ☑️ | 💡 | ||
| no-array-callback-reference | 防止将函数引用直接传递给迭代器方法。 | ✅ | 💡 | ||
| no-array-concat-in-loop | 禁止在循环中使用 Array#concat() 进行数组累积。 | ✅ | |||
| no-array-fill-with-reference-type | 禁止使用引用值作为 Array#fill() 的值。 | ✅ ☑️ | |||
| no-array-from-fill | 禁止在 .fill() 之后使用 Array.from({length: …})。 | ✅ ☑️ | |||
| no-array-front-mutation | 禁止对数组前端进行变更。 | ||||
| no-array-method-this-argument | 禁止在数组方法中使用 this 参数。 | ✅ ☑️ | 🔧 | 💡 | |
| no-array-reduce | 禁止使用 Array#reduce() 和 Array#reduceRight()。 | ✅ | 🔧 | 💡 | |
| no-array-reverse | 优先使用 Array#toReversed() 而非 Array#reverse()。 | ✅ ☑️ | 💡 | ||
| no-array-sort | 优先使用 Array#toSorted() 而非 Array#sort()。 | ✅ ☑️ | 💡 | ||
| no-array-sort-for-min-max | 禁止通过排序数组来获取最小值或最大值。 | ✅ ☑️ | 💡 | ||
| no-array-splice | 优先使用 Array#toSpliced() 而非 Array#splice()。 | ✅ | 💡 | ||
| no-asterisk-prefix-in-documentation-comments | 禁止在文档注释中使用星号前缀。 | 🔧 | |||
| no-async-promise-finally | 禁止将 async 函数用作 Promise#finally() 回调。 | ✅ ☑️ | |||
| no-await-expression-member | 禁止从 await 表达式中访问成员。 | ✅ | 🔧 | ||
| no-await-in-promise-methods | 禁止在 Promise 方法参数中使用 await。 | ✅ ☑️ | 💡 | ||
| no-barrel-files | 禁止使用 barrel 文件。 | ||||
| no-blob-to-file | 禁止不必要的 Blob 到 File 转换。 | ✅ ☑️ | 💡 | ||
| no-boolean-sort-comparator | 禁止返回布尔值的排序比较器。 | ✅ ☑️ | 💡 | ||
| no-break-in-nested-loop | 禁止在嵌套循环以及循环内的 switch 语句中使用 break 和 continue。 | ✅ | |||
| no-canvas-to-image | 建议直接绘制画布,而不是将其转换为图像。 | ✅ ☑️ | |||
| no-chained-comparison | 禁止使用如 a < b < c 这样的链式比较。 | ✅ ☑️ | 💡 | ||
| no-collection-bracket-access | 禁止使用括号表示法访问 Map、Set、WeakMap 和 WeakSet 的条目。 | ✅ ☑️ | 💡 | ||
| no-computed-property-existence-check | 禁止动态对象属性存在性检查。 | ✅ | 💡 | ||
| no-confusing-array-splice | 禁止令人困惑的 Array#{splice,toSpliced}() 用法。 | ✅ | 💡 | ||
| no-confusing-array-with | 禁止令人困惑的 Array#with() 用法。 | ✅ | |||
| no-console-spaces | 不要在 console.log 参数之间使用前导/尾随空格。 | ✅ ☑️ | 🔧 | ||
| no-constant-zero-expression | 禁止始终求值为 0 的算术和位运算。 | ✅ ☑️ | 💡 | ||
| no-declarations-before-early-exit | 禁止在条件提前退出之前声明仅在退出后使用的变量。 | ✅ ☑️ | 🔧 | ||
| no-document-cookie | 不要直接使用 document.cookie。 | ✅ ☑️ | |||
| no-double-comparison | 禁止对相同操作数进行可以合并为一次的两次比较。 | ✅ ☑️ | 💡 | ||
| no-duplicate-if-branches | 禁止 if 链中相邻的重复分支。 | ✅ | |||
| no-duplicate-logical-operands | 禁止逻辑表达式中相邻的重复操作数。 | ✅ ☑️ | 🔧 | 💡 | |
| no-duplicate-loops | 禁止在 for…of 和 for await…of 循环头部使用 .map() 和 .filter()。 | ✅ | |||
| no-duplicate-set-values | 禁止在 Set 构造函数的数组字面量中使用重复的值。 | ✅ | |||
| no-empty-file | 禁止空文件。 | ✅ ☑️ | |||
| no-error-property-assignment | 禁止对内置错误属性进行赋值。 | ✅ ☑️ | |||
| no-exports-in-scripts | 禁止在脚本中使用 exports。 | ✅ ☑️ | |||
| no-for-each | 优先使用 for…of 而非 forEach 方法。 | ✅ ☑️ | 🔧 | 💡 | |
| no-for-loop | 不要使用可以用 for-of 循环替代的 for 循环。 | ✅ | 🔧 | ||
| no-global-object-property-assignment | 禁止对全局对象上的属性进行赋值。 | ✅ ☑️ | |||
| no-immediate-mutation | 禁止在变量赋值后立即进行变更。 | ✅ | 🔧 | 💡 | |
| no-impossible-length-comparison | 禁止与 .length 或 .size 进行不可能的比较。 | ✅ ☑️ | |||
| no-incorrect-query-selector | 禁止错误地使用 querySelector() 和 querySelectorAll()。 | ✅ | 🔧 | ||
| no-incorrect-template-string-interpolation | 禁止使用错误的模板字符串插值语法。 | ✅ | 💡 | ||
| no-instanceof-builtins | 禁止对内置对象使用 instanceof | ✅ ☑️ | 🔧 | 💡 | |
| no-invalid-argument-count | 禁止以无效的参数数量调用函数和构造函数。 | ✅ ☑️ | |||
| no-invalid-character-comparison | 禁止将字符串中的单个字符与多字符字符串进行比较。 | ✅ ☑️ | |||
| no-invalid-fetch-options | 禁止在 fetch() 和 new Request() 中使用无效选项。 | ✅ ☑️ | |||
| no-invalid-file-input-accept | 禁止在文件输入中使用无效的 accept 值。 | 🔧 | |||
| no-invalid-remove-event-listener | 防止使用表达式的结果调用 EventTarget#removeEventListener()。 | ✅ ☑️ | |||
| no-invalid-well-known-symbol-methods | 禁止无效的内部符号方法实现。 | ✅ ☑️ | 🔧 | 💡 | |
| no-keyword-prefix | 禁止以 new 或 class 开头的标识符。 | ||||
| no-late-current-target-access | 禁止在同步事件分发结束后访问 event.currentTarget。 | ✅ | |||
| no-late-event-control | 禁止在同步事件分发结束后调用事件控制方法。 | ✅ | |||
| no-lonely-if | 禁止在不含 else 的 if 块中,将 if 语句作为唯一语句。 | ✅ ☑️ | 🔧 | ||
| no-loop-iterable-mutation | 禁止在迭代过程中修改循环的可迭代对象。 | ✅ | |||
| no-magic-array-flat-depth | 禁止在 Array#flat(…). 中将魔法数字作为 depth 参数 | ✅ ☑️ | |||
| no-manually-wrapped-comments | 禁止手动换行的注释。 | 🔧 | |||
| no-mismatched-map-key | 禁止在访问不同的键之前检查 Map 的键。 | ✅ | |||
| no-misrefactored-assignment | 禁止在复合赋值中,当目标在右侧重复出现时进行错误重构。 | ✅ ☑️ | 💡 | ||
| no-missing-local-resource | 禁止引用缺失的本地资源。 | 🔧 | |||
| no-multiple-promise-resolver-calls | 禁止在同一执行路径上多次调用 Promise 执行器解析函数。 | ✅ ☑️ | |||
| no-named-default | 禁止以命名方式使用默认导入和导出。 | ✅ ☑️ | 🔧 | ||
| no-negated-array-predicate | 禁止使用取反的数组谓词调用。 | ✅ ☑️ | 🔧 | ||
| no-negated-comparison | 禁止使用取反的比较。 | ✅ ☑️ | 🔧 | ||
| no-negated-condition | 禁止使用取反的条件。 | ✅ ☑️ | 🔧 | ||
| no-negation-in-equality-check | 禁止在相等性检查中使用否定表达式。 | ✅ ☑️ | 💡 | ||
| no-nested-ternary | 禁止嵌套三元表达式。 | ✅ | 🔧 | ||
| no-new-array | 禁止使用 new Array()。 | ✅ ☑️ | 🔧 | 💡 | |
| no-new-buffer | 强制使用 Buffer.from() 和 Buffer.alloc() 代替已弃用的 new Buffer()。 | ✅ ☑️ | 🔧 | 💡 | |
| no-non-function-verb-prefix | 禁止非函数值使用函数风格的动词前缀。 | ✅ | 💭 | ||
| no-nonstandard-builtin-properties | 禁止在内置对象上使用非标准属性。 | ✅ ☑️ | |||
| no-null | 禁止使用 null 字面量。 | ✅ | 🔧 | 💡 | |
| no-object-as-default-parameter | 禁止将对象用作默认参数。 | ✅ ☑️ | |||
| no-object-methods-with-collections | 禁止在 Object 方法中使用 Map 或 Set。 | ✅ | 💡 | ||
| no-optional-chaining-on-undeclared-variable | 禁止对未声明的变量使用可选链。 | ✅ | |||
| no-process-exit | 禁止使用 process.exit()。 | ✅ ☑️ | |||
| no-redundant-comparison | 禁止在同一逻辑 AND 中因相等性检查而变得冗余的比较。 | ✅ ☑️ | 🔧 | 💡 | |
| no-return-array-push | 禁止使用 Array#push() 和 Array#unshift() 的返回值。 | ✅ | 💡 | ||
| no-selector-as-dom-name | 禁止在 DOM 名称中使用选择器语法。 | ✅ | 🔧 | ||
| no-shorthand-property-overrides | 禁止使用会覆盖相关长写属性的简写属性。 | ✅ ☑️ | |||
| no-single-promise-in-promise-methods | 禁止向 Promise 方法传递单元素数组。 | ✅ ☑️ | 🔧 | 💡 | |
| no-static-only-class | 禁止仅包含静态成员的类。 | ✅ ☑️ | 🔧 | ||
| no-subtraction-comparison | 建议直接比较值,而不是通过减法并与 0 比较。 | ✅ ☑️ | 🔧 | 💡 | |
| no-thenable | 禁止使用 then 属性。 | ✅ ☑️ | |||
| no-this-assignment | 禁止将 this 赋值给变量。 | ✅ ☑️ | |||
| no-this-outside-of-class | 禁止在类外部使用 this。 | ✅ | |||
| no-top-level-assignment-in-function | 禁止在函数内部对顶层变量进行赋值。 | ✅ | |||
| no-top-level-side-effects | 禁止在导出模块中使用顶层副作用。 | ✅ ☑️ | |||
| no-transition-all | 禁止将 all 用作过渡属性。 | ✅ ☑️ | |||
| no-typeof-undefined | 禁止使用 typeof 比较 undefined。 | ✅ ☑️ | 🔧 | 💡 | |
| no-uncalled-method | 禁止引用未调用的方法。 | ✅ | |||
| no-undeclared-class-members | 要求声明类成员。 | ✅ | 💡 | ||
| no-unnecessary-array-flat-depth | 禁止将 1 用作 Array#flat() 的 depth 参数。 | ✅ ☑️ | 🔧 | ||
| no-unnecessary-array-flat-map | 禁止仅包装单个项目的 Array#flatMap() 回调。 | ✅ | 🔧 | 💡 | |
| no-unnecessary-array-splice-count | 禁止将 .length 或 Infinity 用作 Array#{splice,toSpliced}() 的 deleteCount 或 skipCount 参数。 | ✅ ☑️ | 🔧 | ||
| no-unnecessary-await | 禁止等待非 Promise 值。 | ✅ ☑️ | 🔧 | ||
| no-unnecessary-boolean-comparison | 禁止对布尔字面量进行不必要的比较。 | ✅ | 🔧 | ||
| no-unnecessary-fetch-options | 禁止在 fetch() 和 new Request() 中使用不必要的选项。 | ✅ ☑️ | 🔧 | ||
| no-unnecessary-global-this | 禁止不必要的 globalThis 引用。 | ✅ ☑️ | 🔧 | ||
| no-unnecessary-nested-ternary | 禁止不必要的嵌套三元表达式。 | ✅ ☑️ | 🔧 | ||
| no-unnecessary-polyfills | 强制使用内置方法,而不是不必要的 polyfills。 | ✅ ☑️ | |||
| no-unnecessary-slice-end | 禁止将 .length 或 Infinity 用作 {Array,String,TypedArray}#slice() 的 end 参数。 | ✅ ☑️ | 🔧 | ||
| no-unnecessary-splice | 当存在更简单的替代方案时,禁止使用 Array#splice()。 | ✅ | 🔧 | ||
| no-unnecessary-string-trim | 禁止在 String#trim() 之前使用 String#startsWith() 或 String#endsWith()。 | ✅ ☑️ | 🔧 | ||
| no-unreadable-array-destructuring | 禁止不可读的数组解构。 | ✅ ☑️ | 🔧 | ||
| no-unreadable-for-of-expression | 禁止在 for…of 和 for await…of 循环头中使用不可读的迭代器表达式。 | ✅ | |||
| no-unreadable-iife | 禁止不可读的 IIFE。 | ✅ ☑️ | 💡 | ||
| no-unreadable-new-expression | 禁止不可读的 new 表达式。 | ||||
| no-unreadable-object-destructuring | 禁止不可读的对象解构。 | ✅ ☑️ | |||
| no-unsafe-buffer-conversion | 防止不安全地使用 ArrayBuffer 视图 .buffer。 | ✅ ☑️ | 💡 | ||
| no-unsafe-dom-html | 禁止使用不安全的 DOM HTML API。 | ||||
| no-unsafe-promise-all-settled-values | 禁止在未进行 fulfilled 状态守卫的情况下读取 Promise.allSettled() 结果中的 .value。 | ✅ ☑️ | |||
| no-unsafe-property-key | 禁止将不安全的值用作属性键。 | ✅ | |||
| no-unsafe-sqlite-interpolation | 禁止对传递给 Node 的 node:sqlite API 的 SQL 字符串进行插值。 | ✅ ☑️ | |||
| no-unsafe-string-replacement | 禁止在 String#replace() 和 String#replaceAll() 中使用非字面量替换值。 | ✅ | |||
| no-unused-array-method-return | 禁止忽略选定数组方法的返回值。 | ✅ ☑️ | |||
| no-unused-properties | 禁止未使用的对象属性。 | ||||
| no-useless-boolean-cast | 禁止在数组谓词回调中不必要的 Boolean() 转换。 | ✅ ☑️ | 🔧 | ||
| no-useless-coercion | 禁止对已经是目标类型的值进行无用的类型强制转换。 | ✅ ☑️ | 🔧 | ||
| no-useless-collection-argument | 禁止在 Set、Map、WeakSet 或 WeakMap 中传入无用的值或回退值。 | ✅ ☑️ | 🔧 | 💡 | |
| no-useless-compound-assignment | 禁止无用的复合赋值,例如 x += 0。 | ✅ ☑️ | 💡 | ||
| no-useless-concat | 禁止无用的字面量拼接。 | ✅ ☑️ | 🔧 | ||
| no-useless-continue | 禁止无用的 continue 语句。 | ✅ ☑️ | 🔧 | ||
| no-useless-delete-check | 禁止删除前不必要的存在性检查。 | ✅ ☑️ | 🔧 | 💡 | |
| no-useless-else | 禁止在退出语句后使用 else。 | ✅ | 🔧 | ||
| no-useless-error-capture-stack-trace | 禁止不必要的 Error.captureStackTrace(…)。 | ✅ ☑️ | 🔧 | ||
| no-useless-fallback-in-spread | 禁止在对象字面量展开时无用的回退值。 | ✅ ☑️ | 🔧 | ||
| no-useless-iterator-to-array | 禁止对迭代器使用不必要的 .toArray()。 | ✅ ☑️ | 🔧 | 💡 | |
| no-useless-length-check | 禁止无用的数组长度检查。 | ✅ ☑️ | 🔧 | ||
| no-useless-logical-operand | 禁止在涉及布尔字面量的逻辑表达式中使用不必要的操作数。 | ✅ ☑️ | 🔧 | ||
| no-useless-override | 禁止无用的类方法重写。 | ✅ ☑️ | 🔧 | ||
| no-useless-promise-resolve-reject | 禁止在异步函数或 Promise 回调中返回/生成 Promise.resolve/reject() | ✅ ☑️ | 🔧 | ||
| no-useless-re-export | 禁止冗余的重新导出。 | ✅ ☑️ | |||
| no-useless-recursion | 禁止可以用循环替代的简单递归函数调用。 | ✅ | |||
| no-useless-spread | 禁止不必要的 spread。 | ✅ ☑️ | 🔧 | 💡 | |
| no-useless-switch-case | 禁止 switch 语句中无用的 case。 | ✅ ☑️ | 💡 | ||
| no-useless-template-literals | 禁止无用的模板字符串表达式。 | ✅ ☑️ | 🔧 | 💡 | |
| no-useless-undefined | 禁止无用的 undefined。 | ✅ ☑️ | 🔧 | 💡 | |
| no-xor-as-exponentiation | 禁止在可能意指幂运算的地方使用按位 XOR 运算符。 | ✅ ☑️ | 💡 | ||
| no-zero-fractions | 禁止带有零小数部分或悬空小数点的数字字面量。 | ✅ ☑️ | 🔧 | ||
| number-literal-case | 强制数字字面量使用正确的字母大小写。 | ✅ ☑️ | 🔧 | ||
| numeric-separators-style | 通过正确分组数字来强制数字分隔符的样式。 | ✅ ☑️ | 🔧 | ||
| operator-assignment | 在可能的情况下要求使用赋值运算符简写。 | ✅ | 🔧 | 💡 | |
| prefer-abort-signal-any | 优先使用 AbortSignal.any() 而不是在信号之间手动转发中止事件。 | ✅ | 💡 | ||
| prefer-abort-signal-timeout | 优先使用 AbortSignal.timeout() 而不是使用 setTimeout() 手动中止 AbortController。 | ✅ | 💡 | ||
| prefer-add-event-listener | 优先使用 .addEventListener() 和 .removeEventListener() 而不是 on-函数。 | ✅ ☑️ | 🔧 | ||
| prefer-add-event-listener-options | 在 .addEventListener() 中优先使用选项对象而不是布尔值。 | ✅ ☑️ | 🔧 | ||
| prefer-aggregate-error | 抛出聚合错误时,优先使用 AggregateError。 | ✅ ☑️ | 🔧 | ||
| prefer-array-find | 获取 .filter(…) 的第一个或最后一个元素时,优先使用 .find(…) 和 .findLast(…)。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-array-flat | 展平数组时,优先使用 Array#flat() 而非传统技术。 | ✅ ☑️ | 🔧 | ||
| prefer-array-flat-map | 优先使用 .flatMap(…) 而非 .map(…).flat() 和 .filter(…).flatMap(…)。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-array-from-async | 优先使用 Array.fromAsync() 而非 for await…of 数组累积。 | ✅ | 🔧 | ||
| prefer-array-from-map | 优先使用 Array.from() 映射函数参数。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-array-from-range | 创建范围数组时,优先使用 Array.from({length}, …)。 | ✅ ☑️ | 🔧 | ||
| prefer-array-index-of | 在查找元素索引时,优先使用 Array#{indexOf,lastIndexOf}() 而非 Array#{findIndex,findLastIndex}()。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-array-iterable-methods | 当索引或值未被使用时,优先直接迭代数组或使用 Array#keys() 而非 Array#entries()。 | ✅ | 🔧 | ||
| prefer-array-last-methods | 优先使用面向末尾的数组方法,而非在 Array#reverse() 或 Array#toReversed() 后调用方法。 | ✅ ☑️ | 💡 | ||
| prefer-array-slice | 在读取返回的数组时,优先使用 Array#slice() 而非 Array#splice()。 | ✅ | 💡 | ||
| prefer-array-some | 优先使用 .some(…) 而非 .filter(…).length 检查和 .{find,findLast,findIndex,findLastIndex}(…)。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-at | 优先使用 .at() 方法进行索引访问和 String#charAt()。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-await | 优先使用 await 而非 Promise 链式调用。 | ✅ ☑️ | 💡 | ||
| prefer-bigint-literals | 优先使用 BigInt 字面量而非构造函数。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-blob-reading-methods | 优先使用 Blob#arrayBuffer() 而非 FileReader#readAsArrayBuffer(…),优先使用 Blob#text() 而非 FileReader#readAsText(…)。 | ✅ ☑️ | |||
| prefer-block-statement-over-iife | 优先使用块语句而非仅用于作用域的 IIFE。 | ✅ ☑️ | 🔧 | ||
| prefer-boolean-return | 优先直接返回布尔表达式而非 if 语句。 | ✅ ☑️ | 🔧 | ||
| prefer-class-fields | 优先使用类字段声明而非构造函数中的 this 赋值。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-classlist-toggle | 优先使用 Element#classList.toggle() 来切换类名。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-code-point | 优先使用 String#codePointAt(…) 而非 String#charCodeAt(…),优先使用 String.fromCodePoint(…) 而非 String.fromCharCode(…)。 | ✅ ☑️ | 💡 | ||
| prefer-continue | 优先使用提前 continue 而非整个循环的条件包裹。 | ✅ | 🔧 | ||
| prefer-date-now | 优先使用 Date.now() 获取自 Unix Epoch 以来的毫秒数。 | ✅ ☑️ | 🔧 | ||
| prefer-default-parameters | 优先使用默认参数而非重新赋值。 | ✅ ☑️ | 💡 | ||
| prefer-direct-iteration | 优先使用直接迭代而非默认迭代器方法调用。 | ✅ ☑️ | 🔧 | ||
| prefer-dispose | 优先使用 using/await using 而非手动 try/finally 资源释放。 | 💡 | |||
| prefer-dom-node-append | 优先使用 Element#append() 而非 Node#appendChild()。 | ✅ ☑️ | 🔧 | ||
| prefer-dom-node-html-methods | 优先使用 .getHTML() 和 .setHTML() 而非 .innerHTML。 | ✅ | 🔧 | 💡 | |
| prefer-dom-node-remove | 优先使用 childNode.remove() 而非 parentNode.removeChild(childNode)。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-dom-node-replace-children | 在清空 DOM 子元素时,优先使用 .replaceChildren()。 | ✅ ☑️ | 🔧 | ||
| prefer-dom-node-text-content | 优先使用 .textContent 而非 .innerText。 | ✅ ☑️ | 💡 | ||
| prefer-early-return | 优先使用提前返回,而非对整个函数进行条件包裹。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-else-if | 对于具有相关条件的相邻 if 语句,优先使用 else if。 | ✅ | 🔧 | 💡 | |
| prefer-error-is-error | 在检查错误时,优先使用 Error.isError()。 | 🔧 | |||
| prefer-event-target | 优先使用 EventTarget 而非 EventEmitter。 | ✅ ☑️ | |||
| prefer-explicit-viewport-units | 优先使用显式视口单位。 | 💡 | |||
| prefer-export-from | 重新导出时优先使用 export…from。 | ✅ | 🔧 | 💡 | |
| prefer-flat-math-min-max | 优先使用扁平的 Math.min() 和 Math.max() 调用,而非嵌套调用。 | ✅ ☑️ | 🔧 | ||
| prefer-get-or-insert-computed | 当默认值具有副作用时,优先使用 .getOrInsertComputed()。 | ✅ | 🔧 | ||
| prefer-global-number-constants | 优先使用全局数值常量,而非 Number 静态属性。 | ✅ ☑️ | 🔧 | ||
| prefer-global-this | 优先使用 globalThis,而非 window、self 和 global。 | ✅ ☑️ | 🔧 | ||
| prefer-group-by | 优先使用 Object.groupBy() 或 Map.groupBy() 而非基于 reduce 的分组。 | ✅ | 🔧 | ||
| prefer-has-check | 在检查存在性时优先使用 .has()。 | ✅ ☑️ | 🔧 | ||
| prefer-hoisting-branch-code | 优先将 if 语句所有分支共有的代码移出分支。 | ✅ | 🔧 | 💡 | |
| prefer-https | 优先使用 HTTPS 而非 HTTP。 | ✅ | 🔧 | ||
| prefer-identifier-import-export-specifiers | 在导入和导出说明符中优先使用标识符而非字符串字面量。 | ✅ ☑️ | 🔧 | ||
| prefer-import-meta-properties | 优先使用 import.meta.{dirname,filename} 而非传统技术来获取文件路径。 | 🔧 | |||
| prefer-includes | 在检查存在性或不存在性时,优先使用 .includes() 而非 .indexOf()、.lastIndexOf() 和 Array#some()。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-includes-over-repeated-comparisons | 优先使用 .includes() 而非重复的相等性比较。 | ✅ | |||
| prefer-iterable-in-constructor | 优先在构造函数中直接传递可迭代对象,而不是填充空集合。 | ✅ ☑️ | 🔧 | ||
| prefer-iterator-concat | 优先使用 Iterator.concat(…) 而非临时展开数组。 | 🔧 | 💡 | ||
| prefer-iterator-helpers | 优先使用迭代器辅助函数,而非来自迭代器的临时数组。 | ✅ ☑️ | 💡 | ||
| prefer-iterator-to-array | 优先使用 Iterator#toArray() 而非来自迭代器展开的临时数组。 | ✅ | 🔧 | 💡 | |
| prefer-iterator-to-array-at-end | 优先将 .toArray() 移至迭代器辅助函数链的末尾。 | ✅ ☑️ | 💡 | ||
| prefer-keyboard-event-key | 优先使用 KeyboardEvent#key 而非已弃用的键盘事件属性。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-location-assign | 优先使用 location.assign() 而非对 location.href 进行赋值。 | ✅ | 🔧 | ||
| prefer-logical-operator-over-ternary | 优先使用逻辑运算符而非三元运算符。 | ✅ ☑️ | 💡 | ||
| prefer-map-from-entries | 当将结果用作 map 时,优先使用 new Map() 而非 Object.fromEntries()。 | ✅ ☑️ | 🔧 | ||
| prefer-math-abs | 优先使用 Math.abs() 而非手动绝对值表达式和对称范围检查。 | ✅ ☑️ | 🔧 | ||
| prefer-math-constants | 优先使用 Math 常量而非其近似数值。 | ✅ ☑️ | 💡 | ||
| prefer-math-min-max | 对于简单比较,优先使用 Math.min() 和 Math.max() 而非三元运算符。 | ✅ ☑️ | 🔧 | ||
| prefer-math-trunc | 优先使用 Math.trunc() 来截断数字。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-minimal-ternary | 优先将三元运算符移至表达式中变化最小的部分。 | ✅ ☑️ | |||
| prefer-modern-dom-apis | 优先使用现代 DOM API。 | ✅ ☑️ | 🔧 | ||
| prefer-modern-math-apis | 优先使用现代 Math API 而非遗留模式。 | ✅ ☑️ | 🔧 | ||
| prefer-module | 优先使用 JavaScript 模块 (ESM) 而非 CommonJS。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-native-coercion-functions | 优先直接使用 String、Number、BigInt、Boolean 和 Symbol。 | ✅ ☑️ | 🔧 | ||
| prefer-negative-index | 尽可能使用负索引代替 .length - index。 | ✅ ☑️ | 🔧 | ||
| prefer-node-protocol | 导入 Node.js 内置模块时,优先使用 node: 协议。 | ✅ ☑️ | 🔧 | ||
| prefer-number-coercion | 优先使用 Number() 而非 parseFloat() 和十进制 parseInt()。 | ✅ ☑️ | 💡 | ||
| prefer-number-is-safe-integer | 优先使用 Number.isSafeInteger() 而非整数检查。 | ✅ | 💡 | ||
| prefer-number-properties | 优先使用 Number 静态方法而非全局函数,并可选地优先使用静态属性而非全局常量。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-object-define-properties | 优先使用 Object.defineProperties() 而非多次调用 Object.defineProperty()。 | ✅ ☑️ | 🔧 | ||
| prefer-object-destructuring-defaults | 优先使用对象解构默认值,而非带有展开运算符的默认对象字面量。 | ✅ | 💡 | ||
| prefer-object-from-entries | 优先使用 Object.fromEntries(…) 将键值对列表转换为对象。 | ✅ ☑️ | 🔧 | ||
| prefer-object-iterable-methods | 优先使用最具体的 Object 可迭代方法。 | ✅ ☑️ | 🔧 | ||
| prefer-observer-apis | 优先使用观察者 API,而非带有布局读取的 resize 和 scroll 监听器。 | ✅ | |||
| prefer-optional-catch-binding | 优先省略 catch 绑定参数。 | ✅ ☑️ | 🔧 | ||
| prefer-path2d | 优先使用 Path2D 处理重复绘制的 canvas 路径。 | ✅ ☑️ | |||
| prefer-private-class-fields | 优先使用私有类字段,而非下划线前缀约定。 | ✅ | 🔧 | ||
| prefer-promise-try | 优先使用 Promise.try() 而非 promise 包装样板代码。 | ✅ | 🔧 | ||
| prefer-promise-with-resolvers | 从 new Promise() 中提取 resolver 函数时,优先使用 Promise.withResolvers()。 | ✅ ☑️ | 🔧 | ||
| prefer-prototype-methods | 优先从原型而非实例中借用方法。 | ✅ ☑️ | 🔧 | ||
| prefer-query-selector | 优先使用 .querySelector() 和 .querySelectorAll() 而非旧的 DOM 查询方法。 | ✅ | 🔧 | ||
| prefer-queue-microtask | 优先使用 queueMicrotask() 而非 process.nextTick()、setImmediate() 和 setTimeout(…, 0)。 | ✅ ☑️ | 🔧 | ||
| prefer-reflect-apply | 优先使用 Reflect.apply() 而非 Function#apply()。 | ✅ ☑️ | 🔧 | ||
| prefer-regexp-escape | 优先使用 RegExp.escape() 来转义用于正则表达式的字符串。 | 🔧 | 💡 | ||
| prefer-regexp-test | 优先使用 RegExp#test() 而非 String#match()、String#search() 和 RegExp#exec()。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-response-static-json | 优先使用 Response.json() 而非 new Response(JSON.stringify())。 | ✅ ☑️ | 🔧 | ||
| prefer-scoped-selector | 使用元素查询选择器方法时,优先使用 :scope。 | ✅ | 💡 | ||
| prefer-set-has | 检查存在性或不存在性时,优先使用 Set#has() 而非 Array#includes()。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-set-methods | 优先使用 Set 方法进行 Set 操作。 | ✅ | 🔧 | 💡 | |
| prefer-set-size | 优先使用 Set#size 而非 Array#length。 | ✅ ☑️ | 🔧 | ||
| prefer-short-arrow-method | 优先使用箭头函数属性而非仅包含单个返回值的函数方法。 | 🔧 | |||
| prefer-simple-condition-first | 在逻辑表达式中优先使用简单条件。 | ✅ | 🔧 | ||
| prefer-simple-sort-comparator | 为 Array#sort() 优先使用简单的比较函数。 | ✅ ☑️ | 💡 | ||
| prefer-simplified-conditions | 优先使用简化后的条件。 | ✅ ☑️ | 🔧 | ||
| prefer-single-array-predicate | 优先使用带有组合谓词的单个 Array#some() 或 Array#every()。 | ✅ ☑️ | 💡 | ||
| prefer-single-call | 强制将多个 Array#{push,unshift}()、Element#classList.{add,remove}() 和 importScripts() 合并为一次调用。 | ✅ ☑️ | 🔧 | 💡 | |
| prefer-single-object-destructuring | 每个局部 const 源优先使用单个对象解构声明。 | ✅ | 🔧 | ||
| prefer-single-replace | 强制将多个单字符替换合并为使用正则表达式的单个 String#replaceAll()。 | ✅ ☑️ | 🔧 | ||
| prefer-smaller-scope |