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
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...