react項目配置保存文件時進行eslint的自動格式化

第一步,安裝依賴

npm i eslint babel-eslint --save-dev

第二步,在根目錄新建.eslintrc.js .eslintignore 兩個文件

我們需要一個eslint的配置

安裝依賴

npm install --save-dev eslint babel-eslint eslint-plugin-react eslint-plugin-import eslint-plugin-jsx-a11y

配置.eslintrc.js

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "globals": {
        "$": true,
        "process": true,
        "__dirname": true
    },
    "parser": "babel-eslint",
    "parserOptions": {
        "ecmaFeatures": {
            "experimentalObjectRestSpread": true,
            "jsx": true
        },
        "sourceType": "module",
        "ecmaVersion": 7
    },
    "plugins": [
        "react"
    ],
    "rules": {
        "quotes": [2, "single"], //單引號
        "no-console": 0, //不禁用console
        "no-debugger": 2, //禁用debugger
        "no-var": 0, //對var警告
        "semi": 0, //不強制使用分號
        "no-irregular-whitespace": 0, //不規則的空白不允許
        "no-trailing-spaces": 1, //一行結束後面有空格就發出警告
        "eol-last": 0, //文件以單一的換行符結束
        "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不能有聲明後未被使用的變量或參數
        "no-underscore-dangle": 0, //標識符不能以_開頭或結尾
        "no-alert": 2, //禁止使用alert confirm prompt
        "no-lone-blocks": 0, //禁止不必要的嵌套塊
        "no-class-assign": 2, //禁止給類賦值
        "no-cond-assign": 2, //禁止在條件表達式中使用賦值語句
        "no-const-assign": 2, //禁止修改const聲明的變量
        "no-delete-var": 2, //不能對var聲明的變量使用delete操作符
        "no-dupe-keys": 2, //在創建對象字面量時不允許鍵重複
        "no-duplicate-case": 2, //switch中的case標籤不能重複
        "no-dupe-args": 2, //函數參數不能重複
        "no-empty": 2, //塊語句中的內容不能爲空
        "no-func-assign": 2, //禁止重複的函數聲明
        "no-invalid-this": 0, //禁止無效的this,只能用在構造器,類,對象字面量
        "no-redeclare": 2, //禁止重複聲明變量
        "no-spaced-func": 2, //函數調用時 函數名與()之間不能有空格
        "no-this-before-super": 0, //在調用super()之前不能使用this或super
        "no-undef": 2, //不能有未定義的變量
        "no-use-before-define": 2, //未定義前不能使用
        "camelcase": 0, //強制駝峯法命名
        "jsx-quotes": [2, "prefer-double"], //強制在JSX屬性(jsx-quotes)中一致使用雙引號
        "react/display-name": 0, //防止在React組件定義中丟失displayName
        "react/forbid-prop-types": [2, {"forbid": ["any"]}], //禁止某些propTypes
        "react/jsx-boolean-value": 2, //在JSX中強制布爾屬性符號
        "react/jsx-closing-bracket-location": 1, //在JSX中驗證右括號位置
        "react/jsx-curly-spacing": [2, {"when": "never", "children": true}], //在JSX屬性和表達式中加強或禁止大括號內的空格。
        "react/jsx-indent-props": [2, 4], //驗證JSX中的props縮進
        "react/jsx-key": 2, //在數組或迭代器中驗證JSX具有key屬性
        "react/jsx-max-props-per-line": [1, {"maximum": 1}], // 限制JSX中單行上的props的最大數量
        "react/jsx-no-bind": 0, //JSX中不允許使用箭頭函數和bind
        "react/jsx-no-duplicate-props": 2, //防止在JSX中重複的props
        "react/jsx-no-literals": 0, //防止使用未包裝的JSX字符串
        "react/jsx-no-undef": 1, //在JSX中禁止未聲明的變量
        "react/jsx-pascal-case": 0, //爲用戶定義的JSX組件強制使用PascalCase
        "react/jsx-sort-props": 2, //強化props按字母排序
        "react/jsx-uses-react": 1, //防止反應被錯誤地標記爲未使用
        "react/jsx-uses-vars": 2, //防止在JSX中使用的變量被錯誤地標記爲未使用
        "react/no-danger": 0, //防止使用危險的JSX屬性
        "react/no-did-mount-set-state": 0, //防止在componentDidMount中使用setState
        "react/no-did-update-set-state": 1, //防止在componentDidUpdate中使用setState
        "react/no-direct-mutation-state": 2, //防止this.state的直接變異
        "react/no-multi-comp": 2, //防止每個文件有多個組件定義
        "react/no-set-state": 0, //防止使用setState
        "react/no-unknown-property": 2, //防止使用未知的DOM屬性
        "react/prefer-es6-class": 2, //爲React組件強制執行ES5或ES6類
        "react/prop-types": 0, //防止在React組件定義中丟失props驗證
        "react/react-in-jsx-scope": 2, //使用JSX時防止丟失React
        "react/self-closing-comp": 0, //防止沒有children的組件的額外結束標籤
        "react/sort-comp": 2, //強制組件方法順序
        "no-extra-boolean-cast": 0, //禁止不必要的bool轉換
        "react/no-array-index-key": 0, //防止在數組中遍歷中使用數組key做索引
        "react/no-deprecated": 1, //不使用棄用的方法
        "react/jsx-equals-spacing": 2, //在JSX屬性中強制或禁止等號周圍的空格
        "no-unreachable": 1, //不能有無法執行的代碼
        "comma-dangle": 2, //對象字面量項尾不能有逗號
        "no-mixed-spaces-and-tabs": 0, //禁止混用tab和空格
        "prefer-arrow-callback": 0, //比較喜歡箭頭回調
        "arrow-parens": 0, //箭頭函數用小括號括起來
        "arrow-spacing": 0 //=>的前/後括號
    },
    "settings": {
        "import/ignore": [
            "node_modules"
        ]
    }
};

可以加上自己喜歡的配置
下一步,配置vscode的setting.json

...
"eslint.format.enable": true,
    "eslint.run": "onSave",
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "editor.formatOnPaste": false,
    "editor.formatOnType": true,
    "files.autoSave": "onFocusChange",
...

ps:setting.json中有個配置項 “editor.formatOnSave”: true,不要設爲true,因爲eslint和你安裝的beauty或者prettier可能有衝突

最後在package.json裏面配置

"lint": "eslint --ext .js ./src"

意思是檢查src文件夾下的所有js文件
咱之前配置了.eslintignore文件,裏面同樣可以填我們需要忽略檢查的文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章