前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Webstorm+vue+eslint+prettier融合问题

Webstorm+vue+eslint+prettier融合问题

作者头像
无道
发布2021-07-22 15:52:32
1.9K0
发布2021-07-22 15:52:32
举报
文章被收录于专栏:无道编程无道编程

前言

跟着此文章步骤一点点走下去不一定完全就没问题了,因为都是一点点调出来的的,根本不知道哪些地方改了就没问题,哪些地方改了就有问题了。

那么使用WebStorm存在的问题是:

在webstorm中,调用快捷键格式化,和保存后自动格式化代码的规则不一致,需要调试。

这是一个很简单的问题,也是一个很难受的问题。

根据prettier官方文档Integrating with Linters · Prettier

使用eslint-config-prettier

prettier/eslint-config-prettier: Turns off all rules that are unnecessary or might conflict with Prettier. (github.com)

.eslintrc.json配置:

代码语言:javascript
复制
"extends": [
    "prettier"
]

这样eslint与prettier冲突的规则会被关闭(官网: "extends": ["prettier"] enables the config from eslint-config-prettier, which turns off some ESLint rules that conflict with Prettier. )

总结

反正经过一段时间调试,终于在webstorm中实现快捷键和保存格式,代码风格也一致的效果了:

1、安装

代码语言:javascript
复制
yarn add eslint-config-prettier  -D

# 大概要安装以下:
yarn add @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-prettier eslint-config-prettier eslint eslint-plugin-vue -D

2、配置.eslintrc.json

代码语言:javascript
复制
"extends": [
    "prettier"
],

完整配置参考:

代码语言:javascript
复制
{
  "root": true,
  "env": {
    "es2021": true,
    "node": true,
    "browser": false
  },
  "extends": [
    "eslint:recommended",
    /** @see https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#recommended-configs */
    "plugin:@typescript-eslint/recommended",
    "prettier"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 12,
    "sourceType": "module"
  },
  "plugins": [
    "@typescript-eslint"
  ],
  "ignorePatterns": [
    "types/env.d.ts",
    "node_modules/**",
    "**/dist/**"
  ],
  "rules": {
    "@typescript-eslint/no-unused-vars": "error",
    "@typescript-eslint/no-var-requires": "off",
    "@typescript-eslint/consistent-type-imports": "error",
    "@typescript-eslint/explicit-module-boundary-types": "off",
    //    "space-before-blocks": "warn",
    //    "space-before-function-paren": "warn",
    //    "space-in-parens": "warn",
    //    "no-whitespace-before-property": "off",
    /**
     * Having a semicolon helps the optimizer interpret your code correctly.
     * This avoids rare errors in optimized code.
     * @see https://twitter.com/alex_kozack/status/1364210394328408066
     */
    "semi": [
      "error",
      "always"
    ],
    /**
     * This will make the history of changes in the hit a little cleaner
     */
    "comma-dangle": [
      "warn",
      "always-multiline"
    ],
    /**
     * Just for beauty
     */
    "quotes": [
      "warn",
      "single"
    ],
    "space-infix-ops": "warn",
    "space-unary-ops": "warn"
  }
}

注意:我的webstorm版本是2021.1,如果你是低于这个版本的,特别是2020.1之前的,因为2020.1之前,webstorm需要手动安装prettier插件

html引号问题

.vue模板中,script我喜欢用单引号,但是在template中喜欢双引号,但是每次使用快捷键格式化时,都会把template变成单引号,如果上面的配置没解决问题

那么修改.idea/codeStyles/codeStyleConfig.xml文件,

代码语言:javascript
复制
<HTMLCodeStyleSettings>
  <option name="HTML_SPACE_INSIDE_EMPTY_TAG" value="true" />
  <option name="HTML_QUOTE_STYLE" value="Single" />
  <option name="HTML_ENFORCE_QUOTES" value="true" />
</HTMLCodeStyleSettings>

如果其中有Single,那么去掉这行

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-07-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 总结
  • html引号问题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档