按照个人喜好 (@the1812) 配置的 ESLint Config. 可用于 JavaScript, TypeScript 和 Vue 3 项目.
集成 prettier
一个命令搞定代码风格和格式化, 无需担心互相冲突.
# ✔️
eslint .
# ❌
eslint .
prettier . --write内置 prettier 的配置为:
{
semi: false,
singleQuote: true,
trailingComma: 'all',
arrowParens: 'avoid',
endOfLine: 'auto',
printWidth: 100,
}除纯类型引用以外, 所有定义必须先声明再使用.
// ✔️
function name() { }
const a = name()
// ❌
const a = name()
function name() { }跟丑陋的全大写和下划线说再见.
// ✔️
const SomeConstant = 1
function someFunction (param) {
const clonedParam = clone(param)
}
let someVar = 2
// ❌
const SOME_CONSTANT = 1
function someFunction (param) {
const _param = clone(param)
}
let some_var = 2同名属性优先解构, 同名参数省略冒号.
// ✔️
const { prop } = obj
const param = {
prop,
}
// ❌
const prop = obj.prop
const param = {
prop: prop,
}有意义的名字才是好名字.
// ✔️
export const createApp = () => { }
// ❌
const createApp = () => { }
export default createAppNote
其他常见第三方库的配置文件仍然允许默认导出. 例如 webpack.config.ts, vite.config.ts 等.
控制流语句内容必须有大括号.
// ✔️
if (foo) {
doSomething()
}
while (bar) {
doSomething()
}
for (let i = 0; i < items.length; i+=1) {
doSomething()
}
// ❌
if (foo) doSomething()
while (bar) doSomething()
for (let i = 0; i < items.length; i+=1)
doSomething()pnpm install -D eslint prettier typescript @the1812/eslint-config在 ESLint 配置文件中添加任意一种配置.
import config from '@the1812/eslint-config'
export default configimport config from '@the1812/eslint-config/vue'
export default config