2-5 Validate all files are formatted when linting

Validate all files are formatted when linting

You can’t force everyone on your project to use the prettier integration for their editor, so let’s add a validation script to verify that prettier has been run on all project files.

Fortunately for us, Prettier accepts a --list-different flag that you can use that will throw an error when code is not up to the standard of your project.

提取碼:gpbq

觀看視頻

Code

Transcript

In a project with multiple people on the team, you're going to have some people who have the editor integration going on with their project and everything looks great, and you'll have some people who don't. Let's see what we can do about making sure that people run the format script.

I'm going to open my default user settings. I'll disable this format on save. Now I can make some changes. Hit the save key. It doesn't format automatically.

example.js

const name = 'Freddy'
typeof name === 'string'

if (!('serviceWorker' in navigator)) {
        // you have an old browser :-(
}

const greeting = 'hello'
console.log('${greeting} world!')
;[(1, 2, 3)].forEach(x => console.log(x))

This is what it would look like, potentially, if somebody who doesn't use Prettier in their editor went ahead and tried to save some code. Because ESLint isn't checking this kind of formatting for us, we want to have some sort of automated validation to make sure that things have been formatted properly.

One thing that we can do is if I run Prettier with npx and run it on source example, npx prettier src/example.js, then I can add the flag --list-different. It's going to list for me all the files that would be changed by Prettier.

Terminal Input

npx prettier src/example.js --list-different

I'll go ahead and --write this. Then I'll run Prettier with npx prettier src/example again. I don't see any files listed.

Terminal Input

npx prettier src/example.js --write
npx prettier src/example.js --list-different

This script will exit with a non-zero exit code, meaning the script will fail if any of these files are listed. We can make it part of a validation script. I'm going to make a new script in package.json, called validate. We'll npm run lint. Then we'll run our Prettier script here. We'll copy this. Instead of --write, we'll run --list-different.

package.json

"scripts": {
    "lint": "eslint src",
    "format": "prettier --write \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
    "validate": "npm run lint && prettier --list-different \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
},

With that now, let's go ahead. I'll make a big mess of this. We'll throw a couple things all over the place.

example.js
[圖片上傳失敗...(image-b4564e-1554385186496)]

Now if I run npm run validate, I'll see my ESLint runs.

Terminal Input

npm run validate

Everything passes ESLint, but Prettier is saying, "Hey, there's a file here that should have been formatted but wasn't." Then I can run npm run format. Then I'll run the validate script again. Everything passes fine.

Terminal Input

npm run format
npm run validate

This is a little bit uncomfortable for me. We've got a huge string of text in package.json that we're repeating in both of these scripts. If we wanted to add a new file, we'd need to make sure to add it to both of them.

What I'm going to do is just a quick little clean-up. We'll add a prettier script. The prettier script will be responsible for this part. The flags will be handled by the individual scripts.

package.json

"scripts": {
    "lint": "eslint src",
    "format": "prettier --write \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
    "prettier": "prettier \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
    "validate": "npm run lint && prettier --list-different \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\""
},

Here, for our format, we'll run npm run prettier. Then we'll forward along the write flag, -- --write. For our validate script, we're going to run npm run prettier. We'll forward along the --list-different flag. We'll get rid of that. Now things look a little bit cleaner.

"scripts": {
    "lint": "eslint src",
    "format": "npm run prettier -- --write",
    "prettier": "prettier \"**/*.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|graphql|mdx)\"",
    "validate": "npm run lint && npm run prettier -- --list-different"
},

In review, to validate that all the files in the project have been formatted by Prettier, we can use a --list-different flag when we run Prettier. If there are any files that would be different if Prettier were to format them, then it will fail the script. Otherwise, the script will pass.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市窃躲,隨后出現(xiàn)的幾起案子方椎,更是在濱河造成了極大的恐慌,老刑警劉巖桐智,帶你破解...
    沈念sama閱讀 211,639評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件末早,死亡現(xiàn)場離奇詭異,居然都是意外死亡说庭,警方通過查閱死者的電腦和手機(jī)然磷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,277評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刊驴,“玉大人姿搜,你說我怎么就攤上這事±υ鳎” “怎么了舅柜?”我有些...
    開封第一講書人閱讀 157,221評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長躲惰。 經(jīng)常有香客問我致份,道長,這世上最難降的妖魔是什么础拨? 我笑而不...
    開封第一講書人閱讀 56,474評論 1 283
  • 正文 為了忘掉前任氮块,我火速辦了婚禮,結(jié)果婚禮上诡宗,老公的妹妹穿的比我還像新娘滔蝉。我一直安慰自己,他們只是感情好塔沃,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,570評論 6 386
  • 文/花漫 我一把揭開白布蝠引。 她就那樣靜靜地躺著,像睡著了一般蛀柴。 火紅的嫁衣襯著肌膚如雪螃概。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,816評論 1 290
  • 那天鸽疾,我揣著相機(jī)與錄音吊洼,去河邊找鬼。 笑死肮韧,一個(gè)胖子當(dāng)著我的面吹牛融蹂,可吹牛的內(nèi)容都是我干的旺订。 我是一名探鬼主播,決...
    沈念sama閱讀 38,957評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼超燃,長吁一口氣:“原來是場噩夢啊……” “哼区拳!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起意乓,我...
    開封第一講書人閱讀 37,718評論 0 266
  • 序言:老撾萬榮一對情侶失蹤樱调,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后届良,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體笆凌,經(jīng)...
    沈念sama閱讀 44,176評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,511評論 2 327
  • 正文 我和宋清朗相戀三年士葫,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了乞而。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,646評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡慢显,死狀恐怖爪模,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情荚藻,我是刑警寧澤屋灌,帶...
    沈念sama閱讀 34,322評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站应狱,受9級特大地震影響共郭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜疾呻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,934評論 3 313
  • 文/蒙蒙 一除嘹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧罐韩,春花似錦憾赁、人聲如沸污朽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,755評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蟆肆。三九已至斥滤,卻和暖如春缤灵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,987評論 1 266
  • 我被黑心中介騙來泰國打工毁靶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人幕与。 一個(gè)月前我還...
    沈念sama閱讀 46,358評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像坛怪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子股囊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,514評論 2 348

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

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,309評論 0 10
  • 老師讓我們寫自己的心里話袜匿,我寫的是關(guān)于踢足球的建議。過了一天稚疹,老師竟然要讀我的作文居灯,剛一讀同學(xué)們就嚷嚷,我都害...
    范雯寧閱讀 153評論 1 0
  • 我是日記星球第345號星寶寶内狗,正在參加日記星球第十五期21天蛻變之旅怪嫌,這是我的第六十五篇原創(chuàng)日記。 人身保險(xiǎn)合同是...
    重塑牛仔閱讀 565評論 0 1
  • 今天又是要下鄉(xiāng)去柳沙。 可是我不想下鄉(xiāng)把颐稹!老爸赂鲤,你為什么要強(qiáng)迫我下鄉(xiāng)呢川背?就不能讓我自己做決定嗎?真想自己做決定蛤袒,我一說...
    軌跡的迷途閱讀 238評論 0 2
  • 沉默的人熄云,往往是狠角色 物道 關(guān)注 2018-09· 1字?jǐn)?shù) 1826 · 閱讀 5955 · 日記本 無論在火車...
    你是我的依賴閱讀 199評論 0 0