为什么我总是收到 "[eslint] 删除 `CR` [prettier/prettier]" 的提示?

700
我正在使用带有Prettier 1.7.2和ESLint 1.7.0的VS Code。 每次换行后我都会遇到以下问题:
[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,
}

如何解决这个错误?


13
请查看您的.eslintrc.js文件。从扩展数组中删除“ 'plugin:prettier/recommended'”应该可以解决问题。 - Sanu Soman
4
您可以尝试禁用 ESLint VSCode 扩展。 - Amitesh
42个回答

15
在 .eslintrc 文件中添加以下内容:
  • extends: ['prettier']plugins: ['prettier']

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

在 .prettierrc 中删除以下内容:
  • endOfLine: 'auto'
对我来说有效。

13

修正:我的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"
}

12
将以下内容添加到您的.prettierrc文件中,并打开VSCODE
"endOfLine": "auto"

11

我正在使用git+vscode+windows+vue,阅读了eslint文档:https://eslint.org/docs/rules/linebreak-style

最终通过以下方式解决:

.gitattributes文件中添加*.js text eol=lf,然后运行vue-cli-service lint --fix


11

运行命令 npm run lint -- --fix

我用这个命令解决了问题。


对我也一样,谢谢! - Mojgan

11
将以下代码行添加到".eslintrc.js"文件中。
rules: {'prettier/prettier': ['error', {endOfLine: 'auto'}]}

e.g enter image description here


10
将以下规则添加到.eslintrc文件中,然后重新启动您的项目。
rules: {
    'prettier/prettier': ['error', { "endOfLine": "auto"}, { usePrettierrc: true }],  
}

7

我在我的NestJS应用程序中遇到了同样的问题。将以下代码添加到.eslintrc.js规则中,然后运行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',
  },
],

},


7

解决方案

1. 关闭自动转换git设置

git --global config core.autocrlf false

2. 删除旧的缓存数据

git rm --cached -r .

3. 重置git文件

git reset --hard


7

如果您已经检查过代码

git config --global core.autocrlf input 


git rm --cached -r . 


git reset --hard

这将删除您本地的git更改,请小心执行这些命令。 - Santosh Karanam
在Windows上对我有用。 - Ian Samz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接