How fix 'Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.' inside gatsby-config and Gatsby-node.js

How fix 'Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.' inside gatsby-config and Gatsby-node.js

在我的 eslintrc.js 我有 a.o:

extends: [
    'plugin:react/recommended',
    'airbnb',
    'airbnb/hooks',
    'plugin:jsx-a11y/recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'plugin:prettier/recommended',
  ],
  globals: {
    __PATH_PREFIX__: true,
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
    project: './tsconfig.json',
    ecmaFeatures: {
      jsx: true,
      typescript: true,
      tsx: true,
    },
  },

在我的 Gatsby 项目的根目录中,我有 gatsby-config.jsgatsby-config.js。在文件的开头,我得到一个 ESLint linting 错误:

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

我该如何解决这个问题?

您需要更新 .tsconfig 中的 include 数组以包含您想要 lint 的所有文件,正如您在 the docs:

中看到的那样

Note that if this setting is specified and createDefaultProgram is not, you must only lint files that are included in the projects as defined by the provided tsconfig.json files. If your existing configuration does not include all of the files you would like to lint, you can create a separate tsconfig.eslint.json as follows:

{
  // extend your base config so you don't have to redefine your compilerOptions
  "extends": "./tsconfig.json",
  "include": [
    "src/**/*.ts",
    "test/**/*.ts",
    "typings/**/*.ts",
    // etc

    // if you have a mixed JS/TS codebase, don't forget to include your JS files
    "src/**/*.js"
  ]
}

对于您的情况,您可以创建自定义 (tsconfig.eslint.json) 文件或在现有文件中添加 include 规则以匹配所有需要的文件。