English | 简体中文
- 单引号,半角
- 共享 ESLint 和 Stylelint 配置
- 基于 Airbnb 的代码校验
- 基于 Prettier 的代码格式化
- ESLint Flat Config,轻松编写
- 易于集成、配置和自定义规则
@flypeng/eslint-config
@flypeng/eslint-config-basic
@flypeng/eslint-config-javascript
@flypeng/eslint-config-typescript
@flypeng/eslint-config-vue
@flypeng/eslint-config-react
@flypeng/eslint-config-other
@flypeng/stylelint-config
pnpm add eslint stylelint prettier @flypeng/eslint-config @flypeng/stylelint-config -D`
{
"extends": "@flypeng/eslint-config"
}
使用 ESM
"type": "module"
inpackage.json
// eslint.config.js
import flypeng from '@flypeng/eslint-config';
export default flypeng();
使用 CJS
// eslint.config.js
const flypeng = require('@flypeng/eslint-config').default;
module.exports = flypeng();
{
"extends": "@flypeng/stylelint-config"
}
{
"semi": true,
"singleQuote": true,
"bracketSpacing": true,
"printWidth": 120,
"vueIndentScriptAndStyle": false
}
/dist/*
.local
.output.js
/node_modules/**
**/*.svg
**/*.sh
/public/*
**/*.yaml
{
"scripts": {
"lint": "prettier . --write && eslint . && stylelint \"**/*.{css,scss,vue}\"",
"lint:fix": "prettier . --write && eslint . --fix && stylelint --fix \"**/*.{css,scss,sass,vue}\""
}
}
安装 VSCode ESLint、Stylelint、Prettier、Prettier ESLint 插件并创建.vscode/settings.json
。
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"eslint.enable": true,
"eslint.experimental.useFlatConfig": true, // 启用ESLint新的配置系统
"eslint.format.enable": true,
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"jsx",
"tsx",
"vue",
"html",
"markdown",
"json",
"jsonc",
"json5",
"yaml"
],
"css.validate": false,
"less.validate": false,
"scss.validate": false,
"stylelint.enable": true,
"stylelint.validate": ["css", "less", "scss", "sass", "vue", "postcss"]
}
在团队开发中,.editorconfig 的配置是必要的,以确保团队编辑器的行为风格一致。
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
# Change these settings to your own preference
indent_style = space
indent_size = 4
[*.{ts,tsx,js,jsx,json,css,scss,yml,yaml,html,vue,md}]
indent_size = 2
[*.md]
trim_trailing_whitespace = false
如果需要重写规则,很可能需要指定文件范围,因为除 @flypeng/eslint-config-javascript
以外的软件包所使用的规则都是针对特定文件的插件。如果不声明文件范围,就会出现意外错误。
示例
// eslint.config.js
import flypeng from '@flypeng/eslint-config';
export default [
...flypeng(),
{
/**
* If you do not declare the file Scope, other files will also refer to this rule.
* But the rules of other files do not use the @ typescript-eslint/eslint-plugin plugin, so an error will be reported during Lint.
* Reference detail: https://github.com/eslint/eslint/issues/17485
*/
files: ['**/*.ts'],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
},
];
网上有很多关于使用 Prettier 进行代码格式化的讨论,的确,Prettier 格式化的某些方面有些强制性,导致代码的可读性较差,或者不是你喜欢的代码风格。
其中一篇由 Anthony Fu 撰写的 Why I don't use Prettier 接受了 Prettier 不适用的原因。看看他怎么说。
我为什么要在 @flypeng/eslint-config
中启用 Prettier 来处理代码格式?
我主要考虑了两点:
首先,如果你是个人开发者,而且你对整个项目的代码风格有很好的把握,你可以考虑不使用 Prettier,但如果你是团队开发者,你不能保证你的代码风格会被其他团队成员接受,而且会导致整个项目的代码风格五花八门。因此,这时你应该考虑使用 Prettier 来强制统一代码风格。
第二:ESLint 的主要职责是验证代码而非代码格式,因此对于简单的校验和自动修复可以在一定程度上起到启迪格式化的作用。但对于整体项目的代码风格 ESLint 并不能保证,Prettier 的主要职责是统一整体风格。
基于以上两点,我决定开放使用 Prettier。
V2 是对 V1 的重构,以适应 ESLint 的扁平化配置,并使用 Prettier 作为代码格式化工具。
如果您需要调整 ESLint 的扁平化配置,那么您在内部使用的所有软件包都需要使用新版本的扁平化配置。幸运的是,ESLint 提供了一个适配方案 eslintrc,用于将旧配置转换为新配置。