Egg.js 项目的合理 ESLint 配置文件模板
安装依赖
bash
npm install eslint @babel/eslint-parser eslint-plugin-import eslint-plugin-promise eslint-plugin-node --save-dev
-
extends : 扩展了
eslint-config-egg
以及其他一些常用的插件配置。 -
parser : 使用
@babel/eslint-parser
作为解析器,支持最新的 JavaScript 语法。 -
parserOptions: 设置了解析器选项,包括 ECMAScript 版本和模块类型。
-
env: 设置了 Node.js 和 ES6 环境。
-
settings : 配置了导入解析器,支持
.js
和.ts
文件。 -
rules: 添加了一些常用的 ESLint 规则,包括对控制台日志、未使用的变量、导入检查、Promise 检查和 Node.js 特定的规则
.eslintrc
javascript
{
"extends": [
"eslint-config-egg",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:promise/recommended",
"plugin:node/recommended"
],
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"requireConfigFile": false
},
"env": {
"node": true,
"es6": true
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts"]
}
}
},
"rules": {
"no-console": "warn",
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"import/no-unresolved": "error",
"import/named": "error",
"import/namespace": "error",
"import/default": "error",
"import/export": "error",
"import/extensions": ["error", "always", { "js": "never", "ts": "never" }],
"import/newline-after-import": "error",
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
"promise/always-return": "error",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
"promise/catch-or-return": "error",
"node/no-missing-import": "error",
"node/no-unpublished-require": "error",
"node/no-unsupported-features/es-syntax": "error",
"node/process-exit-as-throw": "error",
"node/shebang": "error"
}
}
但如果同时使用prettier与eslint冲突 可以再补上安装
bash
npm install eslint-config-prettier eslint-plugin-prettier --save-dev
这些工具可以帮助你关闭 ESLint 中与 Prettier 冲突的规则,并将 Prettier 作为 ESLint 的规则之一运行
最终配置为
bash
{
"extends": [
"eslint-config-egg",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:promise/recommended",
"plugin:node/recommended",
"plugin:prettier/recommended", // 添加 Prettier 插件
"prettier" // 确保 Prettier 覆盖其他规则
],
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"requireConfigFile": false
},
"env": {
"node": true,
"es6": true
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts"]
}
}
},
"rules": {
"no-console": "warn",
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"import/no-unresolved": "error",
"import/named": "error",
"import/namespace": "error",
"import/default": "error",
"import/export": "error",
"import/extensions": ["error", "always", { "js": "never", "ts": "never" }],
"import/newline-after-import": "error",
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
"promise/always-return": "error",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
"promise/catch-or-return": "error",
"node/no-missing-import": "error",
"node/no-unpublished-require": "error",
"node/no-unsupported-features/es-syntax": "error",
"node/process-exit-as-throw": "error",
"node/shebang": "error",
"no-trailing-spaces": 1, // 禁用 no-trailing-spaces 规则
"prettier/prettier": ["error", { "singleQuote": true, "semi": false }] // 配置 Prettier 规则
}
}