首页 > 解决方案 > eslint 在没有错误的地方抛出错误

问题描述

我在运行时收到以下错误yarn eslint

/…/src/a.js
  4:1   error  Expected indentation of 1 tab but found 2 spaces   indent
  4:43  error  Strings must use singlequote                       quotes
  5:1   error  Expected indentation of 2 tabs but found 4 spaces  indent
  6:1   error  Expected indentation of 1 tab but found 2 spaces   indent
  6:6   error  Strings must use singlequote                       quotes

✖ 5 problems (5 errors, 0 warnings)
  5 errors and 0 warnings potentially fixable with the `--fix` option.

这是我的.eslintrc.js文件:

module.exports = {
    env: {
        browser: true,
        es2021: true,
    },
    extends: [
        'eslint:recommended',
        'plugin:react/recommended',
    ],
    parserOptions: {
        ecmaFeatures: { jsx: true },
        ecmaVersion: 12,
        sourceType: 'module',
    },
    plugins: ['react'],
    settings: {
        react: { version: 'detect' },
    },
    rules: {
        indent: [
            'error',
            'tab'
        ],
        'linebreak-style': [
            'error',
            'unix',
        ],
        quotes: [
            'error',
            'single'
        ],
        semi: [
            'error',
            'always'
        ]
    }
};

…以及有问题的文件:

import React from "react";
import { css } from "./a.css";

export function App() {
\treturn (
\t\t<div className={css.App}>
\t\t\tHello.
\t\t</div>
\t);
}

注意:我\t在这里包括以表示选项卡,因为 Stack Overflow 用 4 个空格替换了实际的选项卡。

eslint 报告的错误都不是文件中的实际错误。所有缩进都是正确的,我只使用单引号。见鬼,该文件中甚至没有第 43 行,因为 eslint 正在报告。

这里可能会发生什么?

标签: javascripteslint

解决方案


indent: [
        'error',
        'tab'
    ],
quotes: [
        'error',
        'single'
    ],

eslint 中的这条规则告诉您不应使用空格,而是使用制表键进行缩进,或者删除此规则,或者在发生 INDENT 错误的行上,您将找到用于缩进的空间,删除它们并使用 Tab 键也用于引用错误检查您使用单引号保存的字符串值的变量,如果您使用的是 vscode,那么您可能需要删除此规则或更新 vscode 设置并防止它在自动保存时将单引号更新为双引号


推荐阅读