ITADN
sindresorhus/eslint-plugin-unicorn
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

eslint-plugin-unicorn Coverage Status npm version

Unicorn

超过 300 条强大的 ESLint 规则

大多数规则针对 JavaScript 和 TypeScript,但在使用相应的 ESLint 语言插件时,部分规则也可用于检查 CSS、HTML、JSON 和 Markdown

提议新规则 ➡

我们不接受拉取请求,因为 AI 生成的垃圾内容太多。

安装

npm install --save-dev eslint eslint-plugin-unicorn

需要 ESLint >=10.4flat 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 选项] 自动修复。
💡 可通过 编辑器建议] 手动修复。
💭 需要 类型信息]。

NameDescription💼🔧💡💭
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-readJSON.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强制显式比较值的 lengthsize 属性。🔧💡
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禁止不必要的 BlobFile 转换。✅ ☑️💡
no-boolean-sort-comparator禁止返回布尔值的排序比较器。✅ ☑️💡
no-break-in-nested-loop禁止在嵌套循环以及循环内的 switch 语句中使用 breakcontinue
no-canvas-to-image建议直接绘制画布,而不是将其转换为图像。✅ ☑️
no-chained-comparison禁止使用如 a < b < c 这样的链式比较。✅ ☑️💡
no-collection-bracket-access禁止使用括号表示法访问 MapSetWeakMapWeakSet 的条目。✅ ☑️💡
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…offor 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禁止以 newclass 开头的标识符。
no-late-current-target-access禁止在同步事件分发结束后访问 event.currentTarget
no-late-event-control禁止在同步事件分发结束后调用事件控制方法。
no-lonely-if禁止在不含 elseif 块中,将 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 方法中使用 MapSet💡
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禁止将 .lengthInfinity 用作 Array#{splice,toSpliced}()deleteCountskipCount 参数。✅ ☑️🔧
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禁止将 .lengthInfinity 用作 {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…offor 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禁止在 SetMapWeakSetWeakMap 中传入无用的值或回退值。✅ ☑️🔧💡
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,而非 windowselfglobal✅ ☑️🔧
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优先直接使用 StringNumberBigIntBooleanSymbol✅ ☑️🔧
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-resolversnew 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-comparatorArray#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