git commit 钩子 pre-commit做一些代码提交检查工作

团队代码风格不一致怎么解决?eslint? 如果某个人关闭了eslint绕过提交呢,所以需要一些硬手段来强制每个人提交的东西都符合规范,下面以vue项目为例子举例。

package.json配置

"gitHooks": {
    "pre-commit": "lint-staged"
  },
  "lint-staged": {
    "*.js": [
      "vue-cli-service lint",
      "git add"
    ],
    "*.vue": [
      "vue-cli-service lint",
      "git add"
    ],
    "*.s+(a|c)ss": [
      "sass-lint -v -q"
    ]
  },

项目根目录配置sass-lint.yml 具体配置很多,可以根据团队喜好自定义,点这里

options:
  formatter: stylish
  merge-default-rules: false
files:
  include: 'src/**/*.s+(a|c)ss'
rules:
  # Extends
  extends-before-declarations: 2
  extends-before-mixins: 2
  placeholder-in-extend: 2

  # Mixins
  mixins-before-declarations: 2

  # Line Spacing
  empty-line-between-blocks:
    - 2
    -
      allow-single-line-rulesets: true
  one-declaration-per-line: 2
  single-line-per-selector: 0

  # Disallows
  no-attribute-selectors: 0
  no-color-hex: 0
  no-color-keywords: 2
  no-color-literals: 0
  no-combinators: 0
  no-css-comments: 0
  no-debug: 2
  no-disallowed-properties: 0
  no-duplicate-properties: 2
  no-empty-rulesets: 2
  no-extends: 0
  no-ids: 2
  no-important: 0
  no-invalid-hex: 2
  no-mergeable-selectors: 2
  no-misspelled-properties:
    - 2
    -
      extra-properties:
        - 'touch-callout'
        - 'overflow-scrolling'
  no-qualifying-elements: 2 # 待定
  no-trailing-whitespace: 2
  no-trailing-zero: 2
  no-transition-all: 1
  no-universal-selectors: 0
  no-url-protocols: 2
  no-vendor-prefixes: 0
  no-warn: 2
  property-units: 0

  # Nesting
  force-attribute-nesting: 0
  force-element-nesting: 2
  force-pseudo-nesting: 0

  # Name Formats
  class-name-format:
    - 2
    -
      allow-leading-underscore: false
      convention: hyphenatedlowercase
  function-name-format:
    - 2
    -
      allow-leading-underscore: false
      convention: hyphenatedlowercase
  id-name-format: 0
  mixin-name-format:
    - 2
    -
      allow-leading-underscore: false
      convention: hyphenatedlowercase
  placeholder-name-format:
    - 2
    -
      allow-leading-underscore: false
      convention: hyphenatedlowercase
  variable-name-format:
    - 2
    -
      allow-leading-underscore: false
      convention: hyphenatedlowercase

  # Style Guide
  attribute-quotes:
    - 2
    - style: double
  bem-depth: 0
  border-zero: 2
  brace-style:
    - 2
    -
      allow-single-line: true
  clean-import-paths: 2
  empty-args: 2
  hex-length: 2
  hex-notation: 2
  indentation:
    - 2
    -
      size: 2
  leading-zero: 2
  nesting-depth:
    - 2
    -
      max-depth: 3
  # https://github.com/sasstools/sass-lint/blob/develop/lib/config/property-sort-orders/recess.yml
  property-sort-order:
    - 2
    -
      order: recess
  pseudo-element: 0
  quotes:
    - 2
    - style: double
  shorthand-values: 2
  url-quotes:
    - 2
    - style: double
  variable-for-property: 2
  zero-unit: 2

  # Inner Spacing
  space-after-bang: 2
  space-after-colon: 2
  space-after-comma: 2
  space-around-operator: 2
  space-before-bang: 2
  space-before-brace: 2
  space-before-colon: 2
  space-between-parens: 2

  # Final Items
  final-newline: 2
  trailing-semicolon: 2

如此,当你在提交代码时,执行命令 git commit -m '提交信息' 时;就会执行gitHooks的pre-commit,从而对本次提交的修改的内容, *.js * .vue *.sass *.scss执行检查,看是否符合规范,不符合就会在控制台报错并且终止本次提交。


image.png

优点:这对一个多人协作的项目统一编码规范是有很大帮助的。
缺点:每次提交都来检查,当你一次提交很多代码时,会有大量报错让你修改,烦!

改进点:可以实时报错而不是commit时再报错,看团队、个人喜好吧。不过那样子也有人嫌烦。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,547评论 4 374
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,787评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,175评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,752评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,169评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,056评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,195评论 2 321
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,980评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,724评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,872评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,344评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,667评论 3 264
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,379评论 3 245
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,202评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,992评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,189评论 2 286
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,987评论 2 279

推荐阅读更多精彩内容