首页 > 其他分享 >react项目配置Eslint

react项目配置Eslint

时间:2022-09-30 16:58:03浏览次数:50  
标签:off no true 配置 react Eslint error jsx

React Or Taro 项目配置Eslint校验

一、下载Eslint相关deps依赖项;

npm install --save-dev  eslint-plugin-prettier  eslint-plugin-jsx-a11y eslint-config-airbnb
注意由于eslint-config-airbnb目前版本已经超过19,会出现一个小问题,箭头函数和命名函数会被Eslint 提示冲突,这是由于19版本的升级导致的解决方案目前有两个

1,在.eslintrc.js 文件中添加了以下规则

    'react/function-component-definition': [2, { namedComponents: 'arrow-function', unnamedComponents: 'arrow-function' }],

    这样,eslint 将只接受组件的箭头函数,而不是函数表达式(默认情况下);

2,可以改变函数写法来解决冲突,这样是最简单的

    箭头函数:const Demo = () : ReactNode => <div>demo</div>;

    function Demo () {
        return <div>demo</div>
    }
3,通过改变版本去解决冲突,

    直接下载指定的版本的包就可以解决,现在自测18.1.0可以满足当下需求;

二、配置.eslintrc.js文件(将默认的 .eslintrc 文件改完默认导出 .eslintrc.js)

// .eslintrc.js 如果编辑器module.exports报错,可尝试重启编辑器

module.exports = { root: true, extends: ['taro/react', 'airbnb', 'airbnb/hooks'], parser: '@typescript-eslint/parser', // ESLint 默认使用 esprima 作为其解析器,也可以在配置文件中指定一个不同的解析器(它必须是一个 Node 模块,且它必须符合 parser interface) plugins: [ 'react', 'react-hooks', 'import', 'jsx-a11y' ], rules: { 'react/function-component-definition': [2, { namedComponents: 'arrow-function', unnamedComponents: 'arrow-function' }], 'react/jsx-uses-react': 'off', 'react/react-in-jsx-scope': 'off', 'no-undef': 'off', 'import/no-extraneous-dependencies': 'off', 'import/prefer-default-export': 'off', 'import/no-unresolved': 'off', 'no-unused-vars': 'off', 'import/extensions': 'off', 'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx', '.ts', '.tsx'] }], indent: ['error', 2, { SwitchCase: 1 }], 'linebreak-style': 'off', quotes: ['error', 'single'], semi: ['error', 'always'], 'dot-notation': 'off', 'spaced-comment': 'off', 'comma-dangle': 'off', 'space-before-function-paren': ['error', 'never'], 'one-var': 'off', 'one-var-declaration-per-line': 'off', 'no-use-before-define': 'off', 'no-restricted-globals': ['error', 'history'], 'class-methods-use-this': 'off', radix: 'off', 'global-require': 'error', 'default-case': 'off', 'no-param-reassign': 'error', 'consistent-return': 'off', 'no-script-url': 'error', 'no-else-return': 'error', 'no-restricted-syntax': 'error', 'no-extend-native': 'error', 'no-return-assign': 'off', 'no-underscore-dangle': 'off', 'no-unused-expressions': ['error', { allowShortCircuit: true, allowTernary: true, allowTaggedTemplates: true }], 'max-len': ['error', { code: 200, ignoreComments: true, ignoreUrls: true, ignoreTemplateLiterals: true }], 'jsx-quotes': ['error', 'prefer-single'], 'jsx-a11y/alt-text': 'off', 'jsx-a11y/no-autofocus': 'off', 'jsx-a11y/label-has-for': 'off', 'jsx-a11y/label-has-associated-control': 'off', 'jsx-a11y/media-has-caption': 'off', 'jsx-a11y/click-events-have-key-events': 'off', 'jsx-a11y/no-noninteractive-element-interactions': 'off', 'jsx-a11y/no-static-element-interactions': 'off', 'jsx-a11y/anchor-is-valid': 'off', 'prefer-destructuring': 'off', 'no-plusplus': 'off', 'no-trailing-spaces': 'off', 'react/prop-types': 'off', 'react/jsx-tag-spacing': 'off', 'import/no-duplicates': 'off', 'import/order': 'off', 'import/no-dynamic-require': 'off', 'react/no-did-update-set-state': 'error', 'react/no-unused-state': 'error', 'react/no-find-dom-node': 'error', 'react/forbid-prop-types': 'off', 'react/jsx-indent-props': 'off', 'react/no-array-index-key': 'off', 'react/require-default-props': 'off', 'react/sort-comp': 'off', 'react/jsx-wrap-multilines': 'off', 'react/destructuring-assignment': 'off', 'react/jsx-closing-bracket-location': 'off', 'react/jsx-first-prop-new-line': 'off', 'react/no-multi-comp': 'off', 'react/jsx-one-expression-per-line': 'off', 'react/no-access-state-in-setstate': 'off', 'react/jsx-no-bind': 'off', 'react/jsx-indent': [2, 2], 'react/no-unescaped-entities': 'off', 'no-prototype-builtins': 'error', 'no-nested-ternary': 'error', 'react-hooks/exhaustive-deps': 'off', 'react/jsx-no-target-blank': 'error', 'no-console': ['off'], 'react/jsx-props-no-spreading': 'off', 'react-hooks/rules-of-hooks': 'error', 'no-useless-constructor': 'off', 'no-empty-function': 'off', 'object-curly-newline': 'off', 'react/no-danger': 'off', 'react/button-has-type': 'off', 'no-multiple-empty-lines': 'off', 'no-useless-escape': 'off', 'react/no-unused-prop-types': 'off', 'react/default-props-match-prop-types': 'off', camelcase: 'off', }, };

 

三、新建 .eslintignore 文件,配置eslint不检测的文件

// 没啥好讲的,直接将不需要检测的路径或文件丢在这里就ok
/.git/
/.Vscode/
node_modules//
dist/

4.新建.prettierrc.js文件

module.exports = {
    tabWidth: 4,
    singleQuote: true,
    jsxSingleQuote: true,
    printWidth: 140,
    endOfLine: 'auto',
};

 5.修改编辑器的配置文件.editorconfig

.editorconfig 文件用于 统一不同编辑器的代码风格的配置。比如我们要控制一个多人维护的项目缩进统一用4个空格:indent_size = 4

# http://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = crlf

[*.md]
trim_trailing_whitespace = false

6.修改vscode编辑器的setting.json文件

// jsx自动修复
    "editor.formatOnSave": true,
    // 保存自动修复
    "eslint.autoFixOnSave": true,
    "eslint.run": "onSave",
    "javascript.format.enable": false,
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "editor.formatOnPaste": false,
    "editor.formatOnType": true,
    "files.autoSave": "onFocusChange",
    "eslint.nodePath": "",
    "files.trimTrailingWhitespace": true,

    "eslint.validate": [
        "javascript",
        "javascriptreact",
        {
        "language": "html",
        "autoFix": true
        },
        {
        "language": "react",
        "autoFix": true
        }
    ]

 

标签:off,no,true,配置,react,Eslint,error,jsx
From: https://www.cnblogs.com/UnfetteredMan/p/16745415.html

相关文章

  • SpringBoot 的 jar 包及应用配置文件加载
    SpringBoot打包如果你手动使用jar命令打过jar包(如下图)那么一定知道该jar包与使用spring-boot-maven-plugin插件打出来的jar包(如下图)不一样。SpringBoot运......
  • 如何修改redis配置文件(添加密码验证)
    操作步骤:1:找到redis的配置文件,2:停止redis服务,3:修改配置文件,4启动服务1,找到redis的配置文件首先直接试试whereisredis.conf 看能不能返回路径试过2个不同......
  • 迅为RK3588开发板编译环境Ubuntu20.04 编译配置增加交换内存
    迅为提供的编译环境Ubuntu20.04默认配置了交换内存是9G,如果在编译过程中,因内存不够而编译报错,可以参考本小节进行设置。这里举例分配5G交换内存。在开始之前,使用命令检......
  • EF Core 关系配置 多对多
    一对多和一对一都只需要在表中增加外键列,但是在多对多关系中,我们必须引入一张中间表保存两张表之间的关联关系。多对多:不需要声明中间表实体,也不需要声明外键。实体:pu......
  • Truenas Core 配置端口聚合
    最近使用TruenasCore非常舒服,于是准备扩大规模,设备自带的2.5G网口就略显不足,于是准备上双万兆网卡,然后做一个端口聚合装上后成功开机,打开WEB段界面发现已经成功识别设备......
  • 远程配置网关采集网关BL102可对接阿里云华为云
    一、后台配置1.1注册账号在浏览器打开后台管理的页面后,点击页面上的注册帐号信息,然后依次输入内容后,点击注册按钮即可。注册成功后,返回主面页,输入帐号密码登陆系统。登录系......
  • 服务器租用怎么选择硬件配置
    1、根据规模测试压力像服务器这样的物品一般都是一次性的确定好成本,所以企业在选择的时候一定要多想一下。需要做与制造厂商多次协商与谈判,在确定购买的时候也要测试......
  • 在 nginx 中配置 HSTS 并禁用 TLS 1.0、1.1
    可以使用以下地址工具按需生成nginx配置https://ssl-config.mozilla.org/#server=nginxHSTS的配置为:#HSTS(ngx_http_headers_moduleisrequired)(63072000seco......
  • Git 配置文件 浅析
    config文件位置仓库级配置文件:在所创建仓库下./.git/config针对该仓库。在进入仓库的情况下,你可以传递--local选项让Git强制读写此文件,默认情况下用的就是它。......
  • SpringBoot2 不同版本中 文件上传大小配置
    由于springboot具有几个版本,不同版本对于文件上传最大限制的配置也有所不同。所以要注意springboot本身的版本,不然会一直报错#在springboot1.3版本中:multipart.maxFil......