React + TypeScript項目的代碼檢查

配置TypeScript的ESLint設置

1: 安裝相關依賴

yarn add eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin --dev
eslint:ESLint的核心代碼庫
@typescript-eslint/parser:使得ESLint可以解析TypeScript的解析器
@typescript-eslint/eslint-plugin: TypeScript特有的ESLint的rules

2: 在項目根目錄下創(chuàng)建配置文件.eslintrc.js
//.eslintrc.js
module.exports =  {
  parser:  '@typescript-eslint/parser',  // Specifies the ESLint parser
  extends:  [
    'plugin:@typescript-eslint/recommended',  // Uses the recommended rules from the @typescript-eslint/eslint-plugin
  ],
 parserOptions:  {
    ecmaVersion:  2018,  // Allows for the parsing of modern ECMAScript features
    sourceType:  'module',  // Allows for the use of imports
  },
  rules:  {
    // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
    // e.g. "@typescript-eslint/explicit-function-return-type": "off",
  },
};

ESLint + TypeScript + React

如果是React+TypeScript的項目,也可以給React添加一個代碼檢查:eslint-plugin-react

安裝eslint-plugin-react

yarn add eslint-plugin-react --dev

添加相關配置
//.eslintrc.js
module.exports =  {
  parser:  '@typescript-eslint/parser',  // Specifies the ESLint parser
  extends:  [
    'plugin:react/recommended',  // Uses the recommended rules from @eslint-plugin-react
    'plugin:@typescript-eslint/recommended',  // Uses the recommended rules from @typescript-eslint/eslint-plugin
  ],
  parserOptions:  {
  ecmaVersion:  2018,  // Allows for the parsing of modern ECMAScript features
  sourceType:  'module',  // Allows for the use of imports
  ecmaFeatures:  {
    jsx:  false,  // Disallows for the parsing of JSX
  },
  },
  rules:  {
    // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
    // e.g. "@typescript-eslint/explicit-function-return-type": "off",
    semi: "error"
  },
  settings:  {
    react:  {
      version:  'detect',  // Tells eslint-plugin-react to automatically detect the version of React to use
    },
  },
};

在上面的配置里糙臼,我們添加了一條rule, semi: "error"。如果缺少分號,會報一個error。

添加lint命令

//package.json
  "scripts": {
   //other scripts
    "lint": "eslint --ext=ts,tsx src"
  }

以上script的意思,是我們只對/src這個目錄下面的,.ts和tsx文件進行l(wèi)int檢查辨泳。
假如我們有以下代碼

//./src/App.tsx
import React from 'react'

然后我們執(zhí)行命令:

yarn lint

我們會得到一個ESLint的error:


ESLint error

3: 配置代碼格式自動化 - prettier

ESLint是對你的源代碼根據(jù)配置的rules進行檢查,如果有問題會拋出錯誤玖院,提醒你去修改菠红。prettier是會強制把你的代碼按照配置好的規(guī)則進行format。

使用prettier需要安裝以下依賴:
yarn add prettier eslint-config-prettier eslint-plugin-prettier --dev

prettier: prettier的核心代碼庫
eslint-config-prettier: disable可能沖突的ESLint的規(guī)則
eslint-plugin-prettier: 這個插件使得代碼可以通過ESLint的--fix自動被修正

添加prettier的配置文件.prettierrc.js
//.prettierrc.js
module.exports =  {
  semi:  true,
  trailingComma:  'all',
  singleQuote:  true,
  printWidth:  120,
  tabWidth:  4,
};
.eslintrc.js做相應的調(diào)整
//.eslintrc.js
module.exports =  {
  parser:  '@typescript-eslint/parser',  // Specifies the ESLint parser
  extends:  [
    'plugin:@typescript-eslint/recommended',  // Uses the recommended rules from the @typescript-eslint/eslint-plugin
    'prettier/@typescript-eslint',  // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
    'plugin:prettier/recommended',  // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
  ],
  parserOptions:  {
    ecmaVersion:  2018,  // Allows for the parsing of modern ECMAScript features
    sourceType:  'module',  // Allows for the use of imports
  },
};
修改lint的命令难菌,添加--fix參數(shù)
//package.json
"scripts": {
   //other scripts
    "lint": "eslint --ext=ts,tsx src",
    "lintfix": "eslint --fix, --ext=ts,tsx src"
  }

我們添加了一個命令"lintfix": "eslint --fix, --ext=ts,tsx src"试溯,相比上面一個lint,我們增加了--fix郊酒。因為之前我們安裝了eslint-plugin-prettier耍共,使得--fix可以自動fix一些ESLint 的error。
比如猎塞,在prettier.config.js文件里面试读,我們配置了tabWidth: 4,如果我們代碼里面有縮進是2格的荠耽,當我們run了yarn lintfix命令钩骇,這個2格的縮進就會被自動修正為4格。

4: 在代碼提交之前強制代碼格式檢查 - husky && lint-staged

為了保證有問題的代碼不被提交铝量,我們可以在提交之前倘屹,先跑一下lint檢查,如果lint有error慢叨,這次的commit就不成功纽匙。我們可以使用husky和lint-staged來實現(xiàn)以上功能。

安裝husky和lint-staged
yarn add husky lint-staged --dev
創(chuàng)建husky的配置文件husky.config.js
module.exports = {
  hooks: {
    'pre-commit': 'lint-staged', //call lint-staged in git pre-commit hooks(before every commit)
    'pre-push': ''
  }
};

在這個文件里面我們有看到'pre-commit'和'pre-push'拍谐,他們倆都是git提供的hook烛缔,git還有一些其他的hook馏段。這里我們先只對pre-commit進行配置。
這個文件配置的意思就是践瓷,在commit之前(也就是pre-commit這個hook)院喜,先去調(diào)用lint-staged.如果lint-staged有error,這次的commit就不會成功晕翠。
那接下來我們看一下lint-staged的配置

創(chuàng)建lint-staged的配置文件lint-staged.config.js
//lint-staged.config.js
module.exports = {
    "src/*.+(ts|tsx)": ["eslint --fix"],
    "*.+(json|scss|md)": ["prettier --write"]
};

以上配置的意思就是我們給lint-staged安排的task包括兩個:
1 對一些.ts, .tsx文件執(zhí)行eslint --fixd的命令
2 對.json, .scss, .md文件執(zhí)行prettier --write命令
所以喷舀,以上husky和lint-staged結(jié)合起來的配置達到的效果就是:如果你執(zhí)行g(shù)it commit, 會觸發(fā)eslint的檢查,如果有錯誤淋肾,這次commit就不會成功硫麻。


一次git commit因為eslint檢查有錯誤而失敗

以上就是采用ESLint + prettier + husky + lint-staged來做一些代碼格式統(tǒng)一的例子。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末樊卓,一起剝皮案震驚了整個濱河市拿愧,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌简识,老刑警劉巖赶掖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件感猛,死亡現(xiàn)場離奇詭異七扰,居然都是意外死亡,警方通過查閱死者的電腦和手機陪白,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門颈走,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人咱士,你說我怎么就攤上這事立由。” “怎么了序厉?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵锐膜,是天一觀的道長。 經(jīng)常有香客問我弛房,道長道盏,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任文捶,我火速辦了婚禮荷逞,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘粹排。我一直安慰自己种远,他們只是感情好,可當我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布顽耳。 她就那樣靜靜地躺著坠敷,像睡著了一般妙同。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上常拓,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天渐溶,我揣著相機與錄音,去河邊找鬼弄抬。 笑死茎辐,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的掂恕。 我是一名探鬼主播拖陆,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼懊亡!你這毒婦竟也來了依啰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤店枣,失蹤者是張志新(化名)和其女友劉穎速警,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鸯两,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡闷旧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了钧唐。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片忙灼。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖钝侠,靈堂內(nèi)的尸體忽然破棺而出该园,到底是詐尸還是另有隱情,我是刑警寧澤帅韧,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布里初,位于F島的核電站,受9級特大地震影響忽舟,放射性物質(zhì)發(fā)生泄漏双妨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一萧诫、第九天 我趴在偏房一處隱蔽的房頂上張望斥难。 院中可真熱鬧,春花似錦帘饶、人聲如沸哑诊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽镀裤。三九已至竞阐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間暑劝,已是汗流浹背骆莹。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留担猛,地道東北人幕垦。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像傅联,于是被迫代替她去往敵國和親先改。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容