Eslint/Tslint 配置帮助(不包括文件)

Eslint/Tslint Config help (excluding files)

最近在我们的项目中我们迁移到使用@typescript-eslint/parser从tslint慢慢迁移。一切都很顺利,但我有一个小 problem/question 我无法锻炼。

我是否需要在 tsconfig 排除数组以及 .eslintrc 导出对象上的 ignorePatterns 中指定忽略 files/patterns?有什么区别?

我们的 src/lang 目录中有一个 messages.js 文件,我试图忽略它,但目前在我们的预提交挂钩上抛出一个 lint 错误,这让我想知道这个问题和这两种设置如何协同工作。

Parsing error: "parserOptions.project" has been set for '@typescript-eslint/parser' The file does not match your project config: src/lang/messages.js. The file must be included in at least one of the projects provided

我认为我对这些交织的理解是错误的,因为当 eslint 运行时,我认为 parserOptions 会从 tsconfig 中获取项目规则,其中排除了 js 文件。

目前,我在我们的 eslintrc 中谈论的部分看起来像:

module.exports = {
    parser: '@typescript-eslint/parser',
    parserOptions: {
        project: path.resolve(__dirname, './tsconfig.json'),
        tsconfigRootDir: __dirname,
        useJSXTextNode: true,
        sourceType: 'module',
        ecmaFeatures: {
            modules: true,
            jsx: true,
        },
    },
    ignorePatterns: ['node_modules/**', '.storybook/**', 'src/stories/**', '*.scss', '*.js'] // ignoring here works

tsconfig:

"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "src/**/*.js"], // exclude here doesn't work.

package.json:

"scripts": {
    "lint": "tsc --project ./tsconfig.json --noEmit && eslint --ext=jsx,ts,tsx src"
},

ESLint 向 @typescript-eslint/parser 插件传递文件列表(ESLint 从命令行获取)。 @typescript-eslint/parser 插件无法控制此列表,因此它会尽力根据给定的列表进行操作。

错误消息试图通知用户 ESlint 正在检查一个被 @typescript-eslint/parser 插件排除在外的文件。从插件的角度来看,发生这种情况的原因有两个:

  1. 该文件被有意、明确​​地排除(通过将其添加到 tconfig exclude)。
  2. 用户忘记将文件添加到 tsconfig include.

在后一种情况下(更常见),插件想要通知用户他们的配置有误,以便他们可以更正。因此,您看到了错误消息。

为了从 TSLint 中正确排除文件,一种选择是使用 .eslintignore 文件。 你也可以更改 eslint 命令以忽略排除的文件:

eslint ... --ignore-pattern "src/**/*.js"

(但请注意,忽略模式是相对于 当前目录 ,而不是相对于 tsconfig 等的位置)

或者,如果您 希望 ESLint 对文件进行 lint(但仍然在 tsconfig 中排除它们),您可以考虑为 @typescript-eslint/parser 提供更具包容性的 tsconfig创建 tsconfig.eslint.json file that extends from your normal tsconfig

另见这些 GitHub 问题:

我能够通过使用 JS 配置文件 .eslintrc.js 并执行以下操作来使用 tsconfig 排除:

const tsConfig = require('./tsconfig.eslint.json');

module.exports = {
  ...
  ignorePatterns: tsConfig.exclude,
};