我将 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,
}
我怎样才能摆脱这个错误?
'plugin:prettier/recommended'
应该可以解决问题。
尝试在 .prettierrc(或 .prettierrc.json)文件(对象内部)中设置 "endOfLine":"auto"
或设置
'prettier/prettier': [
'error',
{
'endOfLine': 'auto',
}
]
在 eslintrc 文件的规则对象中。
如果您使用的是 Windows 机器 endOfLine 可以根据您的 git 配置“crlf”。
在 VSCode 上更改此设置。
https://i.stack.imgur.com/YkKpb.png
CRLF
更改为 LF
之外,git
可能还在后台进行自动转换。如果您在安装时选择了 checkout Windows-style
,它会将您克隆的源代码转换为 CRLF
。所以重新安装 git
并选择 checkout as-is, commit Unix-style
将修复它。
在我的 Windows 机器中,我通过在当前项目目录中存在的 .eslintrc.js
文件的 rules
对象中添加以下代码片段来解决此问题。
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
},
],
这也适用于我的 Mac
解决方案
git config --global core.autocrlf false
全局配置后,需要再次拉取代码。
问题的根本原因:
罪魁祸首是 git
,core.autocrlf
的配置属性
由于历史原因,windows
和 linux
上的文本文件的换行符不同。
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/
在辅助角色的文件 .eslintrc.json 中添加此代码将解决此问题
"rules": {
"prettier/prettier": ["error",{
"endOfLine": "auto"}
]
}
如果上面的代码对您不起作用,请尝试这两个步骤。
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
已修复 - 我的 .eslintrc.js 看起来像这样:
module.exports = {
root: true,
extends: '@react-native-community',
rules: {'prettier/prettier': ['error', {endOfLine: 'auto'}]},
};
我知道这很旧,但我刚刚在我的团队中遇到了这个问题(一些 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/
尝试这个。这个对我有用:
纱线运行皮棉--修复
或者
npm 运行 lint -- --fix
我用的是git+vscode+windows+vue,看了eslint文档后:https://eslint.org/docs/rules/linebreak-style
最后通过以下方式修复它:
将 *.js text eol=lf
添加到 .gitattributes
然后运行 vue-cli-service lint --fix
修复:我的 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"
}
在 .eslintrc 文件中添加以下内容:
扩展:['prettier'] 和插件:['prettier']
规则:{'prettier/prettier': ['error', {endOfLine: 'auto'}]}
在 .prettierrc 中删除:
endOfLine: '自动'
这个对我有用。
将此添加到您的 .prettierrc 文件并打开 VSCODE
"endOfLine": "auto"
在 .eslintrc 文件中添加以下规则,然后重新启动您的项目。
rules: {
'prettier/prettier': ['error', { "endOfLine": "auto"}, { usePrettierrc: true }],
}
我在这里尝试了一切,对我来说,我需要通过图标扩展>更漂亮>小引擎>扩展设置>更漂亮:行尾>设置为自动来管理更漂亮的配置扩展。
在我的 settings.json 中添加这两行之后
"eslint.run": "onSave",
"editor.formatOnSave": true,
我能够在 .eslintrc.js 规则中使用下面的配置。
"prettier/prettier": ["error", {
"endOfLine":"auto"
}],
检查底部 VS Code 状态栏的右侧,它显示行和列、空格、文本编码(UTF-8 等)等信息。您应该会看到一个 Select End Of Line Sequence
状态显示(LF 或 CRLF),您可以点击它进行更改。确保您没有手动将其更改为与您希望 Prettier 使用的内容相冲突的内容。
我在我的 Nest js 应用程序中遇到了同样的问题。将以下代码添加到 .eslintrc.js
rules 然后运行 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',
},
],
},
npm 运行 lint -- --fix
运行这个对我有用的命令
如您所见,将其添加到 .eslintrc 作品中!
https://i.stack.imgur.com/i633N.png
https://i.stack.imgur.com/lxxuv.png
git config --global core.autocrlf input
再次签出代码并再次打开 Visual Studio Code 并再次运行您的脚本。它对我有用。
input
?这个建议与 koko-js478 的建议有何不同?
我升级到“更漂亮”:“^2.2.0”并且错误消失了
在根目录中打开 .editorconfig 文件并更改:
end_of_line = lf
至
end_of_line = auto
这应该为新文件修复它。
对我有用的是:
按照 Roberto LL 的建议,将 prettier 更新到 2.2.1 版(目前的最新版本)。要做到这一点,请执行
npm 更新更漂亮
按照 Hakan 的建议执行 lint fix(这将修改项目中的所有文件以将行尾转换为 LF)。
npm 运行 lint -- --fix
没有必要更改 .eslintrc 和 .prettierrc 文件!
解决方案
1.禁用自动转换git设置
git --global config core.autocrlf false
2.删除旧缓存数据
git rm --cached -r 。
3.重置git文件
git reset --hard
当我从 git 中提取代码时出现错误,这对我有用:
步骤1:
git config --global core.autocrlf false
第2步:
删除当前文件夹
第 3 步:
再次从 git 拉取代码
再次尝试运行命令
--global
标志在本地为该存储库设置标志。此外,您不需要删除整个 repo 并再次克隆它,只需将分支重置为 origin 即可。
编辑您的 .eslintrc.json 文件并更新如下所示的“prettier/prettier”值。
我面临同样的问题并使用以下更改进行修复。
"prettier/prettier": [
"error", {
"singleQuote": true,
"parser": "flow"
}
],
没有必要忽略 linter 的规则。只是自动修复它
npm install -g prettier
prettier -w .\webpack.config.js # or other file
以上所有答案都是正确的,但是当我使用 windows 并禁用 Prettier ESLint 扩展 rvest.vs-code-prettier-eslint
时,问题将得到解决。
如果您已经签出代码
git config --global core.autocrlf input
git rm --cached -r .
git reset --hard
如果您需要与 .prettierrc.js 一起使用:
module.exports = {
...[config params],
endOfLine: 'auto',
};
笔记!不在 rules
或 prettier/prettier
中。
此处的信息https://prettier.io/docs/en/options.html#end-of-line
不定期副业成功案例分享
.eslintrc
文件对我有用,但不是.prettierrc
文件。不知道为什么或有什么区别(我对 OP 上的所有标签都是新手)。CRLF
更改为LF
对我有用frontend
) 中的.eslintrc.json
。更改后将如下所示:{ "extends": ["react-app", "prettier"], "plugins": ["prettier"], "rules": { "prettier/prettier": ["error", { "endOfLine": "auto" }] } }