NodeJS + ESLint + Prettier - 有史以来最简单的设置
别指望这篇文章里有什么花哨的图片,我们直接说正事吧。我只希望你用的是纱线,因为你应该用。
-
yarn add eslint --dev
(来自这里) -
yarn run eslint --init
(也来自上面的链接) -
在设置提示中按照您想要的任何选项进行操作
eslint
。 -
排除
package-lock.json
可能是eslint
CLI 使用 npm 安装某些东西导致的问题。啧啧。删除那个文件,然后运行就yarn
可以了。 -
yarn add prettier eslint-config-prettier eslint-plugin-prettier @typescript-eslint/parser -D
(来自这里) -
.eslintrc.js
你的 repo现在应该有一个如下所示的文件:
module.exports = {
env: {
browser: true,
es2021: true,
'jest/globals': true,
node: true,
},
extends: ['airbnb-base', 'eslint:recommended', 'prettier'],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'jest', 'prettier'],
rules: {
semi: ['error', 'always'],
quotes: ['error', 'single'],
'import/extensions': [
'error',
'ignorePackages',
{
js: 'never',
jsx: 'never',
ts: 'never',
tsx: 'never',
},
],
'import/no-dynamic-require': 0,
'global-require': 0,
'import/prefer-default-export': 0,
'no-underscore-dangle': 0,
'no-await-in-loop': 0,
'no-restricted-syntax': 0,
'no-return-await': 0,
'no-console': 0,
'prettier/prettier': [
'error',
{
trailingComma: 'es5',
singleQuote: true,
printWidth: 80,
tabWidth: 2,
endOfLine: 'lf',
arrowParens: 'always',
},
],
},
};
- 看起来
.eslintignore
像这样:(如果没有,请创建一个)
/node_modules
/reports
package.json
在你的运行 lint fixes中添加一个命令
{
...
"scripts": {
...
"lintfix": "eslint src --fix --cache",
...
},
}
- 如果你想用Husky做预提交钩子,那就随你便吧。你可以查看网站上的说明,不过这里还是有说明的:a.
yarn add husky --save-dev
b.npx husky install
c.npx husky add .husky/pre-commit "yarn eslint && git add -A"
d. git add -A e. git commit -m “终于配置好了 eslint、prettier 和 husky,没有任何问题🐄💩”