1. npm install -D eslint
  2. npx eslint --init
  3. #提交规范:commitlint commitizen
  4. npm install -D @commitlint/cli @commitlint/config-conventional commitizen cz-customizable
  5. npm i -g commitizen
  6. git init
  7. npx mrm@2 lint-staged
  8. npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'

eslint init

image.png

  1. module.exports = {
  2. env: {
  3. browser: true,
  4. es2021: true,
  5. node: true
  6. },
  7. extends: [
  8. 'plugin:react/recommended',
  9. 'standard'
  10. ],
  11. parserOptions: {
  12. ecmaFeatures: {
  13. jsx: true
  14. },
  15. ecmaVersion: 12,
  16. sourceType: 'module'
  17. },
  18. plugins: [
  19. 'react'
  20. ],
  21. settings: {
  22. react: {
  23. version: 'detect'
  24. }
  25. },
  26. rules: {
  27. 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
  28. 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
  29. 'react/react-in-jsx-scope': 'off',
  30. 'react/prop-types': 'off',
  31. 'react/jsx-curly-spacing': [2, 'never'],
  32. indent: ['error', 2, { SwitchCase: 1 }], // 使用两个空格作缩进
  33. eqeqeq: ['error', 'smart'], // 比较的时候使用严格等于
  34. 'keyword-spacing': 2 // 关键字前后的空格
  35. }
  36. }

package.json

  1. {
  2. ...
  3. "scripts":{
  4. ...,
  5. "lint:js": "eslint --ext .js,.jsx src",
  6. "lint:css": "stylelint --ext .css,.scss src",
  7. "commit": "cz"
  8. }
  9. "lint-staged": {
  10. "*.{js,jsx}": "npm run lint:js",
  11. "*.{css,scss}": "npm run lint:css"
  12. },
  13. "config": {
  14. "commitizen": {
  15. "path": "node_modules/cz-customizable"
  16. },
  17. "cz-customizable": {
  18. "config": ".commitlintrc.js"
  19. }
  20. }
  21. }

.commitlintrc.js

  1. module.exports = {
  2. extends: ['@commitlint/config-conventional'],
  3. rules: {
  4. 'type-empty': [2, 'never'],
  5. 'type-case': [0, 'always', 'lower-case'],
  6. 'subject-empty': [2, 'never'],
  7. },
  8. messages: {
  9. type: '请选择提交的类型',
  10. subject: '请简要描述提交(必填)',
  11. confirmCommit: '确认要使用以上信息提交?(Y/n)',
  12. },
  13. types: [
  14. { value: 'feat', name: 'feat:新增功能' },
  15. { value: 'fix', name: 'fix:修复缺陷' },
  16. { value: 'docs', name: 'docs:文档更新' },
  17. { value: 'style', name: 'style:代码格式' },
  18. { value: 'refactor', name: 'refactor: 代码重构' },
  19. { value: 'perf', name: 'perf:性能提升' },
  20. { value: 'test', name: 'test:测试相关' },
  21. { value: 'build', name: 'build:构建相关' },
  22. { value: 'ci', name: 'ci:持续集成' },
  23. { value: 'revert', name: 'revert:回退代码 ' },
  24. { value: 'chore', name: 'chore:其他修改' },
  25. ],
  26. skipQuestions: ['scope', 'customScope', 'body', 'footer', 'footerPrefix'],
  27. }

CSS 代码校验

  1. npm i -D stylelint autoprefixer postcss postcss-preset-env
  1. module.exports = {
  2. plugins: [
  3. require('autoprefixer'),
  4. require('postcss-preset-env')
  5. ]
  6. }
  1. // .eslint.js
  2. module.exports = {
  3. rules: {
  4. "comma-dangle": ["error", "never"], //是否允许对象中出现结尾逗号
  5. "no-cond-assign": 2, //条件语句的条件中不允许出现赋值运算符
  6. "no-console": 2, //不允许出现console语句
  7. "no-constant-condition": 2, //条件语句的条件中不允许出现恒定不变的量
  8. "no-control-regex": 2, //正则表达式中不允许出现控制字符
  9. "no-debugger": 2, //不允许出现debugger语句
  10. "no-dupe-args": 2, //函数定义的时候不允许出现重复的参数
  11. "no-dupe-keys": 2, //对象中不允许出现重复的键
  12. "no-duplicate-case": 2, //switch语句中不允许出现重复的case标签
  13. "no-empty": 2, //不允许出现空的代码块
  14. "no-empty-character-class": 2, //正则表达式中不允许出现空的字符组
  15. "no-ex-assign": 2, //在try catch语句中不允许重新分配异常变量
  16. "no-extra-boolean-cast": 2, //不允许出现不必要的布尔值转换
  17. "no-extra-parens": 0, //不允许出现不必要的圆括号
  18. "no-extra-semi": 2, //不允许出现不必要的分号
  19. "no-func-assign": 2, //不允许重新分配函数声明
  20. "no-inner-declarations": ["error", "functions"], //不允许在嵌套代码块里声明函数
  21. "no-invalid-regexp": 2, //不允许在RegExp构造函数里出现无效的正则表达式
  22. "no-irregular-whitespace": 2, //不允许出现不规则的空格
  23. "no-negated-in-lhs": 2, //不允许在in表达式语句中对最左边的运算数使用取反操作
  24. "no-obj-calls": 2, //不允许把全局对象属性当做函数来调用
  25. "no-regex-spaces": 2, //正则表达式中不允许出现多个连续空格
  26. "quote-props": 2, //对象中的属性名是否需要用引号引起来
  27. "no-sparse-arrays": 2, //数组中不允许出现空位置
  28. "no-unreachable": 2, //在return,throw,continue,break语句后不允许出现不可能到达的语句
  29. "use-isnan": 2, //要求检查NaN的时候使用isNaN()
  30. "valid-jsdoc": ["error", {
  31. "requireReturn": false,
  32. "requireParamDescription": false,
  33. "requireReturnDescription": true
  34. }], //强制JSDoc注释
  35. "valid-typeof": ["error", {
  36. "requireStringLiterals": true
  37. }], //在使用typeof表达式比较的时候强制使用有效的字符串
  38. "block-scoped-var": 2, //将变量声明放在合适的代码块里
  39. "complexity": 0, //限制条件语句的复杂度
  40. "consistent-return": 2, //无论有没有返回值都强制要求return语句返回一个值
  41. "curly": ["error", "all"], //强制使用花括号的风格
  42. "default-case": 0, //在switch语句中需要有default语句
  43. "dot-notation": ["error", {"allowKeywords": false, "allowPattern": ""}], //获取对象属性的时候使用点号
  44. "eqeqeq": ["error", "smart"], //比较的时候使用严格等于
  45. "no-alert": 1, //不允许使用alert,confirm,prompt语句
  46. "no-caller": 2, //不允许使用arguments.callee和arguments.caller属性
  47. "guard-for-in": 0, //监视for in循环,防止出现不可预料的情况
  48. "no-div-regex": 2, //不能使用看起来像除法的正则表达式
  49. "no-else-return": 0, //如果if语句有return,else里的return不用放在else里
  50. "no-labels": ["error", {
  51. "allowLoop": false,
  52. "allowSwitch": false
  53. }], //不允许标签语句
  54. "no-eq-null": 2, //不允许对null用==或者!=
  55. "no-eval": 2, //不允许使用eval()
  56. "no-extend-native": 2, //不允许扩展原生对象
  57. "no-extra-bind": 2, //不允许不必要的函数绑定
  58. "no-fallthrough": 2, //不允许switch按顺序全部执行所有case
  59. "no-floating-decimal": 2, //不允许浮点数缺失数字
  60. "no-implied-eval": 2, //不允许使用隐式eval()
  61. "no-iterator": 2, //不允许使用__iterator__属性
  62. "no-lone-blocks": 2, //不允许不必要的嵌套代码块
  63. "no-loop-func": 2, //不允许在循环语句中进行函数声明
  64. "no-multi-spaces": 2, //不允许出现多余的空格
  65. "no-multi-str": 2, //不允许用\来让字符串换行
  66. "no-global-assign": 2, //不允许重新分配原生对象
  67. "no-new": 2, //不允许new一个实例后不赋值或者不比较
  68. "no-new-func": 2, //不允许使用new Function
  69. "no-new-wrappers": 2, //不允许使用new String,Number和Boolean对象
  70. "no-octal": 2, //不允许使用八进制字面值
  71. "no-octal-escape": 2, //不允许使用八进制转义序列
  72. "no-param-reassign": 0, //不允许重新分配函数参数"no-proto": 2, //不允许使用__proto__属性
  73. "no-redeclare": 2, //不允许变量重复声明
  74. "no-return-assign": 2, //不允许在return语句中使用分配语句
  75. "no-script-url": 2, //不允许使用javascript:void(0)
  76. "no-self-compare": 2, //不允许自己和自己比较
  77. "no-sequences": 2, //不允许使用逗号表达式
  78. "no-throw-literal": 2, //不允许抛出字面量错误 throw "error"
  79. "no-unused-expressions": 2, //不允许无用的表达式
  80. "no-void": 2, //不允许void操作符
  81. "no-warning-comments": [1, {"terms": ["todo", "fixme", "any other term"]}], //不允许警告备注
  82. "no-with": 2, //不允许使用with语句
  83. "radix": 1, //使用parseInt时强制使用基数来指定是十进制还是其他进制
  84. "vars-on-top": 0, //var必须放在作用域顶部
  85. "wrap-iife": [2, "any"], //立即执行表达式的括号风格
  86. "yoda": [2, "never", {"exceptRange": true}], //不允许在if条件中使用yoda条件
  87. "strict": [2, "function"], //使用严格模式
  88. "no-catch-shadow": 2, //不允许try catch语句接受的err变量与外部变量重名"no-delete-var": 2, //不允许使用delete操作符
  89. "no-label-var": 2, //不允许标签和变量同名
  90. "no-shadow": 2, //外部作用域中的变量不能与它所包含的作用域中的变量或参数同名
  91. "no-shadow-restricted-names": 2, //js关键字和保留字不能作为函数名或者变量名
  92. "no-undef": 2, //不允许未声明的变量
  93. "no-undef-init": 2, //不允许初始化变量时给变量赋值undefined
  94. "no-undefined": 2, //不允许把undefined当做标识符使用
  95. "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], //不允许有声明后未使用的变量或者参数
  96. "no-use-before-define": [2, "nofunc"], //不允许在未定义之前就使用变量"indent": 2, //强制一致的缩进风格
  97. "brace-style": [2, "1tbs", { "allowSingleLine": false}], //大括号风格
  98. "camelcase": [2, {"properties": "never"}], //强制驼峰命名规则
  99. "comma-style": [2, "last"], //逗号风格
  100. "consistent-this": [0, "self"], //当获取当前环境的this是用一样的风格
  101. "eol-last": 2, //文件以换行符结束
  102. "func-names": 0, //函数表达式必须有名字
  103. "func-style": 0, //函数风格,规定只能使用函数声明或者函数表达式
  104. "key-spacing": [2, {"beforeColon": false, "afterColon": true}], //对象字面量中冒号的前后空格
  105. "max-nested-callbacks": 0, //回调嵌套深度
  106. "new-cap": [2, {"newIsCap": true, "capIsNew": false}], //构造函数名字首字母要大写
  107. "new-parens": 2, //new时构造函数必须有小括号
  108. "newline-after-var": 0, //变量声明后必须空一行
  109. "no-array-constructor": 2, //不允许使用数组构造器
  110. "no-inline-comments": 0, //不允许行内注释
  111. "no-lonely-if": 0, //不允许else语句内只有if语句
  112. "no-mixed-spaces-and-tabs": [2, "smart-tabs"], //不允许混用tab和空格
  113. "no-multiple-empty-lines": [2, {"max": 2}], //空行最多不能超过两行
  114. "no-nested-ternary": 2, //不允许使用嵌套的三目运算符
  115. "no-new-object": 2, //禁止使用new Object()
  116. "fun-call-spacing": 2, //函数调用时,函数名与()之间不能有空格
  117. "no-ternary": 0, //不允许使用三目运算符
  118. "no-trailing-spaces": 2, //一行最后不允许有空格
  119. "no-underscore-dangle": 2, //不允许标识符以下划线开头
  120. "no-extra-parens": 0, //不允许出现多余的括号
  121. "one-var": 0, //强制变量声明放在一起
  122. "operator-assignment": 0, //赋值运算符的风格
  123. "padded-blocks": [2, "never"], //块内行首行尾是否空行
  124. "quote-props": 0, //对象字面量中属性名加引号
  125. "quotes": [1, "single", "avoid-escape"], //引号风格
  126. "semi": [2, "always"], //强制语句分号结尾
  127. "semi-spacing": [2, {"before": false, "after": true}], //分后前后空格
  128. "sort-vars": 0, //变量声明时排序
  129. "space-before-blocks": [2, "always"], //块前的空格
  130. "space-before-function-paren": [2, {"anonymous": "always", "named": "never"}], //函数定义时括号前的空格
  131. "space-infix-ops": [2, {"int32Hint": true}], //操作符周围的空格
  132. "keyword-spacing": 2, //关键字前后的空格
  133. "space-unary-ops": [2, { "words": true, "nonwords": false}], //一元运算符前后不要加空格
  134. "wrap-regex": 2, //正则表达式字面量用括号括起来
  135. "no-var": 0, //使用let和const代替var
  136. "generator-star-spacing": [2, "both"], //生成器函数前后空格
  137. "max-depth": 0, //嵌套块深度
  138. "max-len": 0, //一行最大长度,单位为字符
  139. "max-params": 0, //函数最多能有多少个参数
  140. "max-statements": 0, //函数内最多有几个声明
  141. "no-bitwise": 0, //不允许使用位运算符
  142. "no-plusplus": 0 //不允许使用++ --运算符
  143. }
  144. };