react eslint
Last updated on November 22, 2024 pm
🧙 Questions
实现在Webstorm中,alt+f自动根据eslint规范格式化代码并在eslint中自动调用prettier规范进行格式代码
☄️ Ideas
安装eslint
npm install eslint --save-dev
执行eslint初始化命令
npx eslint --init
安装eslint中prettier插件
npm install --save-dev eslint-plugin-prettier
npm install --save-dev eslint-config-prettier
npm install --save-dev --save-exact prettier
配置eslint配置文件
Note:
extends属性中prettier要排在最后,否则prettier不生效
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: [
'standard',
'plugin:react/recommended',
'prettier'
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: 'module',
},
plugins: [
'prettier',
'react',
'@typescript-eslint'
],
rules: {
'prettier/prettier': 'error',
'no-use-before-define': 'off',
'no-undef': 'off',
'react/prop-types': 'off',
'no-unused-vars': 'off',
'node/handle-callback-err': 'off',
'array-callback-return': 'off',
},
};
配置prettier
Note:
修改完prettier配置文件后,需要重启webstorm才会生效
vim .prettierrc
printWidth: 12
tabWidth: 2
useTabs: false
semi: false
singleQuote: true
quoteProps: as-needed
jsxSingleQuote: true
trailingComma: es5
bracketSpacing: true
jsxBracketSameLine: true
arrowParens: always
parser: typescript
requirePragma: true
insertPragma: true
proseWrap: preserve
htmlWhitespaceSensitivity: strict
vueIndentScriptAndStyle: false
endOfLine: lf
配置webstorm
🔗 Links
react eslint
https://ispong.isxcode.com/vue/react/react eslint/