简体   繁体   English

与 Prettier 不一致

[英]Inconsistencies with Prettier

I have a Vue project, which is having some inconsistencies with its linting.我有一个 Vue 项目,它的 linting 有一些不一致。

I'm using the latest version of VS Code.我正在使用最新版本的 VS Code。

For instance, this is a linting error I receive:例如,这是我收到的一个 linting 错误:

在此处输入图像描述

After saving the file, VS Code auto fixes so that the code looks is linted like this保存文件后,VS Code 自动修复,使代码看起来像这样

在此处输入图像描述

However, when I try to run npm run serve但是,当我尝试运行npm run serve

I get an error, asking to change it back.我收到一个错误,要求将其更改回来。

error: Replace `(h)·=>·h(App),` with `h·=>·h(App)` (prettier/prettier) at src/main.js:91:11:
  89 |   router,
  90 |   store,
> 91 |   render: (h) => h(App),
     |           ^
  92 | }).$mount("#app");
  93 | 

my.eslintrc.json file is my.eslintrc.json 文件是

{
  "env": {
    "browser": true,
    "es6": true,
    "jest/globals": true
  },
  "extends": [
    "plugin:vue/essential",
    "airbnb-base",
    "plugin:prettier/recommended"
  ],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
    "parser": "babel-eslint"
    // "ecmaVersion": 2018,
    // "sourceType": "module"
  },
  "plugins": [
    "vue",
    "jest"
  ],
  "rules": {},
  "overrides": [
    {
      "files": [
        "*.js",
        "*.vue"
      ],
      "rules": {
        "sort-imports": "off",
        "spaced-comment": "off",
        "import/prefer-default-export": "off",
        "import/no-unresolved": "off",
        "import/extensions": "off",
        "func-names": "off",
        "object-shorthand": "off",
        "eqeqeq": "warn",
        "prefer-const": "off",
        "camelcase": "off",
        "no-plusplus": "off",
        "no-else-return": "off",
        "consistent-return": "off",
        "no-restricted-syntax": "off",
        "no-shadow": "off",
        "prefer-destructuring": "off",
        "no-return-assign": "off",
        "guard-for-in": "off",
        "jest/no-disabled-tests": "warn",
        "jest/no-focused-tests": "error",
        "jest/no-identical-title": "error",
        "jest/prefer-to-have-length": "warn",
        "jest/valid-expect": "error"
      }
    }
  ]
}

and my VSCode settings.json file is我的 VSCode settings.json 文件是

{
  "eslint.codeAction.showDocumentation": {
    "enable": true
  },
  "explorer.confirmDelete": false,
  "remote.extensionKind": {
    "pub.name": ["ui"]
  },
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.validate": ["javascript", "vue", "html"],
  "editor.formatOnPaste": true,
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "eslint.alwaysShowStatus": true,

  "eslint.run": "onSave",
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "vetur.format.defaultFormatter.html": "prettier",
  "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
  "vetur.format.defaultFormatter.stylus": "none",
  "editor.formatOnType": true,
  "workbench.colorTheme": "Community Material Theme Darker",
  "window.zoomLevel": 1
}

I'm guessing there must be a project vs vs code inconsistency, but I can find it.我猜肯定有一个项目与 vs 代码不一致,但我可以找到它。

Using npm run serve auto fixes the file, however, if I save the offending file again, suddenly it pops up with linting errors all over again (which then cause the Vue app to crash in dev).使用npm run serve自动修复文件,但是,如果我再次保存有问题的文件,它会突然再次弹出 linting 错误(这会导致 Vue 应用程序在开发中崩溃)。

I've tried npm cache clear --force and changing permissions on node modules, and reinstalling them, but no luck.我试过npm cache clear --force和更改节点模块的权限,并重新安装它们,但没有运气。

I think I need to tell my local rules to override prettier, but am unsure how to do so我想我需要告诉我的本地规则来覆盖更漂亮,但我不确定该怎么做

You need to use eslint-config-prettier to turn off eslint rules that conflict with prettier.需要使用eslint-config-prettier prettier 来关闭与 prettier 冲突的 eslint 规则。 Make sure to also include the prettier/vue rules确保还包括prettier/vue规则

https://github.com/prettier/eslint-config-prettier https://github.com/prettier/eslint-config-prettier

 "extends": [
   "plugin:vue/essential",
   "airbnb-base",
   "plugin:prettier/recommended",
   “prettier”,
   “prettier/vue”
 ],

To solve conflict解决冲突

You can make eslint accept all the prettier formattings by installing eslint configuration for prettier您可以通过安装更漂亮的 eslint 配置来使 eslint 接受所有更漂亮的格式

npm install eslint-config-prettier

And include it in the extends option in the file .eslintrc.js并将其包含在文件.eslintrc.jsextends选项中

extends: [
  ...,
  "prettier",
],

Now it won't complain on the prettier format preferences现在它不会抱怨更漂亮的格式偏好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM