经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » TypeScript » 查看文章
Vue3+TypeScript 项目中,配置 ESLint 和 Prettier
来源:cnblogs  作者:惊蛰丶  时间:2023/1/30 15:20:50  对本文有异议

接上篇:从0搭建vite-vue3-ts项目框架:配置less+svg+pinia+vant+axios

文档同步项目gitee:https://gitee.com/lixin_ajax/vue3-vite-ts-pinia-vant-less.git

 

一、Eslint:用于检测代码

安装eslint相关依赖

  1. yarn add eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin -D

eslint-plugin-vue:仅支持vue,提供的规则可以支持 .vue\js\jsx\ts\tsx 文件校验

@typescript-eslint/parser:解析器,让ESLint拥有规范TypeScript代码的能力

@typescript-eslint/eslint-plugin:插件,包含一系列TypeScript的ESint规则

 

初始化eslint

  1. npx eslint --init

选择项目eslint配置,回车确认,空格多选

  1. How would you like to use ESLint? · style 你希望怎样使用eslint
  2. What type of modules does your project use? · esm  你的项目使用什么模块
  3. Which framework does your project use? · vue 项目框架
  4. Does your project use TypeScript? · No / Yes 是否使用typescript
  5. Where does your code run? · browser, node 代码运行在哪
  6. How would you like to define a style for your project? · guide 项目样式
  7. Which style guide do you want to follow? · standard-with-typescript 项目风格
  8. What format do you want your config file to be in? · JavaScript 配置文件格式
  9. Would you like to install them now? · No / Yes 确认是否安装
  10. Which package manager do you want to use? · yarn 安装方式

安装完成,项目根目录生成.eslintrc.cjs文件

  1. // .eslintrc.cjs
  2. module.exports = {
  3. env: {
  4. browser: true,
  5. es2021: true,
  6. node: true
  7. },
  8. extends: [
  9. 'plugin:vue/vue3-essential',
  10. 'standard-with-typescript'
  11. ],
  12. overrides: [
  13. ],
  14. parserOptions: {
  15. ecmaVersion: 'latest',
  16. sourceType: 'module'
  17. },
  18. plugins: [
  19. 'vue'
  20. ],
  21. rules: {
  22. }
  23. }

在.eslintrc.cjs rules中配置eslint规则细节

rules配置eslint官网:https://eslint.org/docs/latest/rules/ 

腾讯云开发社区中文文档:https://cloud.tencent.com/developer/doc/1078

常用规则,参考:https://blog.csdn.net/ivenqin/article/details/104673237/

eslint-plugin-vue rules:https://eslint.vuejs.org/rules/

我的rules:

  1. rules: {
  2. "vue/no-v-html": "off",
  3. "vue/script-setup-uses-vars": "off",
  4. "@typescript-eslint/ban-ts-ignore": "off",
  5. "@typescript-eslint/explicit-function-return-type": "off",
  6. "@typescript-eslint/no-explicit-any": "off",
  7. "@typescript-eslint/no-var-requires": "off",
  8. "@typescript-eslint/no-empty-function": "off",
  9. "vue/custom-event-name-casing": "off",
  10. "no-use-before-define": "off",
  11. "@typescript-eslint/no-use-before-define": "off",
  12. "@typescript-eslint/ban-ts-comment": "off",
  13. "@typescript-eslint/ban-types": "off",
  14. "@typescript-eslint/no-non-null-assertion": "off",
  15. "@typescript-eslint/explicit-module-boundary-types": "off",
  16. "@typescript-eslint/no-unused-vars": [
  17. "error",
  18. {
  19. argsIgnorePattern: "^_",
  20. varsIgnorePattern: "^_",
  21. },
  22. ],
  23. "no-unused-vars": [
  24. "error",
  25. {
  26. argsIgnorePattern: "^_",
  27. varsIgnorePattern: "^_",
  28. },
  29. ],
  30. "space-before-function-paren": "off",
  31.  
  32. "vue/attributes-order": "off",
  33. "vue/one-component-per-file": "off",
  34. "vue/html-closing-bracket-newline": "off",
  35. "vue/max-attributes-per-line": "off",
  36. "vue/multiline-html-element-content-newline": "off",
  37. "vue/singleline-html-element-content-newline": "off",
  38. "vue/attribute-hyphenation": "off",
  39. "vue/require-default-prop": "off",
  40. "vue/require-explicit-emits": "off",
  41. "vue/html-self-closing": [
  42. "error",
  43. {
  44. html: {
  45. void: "always",
  46. normal: "never",
  47. component: "always",
  48. },
  49. svg: "always",
  50. math: "always",
  51. },
  52. ],
  53. "vue/multi-word-component-names": "off",
  54. "vue/no-parsing-error": ["off"],
  55. "eol-last": "off",
  56. },

  

 

二、Prettier:用于格式化代码

 安装prettier相关依赖

  1. yarn add prettier eslint-config-prettier eslint-plugin-prettier stylelint-config-prettier -D

eslint-config-prettier:解决eslint和prettier冲突

eslint-config-prettier:将prettier作为eslint规则

stylelint-config-prettier:关闭所有不必要的或者有可能与Prettier冲突的规则

 

修改.eslintrc.cjs,配置prettier

  1. extends: [
  2. "plugin:prettier/recommended",
  3. ],
  4. plugins: ["prettier"],
  5. rules: {
  6. "prettier/prettier": [
  7. "error",
  8. {
  9. endOfLine: "auto",
  10. },
  11. ],
  12. }

  

修改package.json,配置修复项目格式命令

  1. "scripts": {
  2. "lint-fix": "eslint . --fix"
  3. },

执行命令,yarn lint-fix,项目按照eslint-prettier规则进行代码格式化

修改prettier规则:根目录创建.prettierrc文件,示例如下,更多规则参考官网

  1. {
  2. printWidth: 120, // 超过最大值换行
  3. tabWidth: 2, // 缩进字节数
  4. useTabs: false, // 缩进不使用tab,使用空格
  5. semi: false, // 句尾添加分号
  6. singleQuote: true, // 使用单引号代替双引号
  7. arrowParens: "avoid", // (x) => {} 箭头函数参数只有一个时是否要有小括号。avoid:省略括号
  8. ignorePath: ".prettierignore", // 不使用prettier格式化的文件填写在项目的.prettierignore文件中
  9. jsxBracketSameLine: false, // 在jsx中把'>' 是否单独放一行
  10. jsxSingleQuote: true, // 在jsx中使用单引号代替双引号
  11. trailingComma: "all" // 在对象或数组最后一个元素后面是否加逗号(在ES5中加尾逗号)
  12. }

配置完成!

原文链接:https://www.cnblogs.com/jing-zhe/p/16934493.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号