173

我创建了一个新的 React Native 项目--template typescript

我删除了template作为样板文件一部分的目录。

然后我继续添加 ESLint:

module.exports = {
  parser: "@typescript-eslint/parser",
  plugins: ["@typescript-eslint"],
  extends: ["airbnb-typescript-prettier"]
};

但是,当我打开时babel.config.js,我收到此错误

解析错误:“parserOptions.project”已为@typescript-eslint/parser 设置。

该文件与您的项目配置不匹配:/Users/Dan/site/babel.config.js.

该文件必须至少包含在提供的项目之一中。eslint

4

20 回答 20

116

JavaScript 和 TypeScript 文件的不同 lint 规则

出现此问题的原因之一如下:

  1. 您使用的规则需要类型信息且未指定parserOptions.project;
  2. 您指定parserOptions.project,没有指定createDefaultProgram它将在未来的版本中删除),并且您正在检查项目中不包含的文件(例如babel.config.jsmetro.config.js

TypeScript ESLint Parser 文档

parserOptions.project

此选项允许您提供项目的tsconfig.json. 如果要使用需要类型信息的规则,则需要此设置。

(...)

请注意,如果此设置已指定createDefaultProgram但未指定,则您只能对项目中包含的文件进行 lint,这些文件由提供的tsconfig.json文件定义。如果您现有的配置不包括您想要 lint 的所有文件,您可以创建一个单独的tsconfig.eslint.json.

要解决它,请更新您的 ESLint 配置以仅在 TypeScript 文件上使用 TypeScript 规则:

{
  // ...
  parser: '@typescript-eslint/parser',
  plugins: ["@typescript-eslint"],
  overrides: [
    {
      files: ['*.ts', '*.tsx'], // Your TypeScript files extension

      // As mentioned in the comments, you should extend TypeScript plugins here,
      // instead of extending them outside the `overrides`.
      // If you don't want to extend any rules, you don't need an `extends` attribute.
      extends: [
        'plugin:@typescript-eslint/recommended',
        'plugin:@typescript-eslint/recommended-requiring-type-checking',
      ],

      parserOptions: {
        project: ['./tsconfig.json'], // Specify it only for TypeScript files
      },
    },
  ],
  // ...
}

overrides您可以在官方文档中阅读有关配置的更多信息:覆盖如何工作?


不要对特定文件进行 lint

如果您不想检查错误中提到的文件(例如babel.config.js),您可以忽略它,将其名称添加到文件.eslintignore

babel.config.js

无论如何,如果您的项目同时包含您想要 lint 的 JavaScript 和 TypeScript 文件,上述步骤(关于覆盖 TypeScript 文件的配置)很重要。

您还可以为不同的情况创建其他覆盖,例如测试文件的不同配置,因为它可以使用开发人员依赖项并在node environment上运行,而不是browser.

于 2020-10-22T18:32:18.010 回答
81

您可以创建一个单独tsconfig.eslint.json的用于eslint配置的 TypeScript 配置文件 ()。该文件扩展了必须检查的文件的tsconfig配置和设置include键。

.eslint.js

// ...
parserOptions: {
  // ...
  project: "./tsconfig.eslint.json",
  // ...
},
// ...

tsconfig.eslint.json

{
  "extends": "./tsconfig.json",
  "include": [
    // ...
    "babel.config.js"
  ]
}

或者如果你想忽略它,你可以把它放到.eslintignore.

.eslintignore

// ...
babel.config.js
于 2020-03-24T22:09:45.343 回答
40

在“.eslintignore”中添加一行:

.eslintrc.js

于 2021-02-18T13:22:18.843 回答
24

您需要将该文件添加到您的 tsconfiginclude数组中。有关更多详细信息,请参阅typescript-eslint/typescript-eslint#967

于 2019-10-25T13:17:52.260 回答
22

在我的 ESLint 配置中,我有以下配置:

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
  },
  globals: {
    Promise: "readonly"
  },
  parser: "@typescript-eslint/parser",
  parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"],
  },
  plugins: ["@typescript-eslint"],
  extends: [
    "eslint:recommended",
    "prettier/@typescript-eslint",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
  ],
}

更新 修复的关键部分是:

parser: "@typescript-eslint/parser"

和这个:

parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"], // could be tsconfig.json too
}

不要忘记将该文件或文件的目录包含在includetsconfig.json 的数组中。

于 2020-06-28T01:42:50.433 回答
15
  1. npm i -D @types/node

  2. 包括对文件的打字稿支持*.config.js

tsconfig.json

{
  "include": [
    "**/*.config.js" // for *.config.js files
  ]
}

然后重新加载你的 IDE!

于 2020-09-05T08:15:07.427 回答
6

该错误意味着有一个可以编译但不属于当前定义的项目结构的文件。有几种解决方案:

如果您不关心文件是否已编译(即配置文件等)

  • 将文件名添加到.eslintignore

或者

  • 将文件或文件模式添加到.eslintrc.js文件
ignorePatterns: ["babel.config.js"]
// or 
ignorePatterns: ["**/*.config.js"]

您希望文件编译

将文件、文件夹或模式添加到tsconfig.json文件

include": [
    "src",
    "other_src",
]

注意某些更改需要重启 IDE 才能生效

于 2021-10-15T19:27:22.313 回答
6

只需eslintignorePatterns选项添加到您的.eslintrc.js

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.json',
  },
  ignorePatterns: ["babel.config.js"],
  ...

由于 linting 项目配置文件没有太大价值,您可以放心地忽略它们。

此外,我不会让它们成为您的一部分tsconfig.json或创建一个特殊tsconfig.eslint.json文件,只是为了 linting 目的。

于 2021-07-11T13:34:39.210 回答
4

对我来说,问题源于tsconfig使用该exclude属性时忽略的某些文件,但eslint并未忽略它们。

通常,如果希望 TSC 忽略文件,那么同样适用于 eslint。所以只需将配置中的值复制粘贴到exclude配置.tsconfig中的ignorePatterns属性中.eslintrc

于 2021-03-29T11:55:32.437 回答
2

添加'@typescript-eslint/prefer-nullish-coalescing': 'error'.eslintrc.js.

经过大量的试验和错误,我想出了这个解决方案,在我的情况下效果很好:

module.exports = {
  ...
  overrides: [
    {
      files: ['*.ts', '*.tsx'],
      parserOptions: {
        // this setting is required to use rules that require type information
        project: './tsconfig.json',
      },
      rules: {
        '@typescript-eslint/prefer-nullish-coalescing': 'error',
      },
    },
  ],
}
于 2021-07-11T02:55:37.440 回答
2

我使用从我的项目文件夹指向我的主文件夹的符号链接:

/opt/project/workspace=>~/worspace

使用符号链接路径打开 VSCode ~/workspace,错误始终存在。

使用原始路径打开VSCode:/opt/project/workspace解决问题。

这不应该是一个问题,但现在它是。

于 2020-06-20T21:12:27.317 回答
1

我只需要tsconfig.jsonproject数组和 eslint 脚本中添加新的路径,问题就消失了。

tsconfig.json

project: ["./newfolder/tsconfig.json"]

package.json

"eslint": "eslint -c ./.eslintrc.js './newfolder/**/*.ts'"
于 2022-01-04T10:33:05.827 回答
1

在我们的例子中,我们的目录树中有多个 .eslintrc 和多个 tsconfig.json。(服务器.各一个,React 客户端各一个./client。)

这适用于 CLI,但不适用于 VS Code 的插件。

修复方法是将 ./client/.eslintrcproject值设置为相对于根目录- 而不是相对于 .eslintrc 文件。将其从“./tsconfig.json”更改为“./client/tsconfig.json”后,IDE linting 按预期工作。

于 2020-08-28T19:52:39.610 回答
1

此错误是否来自 VSCode 扩展?即使错误位于同一文件夹(或子文件夹)中(如下面的错误消息所示),错误是否有一个指向顶层的相对路径?

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: ../../../../../home/<ommitted>/projects/floor-planner/app/src/App.vue.
The file must be included in at least one of the projects provided.

如果是这样,请检查它在使用 CLI 时是否仍然存在。

从您的项目根目录,您可以运行eslint .. 如果没有找到这些错误,您很可能通过 symlink打开了项目。

自 2021 年 8 月起,Eslint 扩展不适用于符号链接。

从您的项目目录中,运行pwd -P以获取绝对路径。

在我的例子中,这导致

~/projects/floor-planner/app: pwd -P
/media/projects/2021-Q3/floor-planner/app

通过此路径打开项目。

Eslint 不应再显示错误。

于 2021-08-26T13:35:12.207 回答
1

只需在 .eslintrc.js 文件中添加以下代码。

ignorePatterns: ['.eslintrc.js']
于 2022-01-17T10:30:56.800 回答
1

当我在文件夹结构中打开我的项目太高时,我在 VsCode 中遇到了这个问题。一个奇怪的解决方案,但它奏效了。

在我的示例中,Firestore 函数项目 for express in .ts,我必须从它创建的函数文件夹中打开它。

我应该注意到这是这种问题,因为“npm run-script lint”本身从未返回错误。

于 2020-11-02T10:15:50.240 回答
0

我今天遇到了这个问题,以上解决方案都没有帮助。我遇到的问题是我在同一个目录中有两个文件共享相同的文件名(无扩展名)。例如,src/foo.tssrc/Foo.tsx。重命名其中一个文件修复了 linter 错误。请参阅@typescript-eslint/parser#955

于 2021-12-29T00:28:03.283 回答
0

我花了很多时间来解决这样的问题。

我创建了一个jest.config.ts而不是jest.config.js所以它抛出了这个确切的 eslint 错误

于 2021-11-20T18:18:25.110 回答
0

我在 PHPStorm 中的问题是我设置了工作主管:

在此处输入图像描述

我清除了,一切正常:\

于 2022-01-24T16:02:06.080 回答
-1

在 .eslintrc 中设置parserOptions如下

"parserOptions": {
  "ecmaFeatures": {
    "jsx": true
  },
  "ecmaVersion": 2019,
  "sourceType": "module"        
},
于 2021-09-01T15:33:22.863 回答