简体   繁体   English

eslint/prettier 在 html 标签中添加不需要的空间

[英]eslint/prettier adding unwanted space into html tags

I am trying to add configurations to my project so that the code auto formats.我正在尝试向我的项目添加配置,以便代码自动格式化。 I have made some updates to the user settings in VS Code and also installed eslint and prettier.我对 VS Code 中的用户设置进行了一些更新,还安装了 eslint 和 prettier。

Now whenever I try to save my code gets changer from this现在,每当我尝试保存我的代码时,都会从中获取更改器

<div className={style.app}>
  <div className={style.mobileHeader}>
    <div className={style.logoBox}>
    </div>
  </div>
</div>

to

<
    div className = {
      style.app
    } >
    <
    div className = {
      style.mobileHeader
    } >
    <
    div className = {
      style.logoBox
    } >

All this added whitespace is not only unreadable buy my IDE doesn't even register it as JavaScript.所有这些添加的空格不仅不可读,我的 IDE 甚至没有将其注册为 JavaScript。

I have tried many different configurations in my.eslintrc.js or.eslintrc.json (I only have one but I have tried both naming conventions).我在 my.eslintrc.js 或.eslintrc.json 中尝试了许多不同的配置(我只有一个,但我尝试了两种命名约定)。 Currently I have deleted all the content in my.eslintrc.json except for the empty brackets {}.目前我已经删除了my.eslintrc.json中的所有内容,除了空括号{}。 I also recently updated my user settings to我最近也将我的用户设置更新为

{
  "files.autoSave": "afterDelay",
  "window.zoomLevel": 0,
  "git.autofetch": true,
  "explorer.confirmDragAndDrop": false,
  "workbench.startupEditor": "welcomePage",
  "dart.flutterSdkPath": "/Users/trevor/Applications/flutter",
  "javascript.updateImportsOnFileMove.enabled": "always",
  "python.pythonPath": "/usr/local/bin/python3",
  "editor.defaultFormatter": "octref.vetur",
  "vetur.format.defaultFormatter.html": "prettier",
  "vetur.format.defaultFormatter.css": "prettier",
  "vetur.format.defaultFormatter.postcss": "prettier",
  "vetur.format.defaultFormatter.scss": "prettier",
  "vetur.format.defaultFormatter.less": "prettier",
  "vetur.format.defaultFormatter.stylus": "stylus-supremacy",
  "vetur.format.defaultFormatter.js": "prettier",
  "vetur.format.defaultFormatter.ts": "prettier",
  "vetur.validation.template": false,
  "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
  "eslint.autoFixOnSave": true,
  "editor.formatOnSave": true,
  "eslint.validate": [{
      "language": "vue",
      "autoFix": true
    },
    {
      "language": "html",
      "autoFix": true
    },
    {
      "language": "javascript",
      "autoFix": true
    },
    {
      "language": "react",
      "autoFix": true
    },
  ]
}

I am thinking something here must be causing the issue but I can't see what it would be.我在想这里一定是有什么问题导致了这个问题,但我看不出它会是什么。

I am noticing the only code that is being formatted is the code inside the return statements of my of my React pages.我注意到唯一被格式化的代码是我的 React 页面的 return 语句中的代码。 Other code seems uneffected.其他代码似乎未受影响。

Here's what I usually do in VS Code to get clean and working configs of Prettier and ES Lint这是我通常在 VS Code 中所做的,以获得 Prettier 和 ES Lint 的干净和工作配置

Step 1 Install Prettier & Eslint Extensions inside VS Code.第 1 步在 VS Code 中安装 Prettier 和 Eslint 扩展。 (if not installed already) (如果尚未安装)

Step 2 Install prettier , eslint and eslint-config-prettier from npm for the project as dev dependencies.步骤 2npm为项目安装prettiereslinteslint-config-prettier prettier 作为开发依赖项。 eslint-config-prettier is important to install as it turns off all ESlint rules that are unnecessary or might conflict with Prettier. eslint-config-prettier很重要,因为它会关闭所有不必要或可能与 Prettier 冲突的 ESlint 规则。

npm install -D prettier eslint eslint-config-prettier

Step 3 Turn on Format on Save User Settings.第 3 步在保存用户设置上打开格式。

"editor.formatOnSave": true,

Step 4 Make Sure Prettier requires a config file.第 4 步确保 Prettier 需要一个配置文件。

"prettier.requireConfig": true,

Step 5 Make a config file .prettierrc to let editors and other tooling know you are using Prettier and keep it empty to accept default config from prettier.第 5 步创建一个配置文件.prettierrc让编辑器和其他工具知道您正在使用 Prettier,并将其保留为空以接受来自 prettier 的默认配置。 (It works for me 99% of the time) (它 99% 的时间都对我有用)

echo {} > .prettierrc.json

Step 6 Also make sure that ESlint is not taking any formatting responsibilities in your vscode user configuration第 6 步还要确保 ESlint 在您的 vscode 用户配置中不承担任何格式化责任

"eslint.format.enable": false,

Here is a pretty basic eslintrc.json config that I use while starting new projects.这是我在开始新项目时使用的非常基本的eslintrc.json配置。

{
  "extends": [
    "eslint:recommended",
    "prettier",
    "prettier/react"
  ],
  "plugins": [],
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module",
    "ecmaFeatures": { "jsx": true }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  }
}

Documentation References for Prettier , ES Lint and eslint-config-prettier PrettierES Linteslint-config-prettier 的文档参考

I just encountered this same issue.我刚刚遇到了同样的问题。 In my case, it was due to the JS-CSS-HTML Formatter VSCode extension.就我而言,这是由于 JS-CSS-HTML Formatter VSCode 扩展。 Disabling the extension fixed the issue.禁用扩展解决了这个问题。

you can try this你可以试试这个

1- install these in devDependencies in the package.json 1-将这些安装在 package.json 的 devDependencies 中

 "devDependencies": {
    "eslint": "^6.6.0",
    "eslint-config-airbnb-base": "^14.1.0",
    "eslint-config-prettier": "^6.10.1",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-prettier": "^3.1.2",
    "prettier": "^2.0.2",
  }

2- add this code to eslint.js 2-将此代码添加到 eslint.js

module.exports = {
    env: {
      commonjs: true,
      es6: true,
      node: true
    },
    root: true,
    extends: ["airbnb-base", "prettier"],
    globals: {
      Atomics: "readonly",
      SharedArrayBuffer: "readonly"
    },
    parserOptions: {
      ecmaVersion: 2018
    },
    rules: { 
      "linebreak-style": 0,
      "arrow-body-style": ["error", "as-needed"],
      "prettier/prettier": [
        "error",
        {
          trailingComma: "es5",
          singleQuote: true
        }
      ]
    },
    plugins: ["prettier"]
  };

3- open setting in vs code and search about json 3-在vs代码中打开设置并搜索json

4- click Edit in settings.json and change not important commands 4-单击设置中的编辑。json 并更改不重要的命令

Install these in devDependencies in the package.json :devDependenciespackage.json中安装这些:

"devDependencies": {
  "eslint-config-prettier": "^6.10.1",
  "eslint-plugin-prettier": "^3.1.2",
  "prettier": "^2.0.4"
},

Add this to package.json if you are using create-react-app :如果您使用的是create-react-app ,请将其添加到package.json

"eslintConfig": {
  "extends": [
    "react-app",
    "plugin:prettier/recommended"
  ],
  "ignorePatterns": [
    "node_modules/",
    "build/"
  ]
},

The most important part, In your VSCode settings.json , add those lines:最重要的部分,在您的 VSCode settings.json ,添加这些行:

"editor.codeActionsOnSave": {
  "source.fixAll.eslint": true
},
"editor.formatOnSave": false,

Now when you save a js, jsx, ts, tsx file eslint should automatically fix any problems including prettier formatting problems.现在当你保存一个 js、jsx、ts、tsx 文件时, eslint应该会自动修复任何问题,包括prettier的格式问题。

In my case, the most valuable solution turned out to be ignoring specific parts of the document where I had problems with prettier adding whitespace.就我而言,最有价值的解决方案是忽略文档的特定部分,在这些部分中我在添加prettier的空白时遇到了问题。

In this example, prettier previously broke the quote into its own line, hence the text had whitespace between the quotes from the <q> tag.在这个例子中,prettier 之前将引号分成了自己的一行,因此文本在<q>标记的引号之间有空格。

<!-- prettier-ignore-start -->
<q>Make it work, make it right, make it fast.</q>
<!-- prettier-ignore-end -->

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

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