• 项目架构:eslint 代码检测、提交代码审查


    前言:

    eslint 有什么用,我就不说了,看本文的肯定都已经了解了。

    本文分为两部分内容;

    第一部分讲述开发过程中对代码的语法检测,大部分人呢第一部分就能满足规范需求了;

    第二部分讲述代码提交自动检测,不合格且无法自动修复就自动禁止提交!

    1、npm install eslint --save-dev

    2、npx eslint --init

    3、经过上面两个步骤,像我个人此时安装了以下6个开发依赖库了(根据个人选择)

    eslint

    typescript

    eslint-plugin-vue

    @typescript-eslint/eslint-plugin

    @typescript-eslint/parser

    vue-eslint-parser

    4、配置 .eslintrc.js  .eslintignore

    可能有的人文件不是 .eslintrc.js 而是  .eslintrc.cjs 或其他之类的,根据自身情况处理,没啥太大区别。.eslintrc.js 文件内的 eslint 相关检测规则放文章末尾,避免影响接下来的观看。

    至此第一部分内容已经结束,想要了解第二部分内容的接着往下看即可。


    代码提交自动检测是否正确

    以下内容依赖 husky 、lint-staged 这两个npm库。

    这两部分内容我就不写了,请查看下面这文章的“方案3” 内容,结合下面的2个步骤进行使用即可!

    项目架构:prettier 提交检测、全局||自动格式化代码_oumae-kumiko的博客-CSDN博客

    1、package.json 的 script 下增加以下内容

    1. "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
    2. "lint-fix": "eslint src/**/*.{js,jsx,vue,ts,tsx} --fix",

    2、package.json 根属性增加以下内容

    1. "lint-staged": {
    2. "src/**/*.{js,jsx,vue,ts,tsx}": [
    3. ......
    4. "eslint --fix"
    5. ]
    6. }

    eslint 配置规则

    1. module.exports = {
    2. root: true, // 若有多个配置规则,向外查找到有 root:true 的时候就终止查找,有多个配置则配置会叠加。
    3. env: {
    4. // 声明检测哪些环境,比如window不能在node里用,否则会报错等
    5. browser: true,
    6. // "es6": true,
    7. es2021: true,
    8. node: true,
    9. "vue/setup-compiler-macros": true, // vue setup : eslint 开启 "eslint:recommended" 后需要设置为true
    10. },
    11. extends: [
    12. // 表示使用哪些默认的语法检测配置;数组表示后面的每个配置继承对应前面的配置
    13. "eslint:recommended",
    14. /**<> 额外插件扩展命名规则: plugin:<包名:省略了前缀,比如vue>/<配置名称:比如recommended> */
    15. "plugin:vue/vue3-essential", // 防止错误或意外的行为
    16. // "plugin:vue/vue3-strongly-recommended", // 代码风格配置
    17. // "plugin:vue/vue3-recommended", // 强制执行主观社区默认值的规则,以确保一致性;这个影响的特别多,要用需要重点针对配置rules。经过测试,该项基本包含了上面2项
    18. // "plugin:vue/base", // 好像没啥用。。
    19. "plugin:@typescript-eslint/recommended",
    20. // '@vue/eslint-config-prettier' // 没啥用
    21. ],
    22. globals: {
    23. // true为可读可重写,false只可读,"readonly"也是只可读
    24. coustomVar: true, // 意思是全局变量里有 coustomVar ,在X文件中不声明也可以使用 coustomVar
    25. // defineProps: "readonly",
    26. // defineEmits: "readonly",
    27. // defineExpose: "readonly",
    28. // withDefaults: "readonly"
    29. },
    30. parser: "vue-eslint-parser",
    31. parserOptions: {
    32. // 解析的语法环境有哪些
    33. ecmaVersion: "latest",
    34. parser: "@typescript-eslint/parser",
    35. sourceType: "module",
    36. // 允许解析JSX
    37. ecmaFeatures: {
    38. jsx: true,
    39. },
    40. },
    41. plugins: ["vue", "@typescript-eslint"],
    42. /**
    43. * off 关闭检验
    44. * warn 代码问题警告
    45. * error 代码错误警告
    46. */
    47. rules: {
    48. /** @js */
    49. quotes: ["off", "double"], // single 单引号; double 双引号; backtick 反斜杠
    50. "prefer-const": "off", // 未修改变量 > 报错提示用 consot
    51. "no-console": "off", // 必关,console代码什么的,打包的时候配置删掉就行了,源代码肯定要留着,但要控制整洁,别没啥用的都留着
    52. "no-debugger": "error", // 必开,打了断点的,注意关了!
    53. "no-redeclare": "warn", // 避免引入外部文件导致提交代码失败
    54. "no-self-assign": "warn", // 避免引入外部文件导致提交代码失败
    55. /** @vue */
    56. // "vue/require-v-for-key": "error",
    57. "vue/multi-word-component-names": "off", // 关闭 kebab cased 风格警告!这风格一点也不好!!名字统一不变才是王道。
    58. /** @typescript */
    59. "@typescript-eslint/ban-types": "warn",
    60. "@typescript-eslint/no-inferrable-types": "off", // 开启后:可轻松推断的TS显示类型不需要主动声明
    61. "@typescript-eslint/no-explicit-any": "off", // 开启后:不允许使用 any 类型
    62. "@typescript-eslint/no-unused-vars": "off", // 开启后:声明了未使用,则提示报错 // 建议warn ,自己练习的项目,所以先关了
    63. "@typescript-eslint/no-empty-function": "error", // 禁止空函数。(函数内没任何内容)
    64. },
    65. };

  • 相关阅读:
    有趣的手机软件分享,感兴趣的朋友来瞧瞧
    远程服务器配置 Anaconda 并安装 PyTorch 详细教程
    【Spring】SpringBoot整合ShardingSphere并实现多线程分批插入10000条数据(进行分库分表操作)。
    ptmalloc源码分析 - _int_malloc函数实现smallbins/unsorted bin(07)
    【Java】微服务——Nacos配置管理(统一配置管理&热更新&配置共享&Nacos集群搭建)
    租用国外服务器要怎么选择?
    基于神经网络的图像识别,人工神经元网络的特点
    大疆笔试题——FPGA开发工程师
    D - Square Permutation-AtCoder Beginner Contest 324
    Cadence OrCAD Capture 绘制总线的方法
  • 原文地址:https://blog.csdn.net/lijiahui_/article/details/128135537