react項目配置Eslint

来源:https://www.cnblogs.com/UnfetteredMan/archive/2022/09/30/16745415.html
-Advertisement-
Play Games

React Or Taro 項目配置Eslint校驗 一、下載Eslint相關deps依賴項; npm install --save-dev eslint-plugin-prettier eslint-plugin-jsx-a11y eslint-config-airbnb 註意:由於eslint- ...


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
        }
    ]

 

時間如白駒過隙,忽然而已,且行且珍惜......
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Java資料庫的安裝和使用 1.資料庫的作用 一個問題:淘寶網、京東、微信抖音,都有各自的功能,那麼我們退出系統的時候,為什麼信息還在? 解決之道-文件,資料庫 為瞭解決上訴問題,使用更加利於管理數據東西-資料庫,他能更加有效地管理數據。 舉一個生活化的案例說明:如果說圖書館是保存書籍的,那麼資料庫 ...
  • LIKE操作符:通配符搜索只能用於文本欄位(字元串) 1、%通配符 1 select 2 col_name 3 from 4 table_name 5 where 6 col_name 7 like "%str" 2、_通配符 1 select 2 col_name 3 from 4 table_n ...
  • 一直使用Postgresql資料庫,有一張表是這樣的: DROP TABLE IF EXISTS "public"."devicedata"; CREATE TABLE "public"."devicedata" ( "Id" varchar(200) COLLATE "pg_catalog"."d ...
  • 據中國信通院發佈,2012年到2021年10年間,我國數字經濟規模由12萬億元增長到45.5萬億元,在整個GDP中的比重由21.6%提升至39.8%。順應時代發展新趨勢,“數據”成為新的生產要素已是毋庸置疑的共識。 如果說數據中台的崛起代表著企業數字化轉型從流程驅動走向數據驅動,從數字化走向智能化。 ...
  • 首發微信公眾號:SQL資料庫運維 原文鏈接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485212&idx=1&sn=450e9e94fa709b5eeff0de371c62072b&chksm=ea37536cdd40da7 ...
  • 現如今,手機錄屏是必不可少的能力之一。對於游戲領域作者來說,在平時直播玩游戲、製作攻略、操作集錦時,不方便切屏,這時在游戲內如果有一個錄製按鈕就可以隨時開啟,記錄下每個精彩瞬間,減少後期剪輯工作量;在直播App中開啟一鍵錄屏,不光方便主播後續的賬號運營與復盤,用戶也能隨時截取有意思的片段傳播在社交媒 ...
  • #背景 webpack構建過程中的hooks都有什麼呢?除了在網上看一些文章,還可以通過更直接的辦法,結合官方文檔快速讓你進入webpack的hook世界 寫一個入口文件 //index.js const webpack = require("webpack"); const path = requ ...
  • 語言基礎-變數 前言 從本篇博客開始 博主個人認為重要的知識點都會在在行前添加 ⭐ 來進行標識 變數 ECMASCRIPT變數是鬆散類型,意思是變數可以用於保存任何類型的數據。ECMASCRIPT中有三個關鍵字可以來聲明變數:var、let和const。 值得註意的是let和const只能在ES6以 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...