ChatGPT解决这个技术问题 Extra ChatGPT

为什么我不断收到 Delete 'cr' [prettier/prettier]?

我将 vscode 与 Prettier 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
  }
}

.prettierrc 文件:

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

我怎样才能摆脱这个错误?

查看您的 .eslintrc.js 文件。从 extends 数组中删除 'plugin:prettier/recommended' 应该可以解决问题。
您可以尝试禁用 ESLint VSCode 扩展

M
Michael Freidgeim

尝试在 .prettierrc(或 .prettierrc.json)文件(对象内部)中设置 "endOfLine":"auto"

或设置

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

在 eslintrc 文件的规则对象中。

如果您使用的是 Windows 机器 endOfLine 可以根据您的 git 配置“crlf”。


更改 .eslintrc 文件对我有用,但不是 .prettierrc 文件。不知道为什么或有什么区别(我对 OP 上的所有标签都是新手)。
我的猜测是您可能需要在 VS Code 中使用 Prettier 扩展。 prettierrc 仅在这种情况下有效。
在 Windows 机器上将行尾从 CRLF 更改为 LF 对我有用
对于像我这样的新手来说,这就是要做的事情。打开根目录 (frontend) 中的 .eslintrc.json。更改后将如下所示:{ "extends": ["react-app", "prettier"], "plugins": ["prettier"], "rules": { "prettier/prettier": ["error", { "endOfLine": "auto" }] } }
仅供参考:更改需要重新启动才能在 VSCode 上生效。
Y
Yaobin Then

在 VSCode 上更改此设置。

https://i.stack.imgur.com/YkKpb.png


这将解决问题,但只有在您使用 CRLF 打开其他源文件之前。上面的答案更有效。
除了在 VSCode 中将 CRLF 更改为 LF 之外,git 可能还在后台进行自动转换。如果您在安装时选择了 checkout Windows-style,它会将您克隆的源代码转换为 CRLF。所以重新安装 git 并选择 checkout as-is, commit Unix-style 将修复它。
B
Brent Woodle

在我的 Windows 机器中,我通过在当前项目目录中存在的 .eslintrc.js 文件的 rules 对象中添加以下代码片段来解决此问题。

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

这也适用于我的 Mac


k
koko-js478

解决方案

git config --global core.autocrlf false

全局配置后,需要再次拉取代码。

问题的根本原因:

罪魁祸首是 gitcore.autocrlf 的配置属性

由于历史原因,windowslinux 上的文本文件的换行符不同。

Windows换行时,同时使用回车CR(回车符)和换行符LF(换行符)

Mac 和 Linux 只使用换行符 LF

旧版Mac使用回车CR

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

当我在 Windows 上克隆代码时,autocrlf 默认为 true,然后文件的每一行都会自动转换为 CRLF。如果您不对文件进行任何更改,则 eslint 删除 CR by pre-commit,因为 git 会自动将 CRLF 转换为 LF

参考

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


该解决方案将打破跨平台开发 - 您可以将混合或 LF/CR/CRLF 推送到 repo。我建议不要使用它,除非你单独工作,并且只在一种操作系统(Linux/Mac/Windows)上工作。对于团队来说,一个好的配置是使用 autocrlf=true 并仅配置 linting 工具以尊重它们运行的平台的 EOL 字符,如其他答案(“endOfLine”:“auto”)。更糟糕的是,您建议设置 autocrlf=false GLOBALLY,我认为如果需要这样做,它应该始终是 per-repo,否则您的所有新 repos 从一开始就有问题。
M
Mohammed Al-Reai

在辅助角色的文件 .eslintrc.json 中添加此代码将解决此问题

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

  }

n
naspinski

如果上面的代码对您不起作用,请尝试这两个步骤。

1. 在文件 .eslintrc.json 里面的 rules 对象中添加这段代码就可以解决这个问题

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

2 更改开发服务器--fix

npm run dev

npm run dev --fix

或者

npm run lint -- --fix
yarn run lint -- --fix

H
Hamza Waleed

已修复 - 我的 .eslintrc.js 看起来像这样:

module.exports = {
  root: true,
  extends: '@react-native-community',
  rules: {'prettier/prettier': ['error', {endOfLine: 'auto'}]},
};

R
Radu Luncasu

我知道这很旧,但我刚刚在我的团队中遇到了这个问题(一些 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/


A
Akif

尝试这个。这个对我有用:

纱线运行皮棉--修复

或者

npm 运行 lint -- --fix


W
Waket Zheng

我用的是git+vscode+windows+vue,看了eslint文档后:https://eslint.org/docs/rules/linebreak-style

最后通过以下方式修复它:

*.js text eol=lf 添加到 .gitattributes

然后运行 vue-cli-service lint --fix


A
Ashique Ansari

修复:我的 eslintrc.js 一些规则如下所示:

rules: {
    'prettier/prettier': ['error', { "endOfLine": "auto"}, { usePrettierrc: true }],  // Use our .prettierrc file as source
    'react/react-in-jsx-scope': 'off',
    'react/prop-types': 'off',
    'simple-import-sort/imports': 'error',
    "simple-import-sort/exports": "error"
}

T
Tri Dawn

在 .eslintrc 文件中添加以下内容:

扩展:['prettier'] 和插件:['prettier']

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

在 .prettierrc 中删除:

endOfLine: '自动'

这个对我有用。


V
Vince

将此添加到您的 .prettierrc 文件并打开 VSCODE

"endOfLine": "auto"

z
zernab hussain

在 .eslintrc 文件中添加以下规则,然后重新启动您的项目。

rules: {
    'prettier/prettier': ['error', { "endOfLine": "auto"}, { usePrettierrc: true }],  
}

s
shaedrich

我在这里尝试了一切,对我来说,我需要通过图标扩展>更漂亮>小引擎>扩展设置>更漂亮:行尾>设置为自动来管理更漂亮的配置扩展。

在我的 settings.json 中添加这两行之后

"eslint.run": "onSave",
"editor.formatOnSave": true,

我能够在 .eslintrc.js 规则中使用下面的配置。

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

D
Darren Evans

检查底部 VS Code 状态栏的右侧,它显示行和列、空格、文本编码(UTF-8 等)等信息。您应该会看到一个 Select End Of Line Sequence 状态显示(LFCRLF),您可以点击它进行更改。确保您没有手动将其更改为与您希望 Prettier 使用的内容相冲突的内容。


r
r4k3sh

我在我的 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',
  },
],

},


S
Shamaz saeed

npm 运行 lint -- --fix

运行这个对我有用的命令


对我来说也谢谢你!
F
Frank Guo

如您所见,将其添加到 .eslintrc 作品中!

https://i.stack.imgur.com/i633N.png


E
Elias

https://i.stack.imgur.com/lxxuv.png

git config --global core.autocrlf input

再次签出代码并再次打开 Visual Studio Code 并再次运行您的脚本。它对我有用。


为什么是input?这个建议与 koko-js478 的建议有何不同?
R
Roberto LL

我升级到“更漂亮”:“^2.2.0”并且错误消失了


r
richardwhatever

在根目录中打开 .editorconfig 文件并更改:

end_of_line = lf

end_of_line = auto

这应该为新文件修复它。


C
Cláudio

对我有用的是:

按照 Roberto LL 的建议,将 prettier 更新到 2.2.1 版(目前的最新版本)。要做到这一点,请执行

npm 更新更漂亮

按照 Hakan 的建议执行 lint fix(这将修改项目中的所有文件以将行尾转换为 LF)。

npm 运行 lint -- --fix

没有必要更改 .eslintrc 和 .prettierrc 文件!


prettier 在我的机器上是最新的,但是运行 npm run lint -- --fix 解决了这个问题。
这对我有用nestjs和windows,谢谢!
我尝试了这里提出的许多其他解决方案,但只有这样才能解决问题。
S
Shraddha Goel

解决方案

1.禁用自动转换git设置

git --global config core.autocrlf false

2.删除旧缓存数据

git rm --cached -r 。

3.重置git文件

git reset --hard


P
Peppers

当我从 git 中提取代码时出现错误,这对我有用:

步骤1:

git config --global core.autocrlf false

第2步:

删除当前文件夹

第 3 步:

再次从 git 拉取代码

再次尝试运行命令


git config --global core.autocrlf false 这个命令效果很好,解决了vs code上的LF问题
如果您正在处理具有不同配置的不同项目,您可以通过省略 --global 标志在本地为该存储库设置标志。此外,您不需要删除整个 repo 并再次克隆它,只需将分支重置为 origin 即可。
S
Srilal Sachintha

编辑您的 .eslintrc.json 文件并更新如下所示的“prettier/prettier”值。

我面临同样的问题并使用以下更改进行修复。

"prettier/prettier": [
    "error", {
         "singleQuote": true,
         "parser": "flow" 
    }
],

P
Petr Tripolsky

没有必要忽略 linter 的规则。只是自动修复它

npm install -g prettier
prettier -w .\webpack.config.js # or other file

A
AndriyFM

以上所有答案都是正确的,但是当我使用 windows 并禁用 Prettier ESLint 扩展 rvest.vs-code-prettier-eslint时,问题将得到解决。


K
Keerthi Reddy Yeruva

如果您已经签出代码

git config --global core.autocrlf input 


git rm --cached -r . 


git reset --hard

这将删除所有本地 git 更改,请小心执行这些命令。
S
Sergey Barabanov

如果您需要与 .prettierrc.js 一起使用:

module.exports = {
    ...[config params],
    endOfLine: 'auto',
};

笔记!不在 rulesprettier/prettier 中。

此处的信息https://prettier.io/docs/en/options.html#end-of-line


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅