首页 > 解决方案 > 为什么@typescript-eslint/parser 包含在我的 tsconfig.json 中配置的文件之外的文件?

问题描述

我收到错误:-

Parsing error: "parserOptions.project" has been set for u/typescript-eslint/parser.

The file does not match your project config: .eslintrc.js.

The file must be included in at least one of the projects provided.

IDE - WebStorm 20.1.1

项目结构:-

node_modules

src

--- testfile.js

.eslintrc.js

.prettierrc.js

baseeslint.js

package.json

tsconfig.json

.eslintrc.js:-

module.exports = {
extends: ['./baseeslint'],
parserOptions: {
project: './tsconfig.json'
},
rules: {}
}

baseeslint.js:-

module.exports = {

parser: '@typescript-eslint/parser'

}

tsconfig.json: -

{

"include": [

"/src/**/*.ts",

"/src/**/*.tsx"

],

"exclude": ["node_modules"],

"compilerOptions": {

"outDir": "./lib",

"types": ["styled-components", "react", "@types/react-router-dom", "reactstrap"]

}

}

我完全不知道为什么会这样?我会假设它会忽略根目录中的文件,尤其是在我的 tsconfg.json 中指定的包含?

任何帮助将不胜感激。

编辑: -

我的解决方案,它应该帮助别人......

我将 ignorePatterns 配置添加到我的 .eslintrc.js 文件中:-

module.exports = {
  extends: ["mycustom/eslint-config"],
  parserOptions: {
    project: './tsconfig.json'
  },
  ignorePatterns: ["/*.*"],
  rules: {}
}

这也可以通过 .eslintignore 来完成(请参阅下面的 Anton Bessonov 的回复)。

与此相关的另一件事可能对其他人有用的是我使用的 VScode 配置(我在发布原始问题后从 WebStorm 切换到 VSCode)。我非常喜欢在保存时运行 eslint fix,但我不希望它尝试对所有文件执行此操作。我的项目文件(并且只有我的项目文件)都是 .ts/.tsx。settings.json 中的以下选项允许修复保存而不包括我的 repo 中的每个文件:-

{
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.probe": [
        "typescript",
        "typescriptreact"
    ],
}

如果未指定 eslint.probe,则默认为:-

["javascript", "javascriptreact", "typescript", "typescriptreact", "html", "vue"]

更多关于 VSCode ESLint 扩展设置可以在这里找到。

标签: typescriptvisual-studio-codeeslintvscode-settings

解决方案


为什么会发生这种情况?

我不完全确定,但似乎@typescript-eslint/parser试图将 eslint 配置中涵盖的文件(例如通过扩展名)与tsconfig.json. 由于这些.js文件被@typescript-eslint/parser覆盖,并且取决于您的 eslint 配置,因此该文件可能包含在 eslint 运行中。但是由于该文件未包含在 tsconfig 中,因此您会收到此错误。

如何解决?

根据您的情况,您可以选择其中之一:

  1. 你可以忽略它.eslintignore。这就是@U4EA 所做的。
  2. 如果可以将其添加到包含中tsconfig.json
    "include": [
        ".eslintrc.js",
        // another includes
    ]
  1. 如果您没有“正确”的 tsconfig.json,因为您使用的是 monorepo,那么您可以创建一个新文件tsconfig.eslint.json
{
    "include": [
        ".eslintrc.js"
    ]
}

并将此文件仅用于 eslint:

    parserOptions: {
        project: [
            './tsconfig.eslint.json',
            './packages/*/tsconfig.json',
        ],
    },

推荐阅读