Skip to content

unicorn

基于 eslint-plugin-unicorn 推荐集的现代化增强层,默认开启,已覆盖 178/182 条规则。

js
import lintspec from '@qwqo/eslint-config'

export default lintspec({ unicorn: true })

默认关闭 6 条较激进的规则(filename-caseprevent-abbreviationsno-nullno-array-reduceno-nested-ternaryprefer-top-level-await)。如需启用:

js
export default lintspec({ unicorn: { strict: true } })

标记说明:🔧 可通过 eslint --fix 自动修复 · 💡 在编辑器中提供修复建议。

优先 node: 协议(prefer-node-protocol) 🔧

约定导入 Node 内置模块时使用 node: 协议。

不推荐
js
import { readFile } from 'fs'
推荐
js
import { readFile } from 'node:fs'

创建错误须用 new(throw-new-error) 🔧

约定抛出错误时使用 new

不推荐
js
throw Error('boom')
推荐
js
throw new Error('boom')

优先 Number 静态属性(prefer-number-properties) 🔧 💡

约定使用 Number 上的方法取代全局函数。

不推荐
js
const valid = isNaN(value)
推荐
js
const valid = Number.isNaN(value)

优先 for…of 替代 forEach(no-array-for-each) 🔧 💡

约定用 for...of 取代 Array#forEach(更易于配合 await / break / continue)。

不推荐
js
items.forEach(item => handle(item))
推荐
js
for (const item of items) {
  handle(item)
}

优先 some(prefer-array-some) 🔧 💡

判断是否存在匹配项时约定使用 Array#some

不推荐
js
const exists = items.find(item => item.active) !== undefined
推荐
js
const exists = items.some(item => item.active)

禁止 Promise 方法参数中 await(no-await-in-promise-methods) 💡

约定不要在 Promise.all 等方法的参数里使用 await(会让并发退化为串行)。

不推荐
js
const results = await Promise.all([await fetchA(), await fetchB()])
推荐
js
const results = await Promise.all([fetchA(), fetchB()])

优先 Date.now()(prefer-date-now) 🔧

获取时间戳时约定使用 Date.now()

不推荐
js
const time = new Date().getTime()
推荐
js
const time = Date.now()

内置对象须用 new(new-for-builtins) 🔧 💡

约定构造内置对象时使用 new

不推荐
js
const list = Array(1, 2, 3)
推荐
js
const list = new Array(1, 2, 3)

优先 slice(prefer-string-slice) 🔧

约定使用 String#slice 取代已废弃的 substr / substring

不推荐
js
const head = text.substr(0, 1)
推荐
js
const head = text.slice(0, 1)

TIP

unicorn 共启用 170+ 条规则,完整列表见 全部规则