溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解React開發(fā)必不可少的eslint配置

發(fā)布時(shí)間:2020-09-17 21:24:22 來源:腳本之家 閱讀:263 作者:二月 欄目:web開發(fā)

Eslint介紹

Eslint 是一個(gè)JavaScript驗(yàn)證工具,有了它可以讓你的編輯器像ide一樣進(jìn)行一些靜態(tài)的錯(cuò)誤提示功能.

安裝eslint

$ npm install eslint -g

項(xiàng)目需要安裝的插件

  1. "babel-eslint": "^8.0.3",
  2. "eslint": "^4.13.1",
  3. "eslint-plugin-react": "^7.5.1",

配置詳情

下面的配置涵蓋了開發(fā)者所需要的絕大部分信息,rules中的值0、1、2分別表示不開啟檢查、警告、錯(cuò)誤。你可以看到下面有些是0,如果有需要開啟檢查,可以自己修改為1或者2。

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"], //單引號(hào)
    "no-console": 0, //不禁用console
    "no-debugger": 2, //禁用debugger
    "no-var": 0, //對(duì)var警告
    "semi": 0, //不強(qiáng)制使用分號(hào)
    "no-irregular-whitespace": 0, //不規(guī)則的空白不允許
    "no-trailing-spaces": 1, //一行結(jié)束后面有空格就發(fā)出警告
    "eol-last": 0, //文件以單一的換行符結(jié)束
    "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不能有聲明后未被使用的變量或參數(shù)
    "no-underscore-dangle": 0, //標(biāo)識(shí)符不能以_開頭或結(jié)尾
    "no-alert": 2, //禁止使用alert confirm prompt
    "no-lone-blocks": 0, //禁止不必要的嵌套塊
    "no-class-assign": 2, //禁止給類賦值
    "no-cond-assign": 2, //禁止在條件表達(dá)式中使用賦值語句
    "no-const-assign": 2, //禁止修改const聲明的變量
    "no-delete-var": 2, //不能對(duì)var聲明的變量使用delete操作符
    "no-dupe-keys": 2, //在創(chuàng)建對(duì)象字面量時(shí)不允許鍵重復(fù)
    "no-duplicate-case": 2, //switch中的case標(biāo)簽不能重復(fù)
    "no-dupe-args": 2, //函數(shù)參數(shù)不能重復(fù)
    "no-empty": 2, //塊語句中的內(nèi)容不能為空
    "no-func-assign": 2, //禁止重復(fù)的函數(shù)聲明
    "no-invalid-this": 0, //禁止無效的this,只能用在構(gòu)造器,類,對(duì)象字面量
    "no-redeclare": 2, //禁止重復(fù)聲明變量
    "no-spaced-func": 2, //函數(shù)調(diào)用時(shí) 函數(shù)名與()之間不能有空格
    "no-this-before-super": 0, //在調(diào)用super()之前不能使用this或super
    "no-undef": 2, //不能有未定義的變量
    "no-use-before-define": 2, //未定義前不能使用
    "camelcase": 0, //強(qiáng)制駝峰法命名
    "jsx-quotes": [2, "prefer-double"], //強(qiáng)制在JSX屬性(jsx-quotes)中一致使用雙引號(hào)
    "react/display-name": 0, //防止在React組件定義中丟失displayName
    "react/forbid-prop-types": [2, {"forbid": ["any"]}], //禁止某些propTypes
    "react/jsx-boolean-value": 2, //在JSX中強(qiáng)制布爾屬性符號(hào)
    "react/jsx-closing-bracket-location": 1, //在JSX中驗(yàn)證右括號(hào)位置
    "react/jsx-curly-spacing": [2, {"when": "never", "children": true}], //在JSX屬性和表達(dá)式中加強(qiáng)或禁止大括號(hào)內(nèi)的空格。
    "react/jsx-indent-props": [2, 4], //驗(yàn)證JSX中的props縮進(jìn)
    "react/jsx-key": 2, //在數(shù)組或迭代器中驗(yàn)證JSX具有key屬性
    "react/jsx-max-props-per-line": [1, {"maximum": 1}], // 限制JSX中單行上的props的最大數(shù)量
    "react/jsx-no-bind": 0, //JSX中不允許使用箭頭函數(shù)和bind
    "react/jsx-no-duplicate-props": 2, //防止在JSX中重復(fù)的props
    "react/jsx-no-literals": 0, //防止使用未包裝的JSX字符串
    "react/jsx-no-undef": 1, //在JSX中禁止未聲明的變量
    "react/jsx-pascal-case": 0, //為用戶定義的JSX組件強(qiáng)制使用PascalCase
    "react/jsx-sort-props": 2, //強(qiáng)化props按字母排序
    "react/jsx-uses-react": 1, //防止反應(yīng)被錯(cuò)誤地標(biāo)記為未使用
    "react/jsx-uses-vars": 2, //防止在JSX中使用的變量被錯(cuò)誤地標(biāo)記為未使用
    "react/no-danger": 0, //防止使用危險(xiǎn)的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, //防止每個(gè)文件有多個(gè)組件定義
    "react/no-set-state": 0, //防止使用setState
    "react/no-unknown-property": 2, //防止使用未知的DOM屬性
    "react/prefer-es6-class": 2, //為React組件強(qiáng)制執(zhí)行ES5或ES6類
    "react/prop-types": 0, //防止在React組件定義中丟失props驗(yàn)證
    "react/react-in-jsx-scope": 2, //使用JSX時(shí)防止丟失React
    "react/self-closing-comp": 0, //防止沒有children的組件的額外結(jié)束標(biāo)簽
    "react/sort-comp": 2, //強(qiáng)制組件方法順序
    "no-extra-boolean-cast": 0, //禁止不必要的bool轉(zhuǎn)換
    "react/no-array-index-key": 0, //防止在數(shù)組中遍歷中使用數(shù)組key做索引
    "react/no-deprecated": 1, //不使用棄用的方法
    "react/jsx-equals-spacing": 2, //在JSX屬性中強(qiáng)制或禁止等號(hào)周圍的空格
    "no-unreachable": 1, //不能有無法執(zhí)行的代碼
    "comma-dangle": 2, //對(duì)象字面量項(xiàng)尾不能有逗號(hào)
    "no-mixed-spaces-and-tabs": 0, //禁止混用tab和空格
    "prefer-arrow-callback": 0, //比較喜歡箭頭回調(diào)
    "arrow-parens": 0, //箭頭函數(shù)用小括號(hào)括起來
    "arrow-spacing": 0 //=>的前/后括號(hào)
  },
  "settings": {
    "import/ignore": [
      "node_modules"
    ]
  }
};

哦,老天,你還希望看到更多的react檢查器,那就去 eslint-plugin-react 的github文檔去慢慢翻譯吧。

某些文件關(guān)閉eslint檢查

你不總是希望所有的文件都開啟eslint檢查,那么,給單獨(dú)的js文件關(guān)閉eslint的方式,只需要在該文件的最頂部加上一段注釋。

/*eslint-disable*/
function test() {
  return true
}

給某一行js代碼關(guān)閉eslint檢查

關(guān)閉整個(gè)js文件的行為有點(diǎn)暴力,別擔(dān)心,你還可以只給其中某段代碼關(guān)閉eslint。

// eslint-disable-next-line
alert('foo')

eslint配置文件類型

eslint配置文件類型不只有js和json,其實(shí)包括下面這些:

  1. .eslintrc.js
  2. .eslintrc.yaml
  3. .eslintrc.yml
  4. .eslintrc.json
  5. .eslintrc
  6. package.json

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI