我使用VS Code与漂亮的1.7.2和ESLint 1.7.0。 在每个换行符之后:

[eslint] Delete `CR` [prettier/prettier]

这是.eslintrc.json:

{
  "extends": ["airbnb", "plugin:prettier/recommended"],
  "env": {
    "jest": true,
    "browser": true
  },
  "rules": {
    "import/no-extraneous-dependencies": "off",
    "import/prefer-default-export": "off",
    "no-confusing-arrow": "off",
    "linebreak-style": "off",
    "arrow-parens": ["error", "as-needed"],
    "comma-dangle": [
      "error",
      {
        "arrays": "always-multiline",
        "objects": "always-multiline",
        "imports": "always-multiline",
        "exports": "always-multiline",
        "functions": "ignore"
      }
    ],
    "no-plusplus": "off"
  },
  "parser": "babel-eslint",
  "plugins": ["react"],
  "globals": {
    "browser": true,
    "$": true,
    "before": true,
    "document": true
  }
}

. pretierrc文件:

{
  "printWidth": 80,
  "tabWidth": 2,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "es5",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
}

如何消除这个错误?


当前回答

这是我的工作

步骤1 React js根目录找到.eslintrc文件

步骤2 在.eslintrc找到

"prettier/prettier": [
  2,
  {
    "printWidth": 100,
    "singleQuote": true,
    "trailingComma": "none",
    "tabWidth": 2
  }
]

替换为

"prettier/prettier": [
  "error",
  {
    "endOfLine": "auto"
  }
]

保存文件,然后运行

npm start

其他回答

我知道这是旧的,但我刚刚在我的团队(一些mac,一些linux,一些windows,所有的vscode)遇到了这个问题。

解决方案是在vscode的设置中设置行结束:

.vscode/settings.json

{
    "files.eol": "\n",
}

https://qvault.io/2020/06/18/how-to-get-consistent-line-breaks-in-vs-code-lf-vs-crlf/

检查VS Code底部状态栏的右侧,在那里它显示了诸如行和列,空格,文本编码(UTF-8等)等信息。您应该看到一个Select End Of Line Sequence状态显示(LF或CRLF),您可以单击它进行更改。请确保您没有手动更改它,使其与您希望pretty使用的内容冲突。

我在我的nest js应用程序中也有同样的问题。将下面的代码添加到.eslintrc.jsrules中,然后运行yarn run lint——fix修复了这个问题。

'prettier/prettier': [
  'error',
  {
    endOfLine: 'auto',
  },
],

我的.eslintrc.js规则看起来是这样的。

 rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'prettier/prettier': [
  'error',
  {
    endOfLine: 'auto',
  },
],

},

在VSCode上改变这个设置。

解决方案

git config --global core.autocrlf false

在全局配置之后,您需要再次拉出代码。

问题的根本原因:

罪魁祸首是git, core. selff的配置属性

由于历史原因,windows和linux下文本文件的换行方式不同。

在换行时,换行符同时使用CR(换行符)和LF(换行符) Mac和Linux只使用换行符LF 旧版本的Mac使用回车CR

因此,在不同的系统中创建和使用文本文件时会出现不兼容问题。

当我在Windows上克隆代码时,默认为true,然后文件的每一行都自动转换为CRLF。如果你没有对文件做任何改变,eslint通过预提交删除CR,因为git会自动将CRLF转换为LF。

参考

https://developpaper.com/solution-to-delete-%E2%90%8Deslint-prettier-prettier-error/