百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Vue项目接入Eslint与+Prettier+Husky自动格式化代码

zhezhongyun 2024-12-03 06:25 27 浏览

作者:FrontDream

转发链接:https://mp.weixin.qq.com/s/O9m40zo_j-waJGPGESC6WQ

简介

前端项目,主要有两种选择ESLint和TSLint。TSLint仅针对TS代码,因此如果采用TSLint规范TS代码,JS代码需要采用其他工具。而ESLint不仅能规范js代码,通过配置解析器,也能规范TS代码。此外由于性能问题,TypeScript 官方决定全面采用ESLint。

Eslint的主要功能包含代码格式的校验,代码质量的校验,JS规范,如用===而不是==判断相等、用驼峰命名变量而不是用下划线。而 Prettier 是美丽的意思,只是代码格式的校验(并格式化代码),不会对代码质量进行校验,如单行代码长度、tab 长度、空格、逗号表达式等问题。在实际项目中,eslint可以检测出代码问题,并标红,但是并不会自动格式化,需要手动格式化,接入Prettier并配置(需要分webstorm和vscode)可以进行自动化。但是需要考虑到的是,prettier和eslint的规则有可能冲突,因此需要考虑到当冲突时,需要解决冲突,以谁的标准为准,正常是以prettier为准。

起步

如果是新的项目,用vue官方的脚手架拉取项目时,在下面的步骤中选择Eslint+Prettier就好了,我们可以发现,官方也是集成了Eslint+Prettier的,因此还是比较靠谱的:

安装 Eslint 依赖

npm i -D eslint babel-eslint eslint-plugin-vue @vue/cli-plugin-eslint
  • eslint: ESLint的核心代码
  • babel-eslint: eslint 与 babel 整合包
  • eslint-plugin-vue @vue/cli-plugin-eslint:eslint 与 vue 整合包

当然如果已经安装过了,就可以不用安装了。

配置 Eslint

在项目的根目录下,新建.eslintrc.js文件,并将下面代码拷贝进去,具体规则看注释。或者看StandardJS 官方

// .eslintrc.js
module.exports = {
  root: true,
  parserOptions: {
    // 定义ESLint的解析器
    parser: "babel-eslint",
    sourceType: "module",
  },
  // 指定代码的运行环境
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  extends: [
    //继承 vue 的标准特性
    "plugin:vue/essential",
    "eslint:recommended",
  ],
  // 自定义eslint规则,严格按照StandardJS
  rules: {
    "vue/max-attributes-per-line": [
      2,
      {
        singleline: 10,
        multiline: {
          max: 1,
          allowFirstLine: false,
        },
      },
    ],
    "vue/singleline-html-element-content-newline": "off",
    "vue/multiline-html-element-content-newline": "off",
    "vue/name-property-casing": ["error", "PascalCase"],
    "vue/no-v-html": "off",
    // 两个空格缩进
    indent: [
      2,
      2,
      {
        SwitchCase: 1,
      },
    ],
    // 单引号
    quotes: [
      2,
      "single",
      {
        avoidEscape: true,
        allowTemplateLiterals: true,
      },
    ],
    // 未使用的变量
    "no-unused-vars": [
      2,
      {
        vars: "all",
        args: "after-used",
      },
    ],
    // 关键字前后空格
    "keyword-spacing": [
      2,
      {
        before: true,
        after: true,
      },
    ],
    // function关键字和函数名后面的空格
    "space-before-function-paren": [2, "never"],
    // 除了null,其他用===而不是==
    eqeqeq: ["error", "always", { null: "ignore" }],
    // 字符串拼接操作符直接用空格
    "space-infix-ops": 2,
    // 逗号前面不用空格,逗号后面用空格
    "comma-spacing": [
      2,
      {
        before: false,
        after: true,
      },
    ],
    // else必须和反花括号一行
    "brace-style": [
      2,
      "1tbs",
      {
        allowSingleLine: true,
      },
    ],
    // 多行 if 语句的的括号不能省
    curly: [2, "multi-line"],
    // 使用浏览器全局变量时加上 window. 前缀
    "no-undef": 2,
    // 不允许有连续多行空行
    "no-multiple-empty-lines": [
      2,
      {
        max: 1,
      },
    ],
    // 换行符在运算符的位置
    "operator-linebreak": [
      2,
      "after",
      {
        overrides: {
          "?": "before",
          ":": "before",
        },
      },
    ],
    // 条件语句中赋值语句
    "no-cond-assign": 2,
    // 单行代码块两边加空格
    "block-spacing": [2, "always"],
    // 对属性名强制使用驼峰
    camelcase: [
      0,
      {
        properties: "always",
      },
    ],
    // 不允许有多余的行末逗号
    "comma-dangle": [2, "never"],
    // 始终将逗号置于行末
    "comma-style": [2, "last"],
    // 点号操作符须与属性需在同一行
    "dot-location": [2, "property"],
    // 函数调用时标识符与括号间不留间隔
    "func-call-spacing": ["error", "never"],
    // 键值对当中冒号与值之间要留空白
    "key-spacing": [
      2,
      {
        beforeColon: false,
        afterColon: true,
      },
    ],
    // 构造函数要以大写字母开头, 但调用大写字母开头的函数不一定需要new
    "new-cap": [
      2,
      {
        newIsCap: true,
        capIsNew: false,
      },
    ],
    // 无参的构造函数调用时要带上括号
    "new-parens": 2,
    // 对象中定义了存值器,一定要对应的定义取值器
    "accessor-pairs": 2,
    // 子类的构造器中一定要调用 super
    "constructor-super": 2,
    // 使用数组字面量而不是构造器
    "no-array-constructor": "error",
    // 避免使用 arguments.callee 和 arguments.caller
    "no-caller": 2,
    // 避免对类名重新赋值
    "no-class-assign": 2,
    // 避免修改使用 const 声明的变量
    "no-const-assign": 2,
    // 正则中不要使用控制符
    "no-control-regex": "error",
    // 不要对变量使用 delete 操作。
    "no-delete-var": 2,
    // 不要定义冗余的函数参数
    "no-dupe-args": 2,
    // 类中不要定义冗余的属性
    "no-dupe-class-members": 2,
    // 对象字面量中不要定义重复的属性
    "no-dupe-keys": 2,
    // switch 语句中不要定义重复的 case 分支
    "no-duplicate-case": 2,
    // 同一模块有多个导入时一次性写完
    "no-duplicate-imports": "error",
    // 正则中不要使用空字符
    "no-empty-character-class": 2,
    // 不要解构空值
    "no-empty-pattern": 2,
    //
    "no-eval": 2,
    "no-ex-assign": 2,
    "no-extend-native": 2,
    "no-extra-bind": 2,
    "no-extra-boolean-cast": 2,
    "no-extra-parens": [2, "functions"],
    "no-fallthrough": 2,
    "no-floating-decimal": 2,
    "no-func-assign": 2,
    "no-implied-eval": 2,
    "no-inner-declarations": [2, "functions"],
    "no-invalid-regexp": 2,
    "no-irregular-whitespace": 2,
    "no-iterator": 2,
    "no-label-var": 2,
    "no-labels": [
      2,
      {
        allowLoop: false,
        allowSwitch: false,
      },
    ],
    "no-lone-blocks": 2,
    "no-mixed-spaces-and-tabs": 2,
    "no-multi-spaces": 2,
    "no-multi-str": 2,
    "no-new-func": "error",
    "no-new-object": 2,
    "no-new-require": 2,
    "no-new-symbol": 2,
    "no-new-wrappers": 2,
    "no-obj-calls": 2,
    "no-octal": 2,
    "no-octal-escape": 2,
    "no-path-concat": 2,
    "no-proto": 2,
    "no-redeclare": 2,
    "no-regex-spaces": 2,
    "no-return-assign": [2, "except-parens"],
    "no-self-assign": 2,
    "no-self-compare": 2,
    "no-sequences": 2,
    "no-shadow-restricted-names": 2,
    "no-sparse-arrays": 2,
    "no-template-curly-in-string": "error",
    "no-this-before-super": 2,
    "no-throw-literal": 2,
    "no-trailing-spaces": 2,
    "no-undef-init": 2,
    "no-unmodified-loop-condition": 2,
    "no-unneeded-ternary": [
      2,
      {
        defaultAssignment: false,
      },
    ],
    "no-unreachable": 2,
    "no-unsafe-finally": 2,
    "no-unsafe-negation": "error",
    "no-useless-call": 2,
    "no-useless-computed-key": 2,
    "no-useless-escape": 0,
    "no-useless-rename": 2,
    "no-whitespace-before-property": 2,
    "no-with": 2,
    "padded-blocks": [2, "never"],
    "rest-spread-spacing": ["error", "never"],
    "semi-spacing": [
      2,
      {
        before: false,
        after: true,
      },
    ],
    "space-before-blocks": [2, "always"],
    "space-in-parens": [2, "never"],
    "space-unary-ops": [
      2,
      {
        words: true,
        nonwords: false,
      },
    ],
    "spaced-comment": [
      2,
      "always",
      {
        markers: [
          "global",
          "globals",
          "eslint",
          "eslint-disable",
          "*package",
          "!",
          ",",
        ],
      },
    ],
    "template-curly-spacing": [2, "never"],
    "use-isnan": 2,
    "valid-typeof": 2,
    "wrap-iife": [2, "any"],
    "yield-star-spacing": [2, "both"],
    yoda: [2, "never"],
    // 分号
    semi: [2, "never"],
    "no-unexpected-multiline": 2,
    "arrow-spacing": [
      2,
      {
        before: true,
        after: true,
      },
    ],
    "eol-last": 2,
    "generator-star-spacing": [
      2,
      {
        before: true,
        after: true,
      },
    ],
    "handle-callback-err": [2, "^(err|error)#34;],
    "jsx-quotes": [2, "prefer-single"],
    "no-array-constructor": 2,
    "no-console": "off",
    "no-native-reassign": 2,
    "no-negated-in-lhs": 2,
    "no-shadow-restricted-names": 2,
    "no-spaced-func": 2,
    "no-useless-constructor": 2,
    "one-var": [
      2,
      {
        initialized: "never",
      },
    ],
    "prefer-const": 2,
    "no-debugger": process.env.NODE_ENV === "production" ? 2 : 0,
    "object-curly-spacing": [
      2,
      "always",
      {
        objectsInObjects: false,
      },
    ],
    "array-bracket-spacing": [2, "never"],
  },
  //当使用第三方的SDK时,eslint会报找不到,可以加入到globals,取消对这个的检查
  globals: {
    fengmap: true,
  },
};

如果有需要忽略的文件也可以在 .eslintignore 文件中进行配置:

// .eslintignore
build/*.js
src/assets
public
dist

安装 prettier 依赖

Prettier 是一个代码格式化工具,但并非针对一种语言,对 HTML/CSS/JavaScript/Vue/SCSS 都有效果。可以通过配置文件在不同项目间统一代码格式化,以修正不同编辑器/IDE 之间格式化不同的问题。

安装依赖:

npm i -D prettier eslint-plugin-prettier eslint-config-prettier prettier-eslint-cli
  • prettier:prettier插件的核心代码
  • eslint-plugin-prettier:将prettier作为ESLint规范来使用
  • eslint-config-prettier:解决ESLint中的样式规范和prettier中样式规范的冲突,以prettier的样式规范为准,使ESLint中的样式规范自动失效
  • prettier-eslint-cli:prettier-eslint-cli 允许你对多个文件用prettier-eslint进行格式化。

Prettier 配置

在项目的根目录下创建.prettierrc.js文件并配置prettier代码检查规则:

// .prettierrc.js
module.exports = {
  // 最大长度80个字符
  printWidth: 80,
  // 行末分号
  semi: false,
  // 单引号
  singleQuote: true,
  // JSX双引号
  jsxSingleQuote: false,
  // 尽可能使用尾随逗号(包括函数参数)
  trailingComma: "none",
  // 在对象文字中打印括号之间的空格。
  bracketSpacing: true,
  // > 标签放在最后一行的末尾,而不是单独放在下一行
  jsxBracketSameLine: false,
  // 箭头圆括号
  arrowParens: "avoid",
  // 在文件顶部插入一个特殊的 @format 标记,指定文件格式需要被格式化。
  insertPragma: false,
  // 缩进
  tabWidth: 2,
  // 使用tab还是空格
  useTabs: false,
  // 行尾换行格式
  endOfLine: "auto",
  HTMLWhitespaceSensitivity: "ignore",
};

再更新一下eslint的配置,以处理prettier和eslint的冲突。

// .eslintrc.js
module.exports = {
  // 其他配置。。。
  extends: [
    //继承 vue 的标准特性
    "plugin:vue/essential",
    "eslint:recommended",
    //避免与 prettier 冲突
    "plugin:prettier/recommended",
  ],
  // 其他配置不变。。。
};

开发工具配置

公司的开发工具可能有Vscode和Webstorm。因此这里分开配置,并自行找到对应的方案。

VScode 配置

首先需要安装Eslint、Prettier、Vetur插件。如下图所示:

然后打开setting将下面的代码黏贴进去,这样在 VScode 中保存(ctrl+s)的时候就会进行自动格式化:

{
  "eslint.enable": true,
  "eslint.options": {
    "extensions": [".js", ".vue", ".ts", ".tsx"]
  },
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.validate": [
    "vue",
    "html",
    "javascript",
    "typescript",
    "javascriptreact",
    "typescriptreact"
  ],
  "vetur.format.defaultFormatter.html": "prettier"
}

需要注意的是,在vscode中其实Vetur也有一套Format规则,因此会和prettier冲突。如何解决呢,如下图所示,打开setting将Vetur的规则改成Prettier就好了。

Webstorm 配置

首先WebStorm 配置 ESLint 及时检查:

然后配置保存自动格式化:

打开webstorm设置(file=>setting=>tools=>flie watchers),如下图所所示:

正常情况下 prettier 路径不用改,除非特殊情况。然后一路 ok=>apply 即可。

其他

在项目迁移和规范化的过程中,我们不可能一次性将所有已经存在的代码都格式化成符合Eslint和Prettier规范的代码,因此在实际过程中我们是采用混合开发,也就是在实际做业务需求过程中将改动的文件格式化成符合规范的代码,对尚未碰见的代码不做改动,保证项目的正常运行。在开发的过程中,为了保证团队所有成员都能严格执行 Eslint 规范,采用 husky 构建工作流,eslint 将检查做了修改,存在 stage 阶段尚未 commit 阶段的代码,在 commit 前进行校验,校验无误即通过,否则不通过。当然这不是必须的,因此以下内容可根据需要进行阅读。

安装依赖

npm install husky lint-staged --save-dev
  • husky: 在项目中添加git钩子,在 git 各个生命周期(姑且这样称呼吧)中执行一些自定义操作。我们这里主要是用在 git提交之前执行 linter 操作,不通过则提交无效。
  • lint-staged: 简而言之,就是只针对 git 提交的文件进行一些操作,而非整个项目的所有文件。我们这里主要是用在git 提交之前进行 linter 时只针对提交的文件,以进行渐进式的重构。
// .huskyrc.js
module.exports = {
  hooks: {
    // git commit 前的钩子
    "pre-commit": "lint-staged",
    "post-commit": "git update-index --again",
  },
};
// lint-staged.config.js
module.exports = {
  "src/**/*.{js,vue}": ["eslint --fix", "git add"],
  "src/**/*.{vue,html,css,scss,sass}": ["stylelint --fix", "git add"],
  "src/**/*.{js,vue,html,css,scss,sass}": [
    "prettier-eslint --write",
    "git add",
  ],
};

总结

基本上,这些工具初次配置起来还是非常麻烦的,但这是一件一劳永逸的事情,所以还是值得花时间去做的。

作者:FrontDream

转发链接:https://mp.weixin.qq.com/s/O9m40zo_j-waJGPGESC6WQ

相关推荐

JPA实体类注解,看这篇就全会了

基本注解@Entity标注于实体类声明语句之前,指出该Java类为实体类,将映射到指定的数据库表。name(可选):实体名称。缺省为实体类的非限定名称。该名称用于引用查询中的实体。不与@Tab...

Dify教程02 - Dify+Deepseek零代码赋能,普通人也能开发AI应用

开始今天的教程之前,先解决昨天遇到的一个问题,docker安装Dify的时候有个报错,进入Dify面板的时候会出现“InternalServerError”的提示,log日志报错:S3_USE_A...

用离散标记重塑人体姿态:VQ-VAE实现关键点组合关系编码

在人体姿态估计领域,传统方法通常将关键点作为基本处理单元,这些关键点在人体骨架结构上代表关节位置(如肘部、膝盖和头部)的空间坐标。现有模型对这些关键点的预测主要采用两种范式:直接通过坐标回归或间接通过...

B 客户端流RPC (clientstream Client Stream)

客户端编写一系列消息并将其发送到服务器,同样使用提供的流。一旦客户端写完消息,它就等待服务器读取消息并返回响应gRPC再次保证了单个RPC调用中的消息排序在客户端流RPC模式中,客户端会发送多个请...

我的模型我做主02——训练自己的大模型:简易入门指南

模型训练往往需要较高的配置,为了满足友友们的好奇心,这里我们不要内存,不要gpu,用最简单的方式,让大家感受一下什么是模型训练。基于你的硬件配置,我们可以设计一个完全在CPU上运行的简易模型训练方案。...

开源项目MessageNest打造个性化消息推送平台多种通知方式

今天介绍一个开源项目,MessageNest-可以打造个性化消息推送平台,整合邮件、钉钉、企业微信等多种通知方式。定制你的消息,让通知方式更灵活多样。开源地址:https://github.c...

使用投机规则API加快页面加载速度

当今的网络用户要求快速导航,从一个页面移动到另一个页面时应尽量减少延迟。投机规则应用程序接口(SpeculationRulesAPI)的出现改变了网络应用程序接口(WebAPI)领域的游戏规则。...

JSONP安全攻防技术

关于JSONPJSONP全称是JSONwithPadding,是基于JSON格式的为解决跨域请求资源而产生的解决方案。它的基本原理是利用HTML的元素标签,远程调用JSON文件来实现数据传递。如果...

大数据Doris(六):编译 Doris遇到的问题

编译Doris遇到的问题一、js_generator.cc:(.text+0xfc3c):undefinedreferenceto`well_known_types_js’查找Doris...

网页内嵌PDF获取的办法

最近女王大人为了通过某认证考试,交了2000RMB,官方居然没有给线下教材资料,直接给的是在线教材,教材是PDF的但是是内嵌在网页内,可惜却没有给具体的PDF地址,无法下载,看到女王大人一点点的截图保...

印度女孩被邻居家客人性骚扰,父亲上门警告,反被围殴致死

微信的规则进行了调整希望大家看完故事多点“在看”,喜欢的话也点个分享和赞这样事儿君的推送才能继续出现在你的订阅列表里才能继续跟大家分享每个开怀大笑或拍案惊奇的好故事啦~话说只要稍微关注新闻的人,应该...

下周重要财经数据日程一览 (1229-0103)

下周焦点全球制造业PMI美国消费者信心指数美国首申失业救济人数值得注意的是,下周一希腊还将举行第三轮总统选举需要谷歌日历同步及部分智能手机(安卓,iPhone)同步日历功能的朋友请点击此链接,数据公布...

PyTorch 深度学习实战(38):注意力机制全面解析

在上一篇文章中,我们探讨了分布式训练实战。本文将深入解析注意力机制的完整发展历程,从最初的Seq2Seq模型到革命性的Transformer架构。我们将使用PyTorch实现2个关键阶段的注意力机制变...

聊聊Spring AI的EmbeddingModel

序本文主要研究一下SpringAI的EmbeddingModelEmbeddingModelspring-ai-core/src/main/java/org/springframework/ai/e...

前端分享-少年了解过iframe么

iframe就像是HTML的「内嵌画布」,允许在页面中加载独立网页,如同在画布上叠加另一幅动态画卷。核心特性包括:独立上下文:每个iframe都拥有独立的DOM/CSS/JS环境(类似浏...