当前位置:网站首页>3.集成eslint、prettier
3.集成eslint、prettier
2022-06-30 09:32:00 【山川pro】
- 下载依赖
npm i -D eslint prettier eslint-config-airbnb eslint-config-prettier eslint-plugin-prettier eslint-plugin-import eslint-import-resolver-webpack eslint-plugin-jsx-a11y eslint-plugin-react eslint-plugin-react-hooks @babel/eslint-parser
eslint-plugin-import:此插件主要为了校验 import/export 语法,防止错误拼写文件路径以及导出名称的问题eslint-plugin-jsx-a11y:提供 jsx 元素可访问性校验eslint-plugin-react:校验 Reacteslint-plugin-react-hooks:根据 Hooks API 校验 Hooks 的使用eslint-config-airbnb: 基本的代码规范库eslint-import-resolver-webpack: 解决 eslint 读取不到别名(alias)导致报错的问题eslint-config-prettier: 禁用掉了一些不必要的以及和 Prettier 相冲突的 ESLint 规则。eslint-plugin-prettier: 将 prettier 作为 ESLint 的规则来使用,相当于代码不符合 Prettier 的标准时,会报一个 ESLint 错误,同时也可以通过 eslint --fix 来进行格式化@babel/eslint-parser: 一个解析器,允许 ESLint 运行在由 Babel 转换的源代码上。npm i -D eslint-webpack-plugineslint-webpack-plugin:开启 eslint
- 在 webpack 中配置
const ESLintPlugin = require('eslint-webpack-plugin'); // 开启eslint
plugins: [
new ESLintPlugin({
context: resolve('../src'),
// fix: true, /* 自动帮助修复 */
extensions: ['js', 'json', 'jsx'],
exclude: 'node_modules'
})
],
- 配置
.eslintrc.js文件(放在根目录下)
const fs = require('fs');a
const path = require('path');
const prettierOptions = JSON.parse(fs.readFileSync(path.resolve(__dirname, '.prettierrc'), 'utf8'));
const OFF = 0;
const ERROR = 2;
module.exports = {
parser: '@babel/eslint-parser', // 解释器
extends: ['airbnb', 'prettier'],
plugins: ['prettier', 'react', 'react-hooks', 'jsx-a11y'],
env: {
browser: true, // 启用浏览器中全局变量
node: true, // 启用node中全局变量
es6: true,
},
// 解析器
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
rules: {
'prettier/prettier': ['error', prettierOptions],
'no-console': ERROR,
'no-array-constructor': ERROR,
'no-unused-vars': OFF, // 未使用import的值
'no-undef': ERROR, // 不允许使用未定义变量
'no-underscore-dangle': OFF, // 不允许 '_' 开头命名
'no-cond-assign': ERROR, // 条件语句不允许赋值
'no-var': ERROR, // 不用var
'no-extra-boolean-cast': ERROR, // 禁止不必要的布尔转换
'no-undef-init': ERROR, // 禁止赋值undefined
'import/imports-first': OFF,
'import/newline-after-import': OFF,
'import/no-dynamic-require': OFF,
'import/no-extraneous-dependencies': OFF,
'import/no-named-as-default': OFF,
'import/no-unresolved': ERROR,
'import/no-webpack-loader-syntax': OFF,
'import/prefer-default-export': OFF,
'comma-spacing': [
ERROR,
{
// 控制逗号前后的空格
before: false,
after: true,
},
],
'max-len': [ERROR, 200, { ignoreUrls: true }], // 强制一行的最大长度
'no-mixed-spaces-and-tabs': [
// 禁止空格和 tab 的混合缩进
'error',
'smart-tabs',
],
'no-dupe-class-members': ERROR, // 禁止类属性重载
'no-this-before-super': ERROR, // super前不允许用this
'no-dupe-args': ERROR,
'no-dupe-keys': ERROR,
'no-duplicate-case': ERROR,
'no-shadow': ERROR,
'brace-style': [
ERROR,
'1tbs',
{
allowSingleLine: true,
},
],
'no-const-assign': ERROR, // 禁止给const变量赋值
'no-duplicate-imports': ERROR, // 禁止重复引入模块
'react/jsx-key': ERROR, // 在数组或迭代器中验证JSX具有key属性
'react/jsx-no-duplicate-props': ERROR, // 防止JSX中重复的props
'react/jsx-no-undef': ERROR, // 在jsx中禁止使用为申明的变量
'react/no-unknown-property': ERROR, // 防止使用未知的dom属性
'react/react-in-jsx-scope': ERROR, // 防止忘记引入React
'react/jsx-filename-extension': [
ERROR,
{
extensions: ['.js', '.jsx'],
},
],
'react/prop-types': OFF,
'react/jsx-wrap-multilines': OFF,
'react/jsx-one-expression-per-line': OFF,
'react/jsx-props-no-spreading': OFF,
'react/state-in-constructor': OFF,
'react/static-property-placement': OFF,
'react/destructuring-assignment': OFF,
'react/no-array-index-key': 'warn',
'react-hooks/rules-of-hooks': 'error',
'react/jsx-closing-tag-location': OFF,
'react/forbid-prop-types': OFF,
'react/jsx-first-prop-new-line': [ERROR, 'multiline'],
'react/jsx-no-target-blank': OFF,
'react/jsx-uses-vars': ERROR,
'react/require-default-props': OFF,
'react/require-extension': OFF,
'react/self-closing-comp': OFF,
'react/sort-comp': OFF,
'react/jsx-fragments': OFF,
'require-yield': OFF,
},
settings: {
'import/resolver': {
webpack: {
config: './webpack/webpack-base-config.js',
},
},
},
};
- 配置
prettier
- 新建
.prettierrc,.prettierignore两个文件,分别写入以下配置
{
"printWidth": 200,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true, //使用单引号
"trailingComma": "all" //末尾添加分号
}
dist/
node_modules/
webpack/
package-lock.json
package.json
边栏推荐
- Numpy (data type)
- Pit encountered by fastjason
- Talk about the job experience of kotlin cooperation process
- ReturnJson,让返回数据多一些自定义数据或类名
- Bluetooth BT RF test (forwarding)
- Abstract factory pattern
- Deep Learning with Pytorch- A 60 Minute Blitz
- ACM intensive training graph theory exercise 3 in the summer vacation of 2020 [problem solving]
- Set, map and modularity
- qmlplugindump executable not found.It is required to generate the qmltypes file for VTK Qml
猜你喜欢

Design of mfc+mysql document data management system based on VS2010

Mysq database remote connection error, remote connection is not allowed

Idea shortcut key settings

float

7. know JNI and NDK

仿照微信Oauth2.0接入方案

Small program learning path 1 - getting to know small programs

JVM notes (III): analysis of JVM object creation and memory allocation mechanism

Cb/s Architecture - Implementation Based on cef3+mfc

Terminal -- Zsh of terminal three swordsmen
随机推荐
Function simplification principle: save if you can
Small program learning path 1 - getting to know small programs
Express file upload
小程序开发踩坑之旅
qmlplugindump executable not found.It is required to generate the qmltypes file for VTK Qml
Pytorch for former Torch users - Tensors
Deep understanding of continuation principle
Clickhouse installation (quick start)
Framework program of browser self-service terminal based on IE kernel
Express の Hello World
Net framework system requirements
Design of mfc+mysql document data management system based on VS2010
Tablet PC based ink handwriting recognition input method
MySQL directory
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.
Talk about the kotlin cooperation process and the difference between job and supervisorjob
RPC understanding
Deep understanding of kotlin collaboration context coroutinecontext
Invalid update: invalid number of sections. The number of sections contained in the table view after
Startup of MySQL green edition in Windows system