2

我使用 Create React Apps 的 Typescript 模板创建了一个 React 项目,为 ESLint 添加了必要的插件并将6.8.0ESLint 和 prettier 配置在一起,但是每当我编辑.ts.tsx文件时,我都会收到 ESLint 错误Delete ␍⏎␍⏎``

我在 VSCode 中安装了 ESLint 和 Prettier 扩展

我检查了 SO 上的其他各种帖子,并尝试了提到的大部分设置,

我将此添加到我的.eslintrc.json文件中

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

这是我的.prettierrc

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

但是,当我在 .ts/.tsx 文件中创建新行时,我仍然会收到一个 lint 错误

线路错误

vscode中的eslint问题

我将 VSCode 设置中的所有内容更改为使用CRLF(我在 Windows 上)"files.eol": "\r\n",

即使我尝试使用不同的行尾,我也会遇到类似的错误。

如果我做

"prettier/prettier": [
    "error",
    {
        "endOfLine": "lf"
    },
    { "usePrettierrc": true }
],

欧尔夫

如果我将endOfLine : crlf其设置为与 auto 相同的错误!

因为它在这里的价值是我的全部.eslintrc.json

{
    "env": {
        "browser": true,
        "es6": true,
        "jest": true
    },
    "extends": [
        "standard",
        "plugin:react/recommended",
        "plugin:@typescript-eslint/recommended",
        "prettier/@typescript-eslint",
        "plugin:prettier/recommended",
        "plugin:jsx-a11y/recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly",
        "__DEV__": "readonly"
    },
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "project": "tsconfig.json",
        "tsconfigRootDir": "."
    },
    "plugins": ["react", "react-hooks", "@typescript-eslint", "prettier"],
    "rules": {
        "camelcase": "off",
        "no-unused-expressions": "off",
        "react/prop-types": "off",
        "react/jsx-one-expression-per-line": "off",
        "react-hooks/rules-of-hooks": "error",
        "react-hooks/exhaustive-deps": "warn",
        "react/jsx-filename-extension": [
            1,
            {
                "extensions": [".tsx"]
            }
        ],
        "@typescript-eslint/no-unused-vars": [
            "error",
            {
                "argsIgnorePattern": "_"
            }
        ],
        "@typescript-eslint/explicit-function-return-type": [
            "error",
            {
                "allowExpressions": true
            }
        ],

        // Remove after
        "@typescript-eslint/no-empty-interface": "off",
        "jsx-a11y/no-static-element-interactions": "off",
        "jsx-a11y/click-events-have-key-events": "off",
        "prettier/prettier": [
            "error",
            {
                "endOfLine": "crlf"
            },
            { "usePrettierrc": true }
        ],

        // Remove After

        "jsx-quotes": "warn",
        "import/prefer-default-export": "off",
        "import/extensions": [
            "error",
            "ignorePackages",
            {
                "ts": "never",
                "tsx": "never"
            }
        ]
    },
    "settings": {
        "import/resolver": {
            "typescript": {}
        },
        "react": {
            "version": "detect"
        }
    }
}
4

2 回答 2

14

由于这篇文章获得了一些流量,我通过将此规则添加到我的 eslint 配置来解决这个问题

rules: {
    'prettier/prettier': ['off', { singleQuote: true }],
}

这里的关键部分是'off'当您将其设置为'error'默认情况下会显示此错误。将其关闭会禁用该检查。

于 2021-02-17T18:30:15.267 回答
1

您还可以更改行尾的 VS 代码设置:

  • 按 F1
  • 选择“更改行尾序列”
  • 选择“LF”而不是“CRLF”

瞧!

于 2021-12-08T15:43:25.287 回答