"parserOptions.project"已设置为@typescript-eslint/parser。

8 浏览
0 Comments

"parserOptions.project"已设置为@typescript-eslint/parser。

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

我删除了作为脚手架的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

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

admin 更改状态以发布 2023年5月24日
0
0 Comments

您可以创建一个单独的TypeScript配置文件(tsconfig.eslint.json)用于eslint配置。该文件扩展了tsconfig配置,并为需要逐行检查的文件设置了include键。

.eslint.js:

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

tsconfig.eslint.json:

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

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

.eslintignore:

// ...
babel.config.js

0
0 Comments

JavaScript和TypeScript文件的不同的Lint规则

出现问题的原因可以是以下之一:

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

根据TypeScript ESLint解析器文档

parserOptions.project

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

(...)

请注意,如果已指定此设置,而未指定createDefaultProgram,则您必须仅检查由提供的tsconfig.json文件定义的项目中包括的文件。如果您现有的配置没有包含所有要检查的文件,则可以创建单独的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配置的内容:overrides如何工作?


不要检查特定的文件

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

babel.config.js

在这种情况下,上面提到的步骤(关于为TypeScript文件覆盖配置)非常重要,因为您的项目可能包含想要检查的JavaScript和TypeScript文件。

您还可以为不同的情况创建其他overrides,例如测试文件的不同配置,因为可以使用开发者依赖项并在node 环境中运行,而不是在browser中运行。

0