简体   繁体   English

eslint 在没有错误的地方抛出错误

[英]eslint throwing errors where there are no errors

I'm receiving the following errors when running yarn 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.

This is my .eslintrc.js file:这是我的.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'
        ]
    }
};

… and the file in question: …以及有问题的文件:

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);
}

Note: I include \t here to denote tabs as Stack Overflow replaces actual tabs with 4 spaces.注意:我在这里包含\t来表示选项卡,因为 Stack Overflow 用 4 个空格替换了实际的选项卡。

None of the errors reported by eslint are actual errors in the file. eslint 报告的错误都不是文件中的实际错误。 All indents are correct, and I only use single quotes.所有缩进都是正确的,我只使用单引号。 Hell, there isn't even a line 43 in that file, as eslint is reporting.见鬼,该文件中甚至没有第 43 行,因为 eslint 正在报告。

What might be happening here?这里可能会发生什么?

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

This rule in you eslint tell that you shall not use space but a tab Key for indentation either remove this rule OR on the lines where the INDENT error has occured you shall find space used for indentation remove them and use the Tab key Also for the Quote Error check you variable for the string values saved using single quote, if you are using vscode then you might need to remove this rule or update the vscode setting and prevent it from updating the single quote to double quote while auto saving eslint 中的这条规则告诉您不应使用空格,而是使用制表键进行缩进,或者删除此规则,或者在发生 INDENT 错误的行上,您将找到用于缩进的空间,删除它们并使用 Tab 键也用于引用错误检查您使用单引号保存的字符串值的变量,如果您使用的是 vscode,那么您可能需要删除此规则或更新 vscode 设置并防止它在自动保存时将单引号更新为双引号

facepalm.掌心。

This ended up being a problem with an aspect of the project I didn't mention.这最终成为我没有提到的项目的一个方面的问题。 I'm using Rollup to bundle by code, along with the @rollup/plugin-eslint plugin to run eslint when building.我正在使用Rollup通过代码捆绑,以及@rollup/plugin-eslint插件在构建时运行 eslint。 I had my plugins in this order:我按以下顺序安装了我的插件:

plugins: [
    babel({
        babelHelpers: 'bundled',
        exclude: 'node_modules/**',
    }),
    resolve({
        browser: true,
    }),
    eslint({
        include: '**/*.js',
        throwOnError: true,
    }),
    styles({
        modules: true,
    }),
]

I just had to move the eslint() plugin to the front of the array and all was golden.我只需要将eslint()插件移到数组的前面,一切都是金色的。

plugins: [
    eslint({
        include: '**/*.js',
        throwOnError: true,
    }),
    babel({
        babelHelpers: 'bundled',
        exclude: 'node_modules/**',
    }),
    resolve({
        browser: true,
    }),
    styles({
        modules: true,
    }),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM