自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

花十分鐘的時間武裝你的代碼庫

開發(fā) 前端
當我們的代碼庫有很多人維護時,經(jīng)常會出現(xiàn)代碼風(fēng)格不一致或者代碼質(zhì)量不過關(guān),本文正是為了解決這個問題而生,閱讀本篇文章并不需要很長時間,如果你的代碼庫還沒有進行這些配置,正是你大展身手的好時機,武裝一下你的代碼庫。

當我們的代碼庫有很多人維護時,經(jīng)常會出現(xiàn)代碼風(fēng)格不一致或者代碼質(zhì)量不過關(guān),提交信息雜亂的情況,當然啦,即使是一個人的代碼庫,有的時候,自己寫代碼時不太注意細節(jié),也會出現(xiàn)風(fēng)格不一致的情況。

本文正是為了解決這個問題而生,閱讀本篇文章并不需要很長時間,如果你的代碼庫還沒有進行這些配置,正是你大展身手的好時機,武裝一下你的代碼庫。

1. 規(guī)范 commit 信息

首先,看下 angular 的代碼庫的 commit 記錄,如圖:

我們可以利用 commitizen 和 husky 來規(guī)范代碼庫的 commit。

安裝以下依賴:

  1. npm install @commitlint/cli @commitlint/config-conventional husky  -D 

如果你還沒有安裝過 commitizen,那么先全局安裝:

  1. npm install commitizen -g 

在 package.json 中增加 husky 字段。

  1.     "husky": { 
  2.         "hooks": { 
  3.             "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" 
  4.         } 
  5.     }, 

husky 是 git hook 工具,使用 husky,我們可以方便的在 package.json 中配置 git hook 腳本,例如: pre-commit、 pre-push、 commit-msg 等的。

(1) 創(chuàng)建 commitlint.config.js 文件

  1. module.exports = { 
  2.     extends: ["@commitlint/config-conventional"], 
  3. }; 

此刻開始,請使用 git cz 來替代 git commit 提交信息,我們來看看,假設(shè)我們隨便寫一個 git commit -m 'fixbug' 會提示什么?

使用 git cz 來進行填寫 commit 的內(nèi)容。


git cz 的 type 說明:

雖然,我們現(xiàn)在已經(jīng)可以規(guī)范提交信息了,但是我們可能不喜歡默認的交互,例如,一個精簡的描述就可以了,不希望再提示我去寫詳細的描述,那么就可以使用 cz-customizable 來進行定制。

(2) 自定義提交說明

安裝 cz-customizable:

  1. npm install cz-customizable -D 

cz-customizable 是可自定義的 Commitizen 插件,可幫助實現(xiàn)一致的 commit message。

cz-customizable 適合大型團隊去自定義 scope,和 commit type。

新建 .cz-config.js:

在項目根目錄下創(chuàng)建 .cz-config.js 文件:

官方提供了一份配置信息,可以去這個地址查看:

https://github.com/leoforfree/cz-customizable/blob/master/cz-config-EXAMPLE.js

  1. //.cz-config.js 
  2. module.exports = { 
  3.   types: [ 
  4.     { value: 'feat', name: 'feat:     A new feature' }, 
  5.     { value: 'fix', name: 'fix:      A bug fix' }, 
  6.     { value: 'docs', name: 'docs:     Documentation only changes' }, 
  7.     { 
  8.       value: 'style', 
  9.       name: 
  10.         'style:    Changes that do not affect the meaning of the code\n            (white-space, formatting, missing semi-colons, etc)', 
  11.     }, 
  12.     { 
  13.       value: 'refactor', 
  14.       name: 'refactor: A code change that neither fixes a bug nor adds a feature', 
  15.     }, 
  16.     { 
  17.       value: 'perf', 
  18.       name: 'perf:     A code change that improves performance', 
  19.     }, 
  20.     { value: 'test', name: 'test:     Adding missing tests' }, 
  21.     { 
  22.       value: 'chore', 
  23.       name: 
  24.         'chore:    Changes to the build process or auxiliary tools\n            and libraries such as documentation generation', 
  25.     }, 
  26.     { value: 'revert', name: 'revert:   Revert to a commit' }, 
  27.     { value: 'WIP', name: 'WIP:      Work in progress' }, 
  28.   ], 
  29.  
  30.   scopes: [{ name: 'accounts' }, { name: 'admin' }, { name: 'exampleScope' }, { name: 'changeMe' }], 
  31.  
  32.   allowTicketNumber: false, 
  33.   isTicketNumberRequired: false, 
  34.   ticketNumberPrefix: 'TICKET-', 
  35.   ticketNumberRegExp: '\\d{1,5}', 
  36.  
  37.   // it needs to match the value for field type. Eg.: 'fix' 
  38.   /* 
  39.   scopeOverrides: { 
  40.     fix: [ 
  41.       {name: 'merge'}, 
  42.       {name: 'style'}, 
  43.       {name: 'e2eTest'}, 
  44.       {name: 'unitTest'} 
  45.     ] 
  46.   }, 
  47.   */ 
  48.   // override the messages, defaults are as follows 
  49.   messages: { 
  50.     type: "Select the type of change that you're committing:", 
  51.     scope: '\nDenote the SCOPE of this change (optional):', 
  52.     // used if allowCustomScopes is true 
  53.     customScope: 'Denote the SCOPE of this change:', 
  54.     subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n', 
  55.     body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n', 
  56.     breaking: 'List any BREAKING CHANGES (optional):\n', 
  57.     footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n', 
  58.     confirmCommit: 'Are you sure you want to proceed with the commit above?', 
  59.   }, 
  60.  
  61.   allowCustomScopes: true, 
  62.   allowBreakingChanges: ['feat', 'fix'], 
  63.   // skip any questions you want 
  64.   skipQuestions: ['body'], 
  65.  
  66.   // limit subject length 
  67.   subjectLimit: 100, 
  68. }; 
  • types:描述修改的性質(zhì)是什么,是bugfix還是feat,在這里進行定義。
  • scopes:定義之后,我們就可以通過上下鍵去選擇 scope
  • scopeOverrides:針對每一個type去定義scope
  • allowBreakingChanges:如上設(shè)置為 ['feat', 'fix'],只有我們type選擇了 feat 或者是 fix,才會詢問我們 breaking message.
  • allowCustomScopes:設(shè)置為 true,在 scope 選擇的時候,會有 empty 和 custom 可以選擇,顧名思義,選擇 empty 表示 scope 缺省,如果選擇 custom,則可以自己輸入信息
  • skipQuestions:指定跳過哪些步驟,例如跳過我們剛剛說的詳細描述,設(shè)置其為 scope: ['body'],假設(shè)我們的項目也不會涉及到關(guān)聯(lián) issue,我們可以設(shè)置其為 scope: ['body', 'footer']
  • subjectLimit:描述的長度限制

這里我就不一一演示每個字段修改之后的情況了,根據(jù)字段的說明,建議如果想自定義提交規(guī)則,在本地進行修改驗證,公司內(nèi)部的代碼庫不需要管理 issue,另外,我不喜歡寫長描述,所以我把 body 和 footer 給 skip 掉了。

cz-customizable 會首先在項目根目錄下尋找: .cz-config.js 或 .config/cz-config.js,如果找不到,會去主目錄尋找。我們也可以在 package.json 中手動去指定配置文件的路徑。

  1. "config": { 
  2.     "commitizen": {  
  3.         "path": "node_modules/cz-customizable" 
  4.     }, 
  5.     "cz-customizable": { 
  6.         "config": "config/path/to/my/config.js" 
  7.     } 

現(xiàn)在,我們已經(jīng)規(guī)范了 commit 信息,但是沒有對提交的代碼進行規(guī)范,在一個代碼庫中,經(jīng)常出現(xiàn)2個空格/4個空格混用,有些地方寫 ;,有些不寫 ;,風(fēng)格不統(tǒng)一。例如,我們希望提交到git庫的代碼,都能夠通過 eslint 檢查或者是通過測試。我們可以借助于 pre-commit 這個鉤子來做這些事情。

2. 代碼提交前檢查

安裝依賴:

  1. npm install lint-staged -D 

使用 pre-commit 的 hook

  1. "husky": { 
  2.     "hooks": { 
  3.         "pre-commit": "lint-staged" 
  4.     } 
  5. }, 
  6. "lint-staged": { 
  7.     "**/*.js": [ 
  8.         "prettier --write",  
  9.         "eslint" 
  10.     ] 

這樣配置之后,每次提交的時候,都會對要提交的文件(并不是對整個項目)進行 prettier 格式化和 eslint 檢查,都通過之后,才能 commit 成功。

(1) eslint 和 prettier 配置

我的項目是 react 項目,下面是我進行的配置。

安裝 eslint 和 prettier 相關(guān)依賴:

  1. npm install eslint eslint-config-prettier eslint-plugin-promise eslint-plugin-react eslint-plugin-react-hooks prettier babel-eslint -D 

新建 .prettierrc.js

當然啦,你也可以在 package.json 的 prettier 字段中配置,這里我配置成了獨立的文件,以便后期維護。

  1. module.exports = { 
  2.   printWidth: 100, //長度超過100斷行 
  3.   singleQuote: true,//使用單引號 
  4. }; 

如果你有一些文件不需要 prettier 進行格式化,那么可以新建一個 .prettierignore 文件,如下:

  1. dist 
  2. node_modules 
  3. public 

新建 .eslintrc.js 文件

以下是我的配置:

  1. module.exports = { 
  2.   settings: { 
  3.     react: { 
  4.       pragma: 'React', 
  5.       version: 'detect' 
  6.     } 
  7.   }, 
  8.   // babel parser to support ES6/7 features 
  9.   parser: 'babel-eslint', 
  10.   parserOptions: { 
  11.     ecmaVersion: 7, 
  12.     ecmaFeatures: { 
  13.       experimentalObjectRestSpread: true, 
  14.       jsx: true 
  15.     }, 
  16.     sourceType: 'module' 
  17.   }, 
  18.   extends: [ 
  19.     'prettier',  
  20.     'prettier/react' 
  21.   ], 
  22.   plugins: [ 
  23.     'promise',  
  24.     'react',  
  25.     'react-hooks' 
  26.   ], 
  27.   env: { 
  28.     browser: true, 
  29.     es6: true, 
  30.     node: true 
  31.   }, 
  32.   rules: { 
  33.     'no-compare-neg-zero': 2, //禁止與 -0 進行比較 
  34.     'no-cond-assign': 2, //禁止條件表達式中出現(xiàn)賦值操作符 
  35.     'no-console': 1, //禁用 console 
  36.     'no-constant-condition': 1, //禁止在條件中使用常量表達式 
  37.     'no-control-regex': 1, //禁止在正則表達式中使用控制字符 
  38.     'no-debugger': 2, //禁用 debugger 
  39.     'no-dupe-args': 2, //禁止 function 定義中出現(xiàn)重名參數(shù) 
  40.     'no-dupe-keys': 2, //禁止對象字面量中出現(xiàn)重復(fù)的 key 
  41.     'no-duplicate-case': 2, //禁止出現(xiàn)重復(fù)的 case 標簽 
  42.     'no-const-assign': 1, //禁止修改const聲明的變量 
  43.     'no-empty': 1, //禁止出現(xiàn)空語句塊 
  44.     'no-empty-character-class': 2, //禁止在正則表達式中使用空字符集 
  45.     'no-ex-assign': 2, //禁止對 catch 子句的異常參數(shù)重新賦值 
  46.     'no-extra-boolean-cast': 1, //禁止不必要的布爾轉(zhuǎn)換 
  47.     'no-extra-semi': 1, //禁止不必要的分號 
  48.     'no-func-assign': 2, //禁止對 function 聲明重新賦值 
  49.     'no-inner-declarations': 0, //禁止在嵌套的塊中出現(xiàn)變量聲明或 function 聲明,ES6中無需禁止 
  50.     'no-invalid-regexp': 2, //禁止 RegExp 構(gòu)造函數(shù)中存在無效的正則表達式字符串 
  51.     'no-irregular-whitespace': 1, //禁止在字符串和注釋之外不規(guī)則的空白 
  52.     'no-obj-calls': 2, //禁止把全局對象作為函數(shù)調(diào)用,比如Math() JSON() 
  53.     'no-regex-spaces': 1, //禁止正則表達式字面量中出現(xiàn)多個空格 
  54.     'no-sparse-arrays': 1, //禁用稀疏數(shù)組 
  55.     'no-unexpected-multiline': 1, //禁止出現(xiàn)令人困惑的多行表達式 
  56.     'no-unreachable': 1, //禁止在return、throw、continue 和 break 語句之后出現(xiàn)不可達代碼 
  57.     'no-unsafe-finally': 2, //禁止在 finally 語句塊中出現(xiàn)控制流語句 
  58.     'no-unsafe-negation': 1, //禁止對關(guān)系運算符的左操作數(shù)使用否定操作符 
  59.     'use-isnan': 2, //要求使用 isNaN() 檢查 NaN,如 isNaN(foo),而非foo == NaN 
  60.     'valid-typeof': 2, //強制 typeof 表達式與有效的字符串(如: 'undefined', 'object', 'boolean', 'number', 'string', 'function','symbol')進行比較 
  61.     'no-case-declarations': 1, //不允許在 case 子句中使用詞法聲明 
  62.     'no-empty-pattern': 2, //禁止使用空解構(gòu)模式 
  63.     'no-fallthrough': 2, //禁止 case 語句落空 
  64.     'no-global-assign': 2, //禁止對原生對象或只讀的全局對象進行賦值 
  65.     'no-octal': 1, //禁用八進制字面量 
  66.     'no-redeclare': 1, //禁止多次聲明同一變量 
  67.     'no-self-assign': 1, //禁止自我賦值 
  68.     'no-unused-labels': 1, //禁用出現(xiàn)未使用過的標 
  69.     'no-useless-escape': 1, //禁用不必要的轉(zhuǎn)義字符 
  70.     'no-delete-var': 2, //禁止刪除變量 
  71.     'no-undef': 2, //禁用使用未聲明的變量,除非它們在 /*global */ 注釋中被提到 
  72.     'no-unused-vars': 1, //禁止出現(xiàn)未使用過的變量 
  73.     'constructor-super': 2, //要求在構(gòu)造函數(shù)中有 super() 的調(diào)用 
  74.     'no-class-assign': 2, //禁止給類賦值 
  75.     'no-dupe-class-members': 2, //禁止類成員中出現(xiàn)重復(fù)的名稱 
  76.     'no-new-symbol': 2, //禁止 Symbol 和 new 操作符一起使用 
  77.     'no-this-before-super': 2, //禁止在構(gòu)造函數(shù)中,在調(diào)用 super() 之前使用 this 或 super 
  78.     'require-yield': 2, //要求 generator 函數(shù)內(nèi)有 yield 
  79.     'no-mixed-spaces-and-tabs': 1, //要求不適用space,tab混用 
  80.     'react/forbid-prop-types': [1, { forbid: ['any'] }], //禁止某些propTypes 
  81.     'react/prop-types': 1, //沒用對props類型進行校驗 
  82.     'react/jsx-closing-bracket-location': 1, //在JSX中驗證右括號位置 
  83.     'react/jsx-curly-spacing': [1, { when: 'never', children: true }], //在JSX屬性和表達式中加強或禁止大括號內(nèi)的空格。 
  84.     'react/jsx-key': 2, //在數(shù)組或迭代器中驗證JSX具有key屬性 
  85.     'react/jsx-max-props-per-line': [1, { maximum: 1 }], // 限制JSX中單行上的props的最大數(shù)量 
  86.     'react/jsx-no-duplicate-props': 2, //防止在JSX中重復(fù)的props 
  87.     'react/jsx-no-undef': 1, //在JSX中禁止未聲明的變量 
  88.     'react/no-string-refs': 1, //Using string literals in ref attributes is deprecated 
  89.     'react/jsx-uses-react': 1, //防止反應(yīng)被錯誤地標記為未使用 
  90.     'react/jsx-uses-vars': 1, //防止在JSX中使用的變量被錯誤地標記為未使用 
  91.     'react/no-danger': 1, //防止使用危險的JSX屬性 
  92.     'react/no-did-update-set-state': 2, //防止在componentDidUpdate中使用setState 
  93.     'react/no-did-mount-set-state': 0, //防止在componentDidUpdate中使用setState 
  94.     'react/no-direct-mutation-state': 2, //防止this.state賦值 
  95.     'react/no-unknown-property': 2, //防止使用未知的DOM屬性 
  96.     'react/prefer-es6-class': 1, //為React組件強制執(zhí)行ES5或ES6類 
  97.     'react/react-in-jsx-scope': 0, //使用JSX時,必須要引入React 
  98.     'react/sort-comp': 0, //強制組件方法順序 
  99.     'react/sort-prop-types': 0, //強制組件屬性順序 
  100.     'react/jsx-sort-props': 1, 
  101.     'react/no-deprecated': 1, //不使用棄用的方法 
  102.     'react/jsx-equals-spacing': 1, //在JSX屬性中強制或禁止等號周圍的空格 
  103.     'react/wrap-multilines': 0, 
  104.     'comma-dangle': 1, //對象字面量項尾不能有逗號 
  105.     'react/no-multi-comp': 0, //防止每個文件有多個組件定義 
  106.     'flowtype/generic-spacing': 0, //泛型對象的尖括號中類型前后的空格規(guī)范 
  107.     'flowtype/space-after-type-colon': 0, //類型注解分號后的空格規(guī)范 
  108.     // react-hooks 
  109.     'react-hooks/rules-of-hooks': 'error', 
  110.     'react-hooks/exhaustive-deps': 'warn' 
  111.   } 
  112. }; 

現(xiàn)在,再也不能隨心所欲往你的代碼庫提交文件啦,不過 eslint 和 prettier 的規(guī)則要和團隊的成員協(xié)商制定哈~

 

責(zé)任編輯:趙寧寧 來源: 前端宇宙
相關(guān)推薦

2023-07-15 18:26:51

LinuxABI

2020-12-17 06:48:21

SQLkafkaMySQL

2019-04-01 14:59:56

負載均衡服務(wù)器網(wǎng)絡(luò)

2023-11-09 14:44:27

Docker鏡像容器

2022-03-21 08:05:38

HTTP/1.1QUIC協(xié)議

2019-01-28 18:32:37

數(shù)據(jù)分析Python

2021-09-07 09:40:20

Spark大數(shù)據(jù)引擎

2022-06-16 07:31:41

Web組件封裝HTML 標簽

2024-06-19 09:58:29

2023-04-12 11:18:51

甘特圖前端

2015-09-06 09:22:24

框架搭建快速高效app

2012-07-10 01:22:32

PythonPython教程

2023-11-30 10:21:48

虛擬列表虛擬列表工具庫

2024-05-13 09:28:43

Flink SQL大數(shù)據(jù)

2016-06-13 14:07:50

Java動態(tài)代理

2022-03-04 16:06:33

數(shù)據(jù)庫HarmonyOS鴻蒙

2009-10-09 14:45:29

VB程序

2019-09-16 09:14:51

2024-11-07 16:09:53

2022-08-26 09:01:07

CSSFlex 布局
點贊
收藏

51CTO技術(shù)棧公眾號