为@typescript-eslint/parser 设置了“parserOptions.project”

Posted

技术标签:

【中文标题】为@typescript-eslint/parser 设置了“parserOptions.project”【英文标题】:"parserOptions.project" has been set for @typescript-eslint/parser 【发布时间】:2020-02-18 23:01:12 【问题描述】:

我用--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

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

【问题讨论】:

tsconfig.js文件中添加babel.config.js: "include": [ "next-env.d.ts", "*/.ts", "*/.tsx”、“postcss.config.js”、“.eslintrc.js”] @HimanshuTanwar 不需要包含babel.config.js,只是不要用 ESLint TypeScript 解析器解析它。看看my answer 的另一种方法,只解析 TS 文件。 【参考方案1】:

javascript 和 TypeScript 文件的不同 lint 规则

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

    您使用的规则需要 type 信息但未指定 parserOptions.project; 您指定了parserOptions.project,未指定createDefaultProgram (it will be removed in a future version),并且您正在检查项目中未包含的文件(例如babel.config.jsmetro.config.js

来自TypeScript ESLint Parser docs:

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 配置的更多信息:How do overrides work?


不要对特定文件进行 lint

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

babel.config.js

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

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

【讨论】:

这基本上是解决方案,完美运行,解释证明解决方案。 从 cmets 中很清楚,但有人有兴趣了解 eslint eslint.org/docs/user-guide/… 的 overrides 属性 这在第二种情况下不起作用,因为它仍然尝试将需要类型信息的规则应用于 .js 文件,例如babel.config.js。还需要将需要类型的规则添加到此覆盖子句中。所以,例如: files: ['*.ts'], extends: [ 'plugin:@typescript-eslint/recommended-requiring-type-checking', ], parserOptions: project: ['./tsconfig.json'], , 将这些文件放在.eslintignore 中不是最干净的解决方案吗? Linting 配置文件并没有真正增加好处。并且以使用overrides 的方式重写 ESLint 配置以仅获取所需的文件感觉很奇怪——它不只是模拟“忽略”但更复杂吗? @bluenote10 我同时使用overrides.rulesrules,因为我需要以不同的方式对不同的文件进行 lint。例如,rules 包含我想应用于所有文件的规则(.ts.js),而我有 2 个不同的 overrides 来应用 TypeScript 规则(@typescript-eslint-)和一些特定规则测试文件(例如在import/no-extraneous-dependencies 规则中允许devDependencies)。我不想忽略文件,只是根据需要有不同的规则。【参考方案2】:

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

.eslint.js:

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

tsconfig.eslint.json:


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

或者如果你想忽略它,你可以将它放入.eslintignore

.eslintignore:

// ...
babel.config.js

【讨论】:

你不需要包含那个文件,只是不要解析它。看看my answer,我解释了它并分享了一个代码来解决这个问题:) @RafaelTavares 但如果我想 lint 配置? @godblessstrawberry 你不应该用 TypeScript linting 对 .js 进行 lint。只需使用 JS ESLint 规则即可。 @RafaelTavares 怎么样? 不起作用。我创建了一个 tsconfig.eslint.json,从 tsconfig.json 扩展并添加了我希望包含的文件,但我仍然遇到同样的错误。【参考方案3】:

在“.eslintignore”中添加一行:

.eslintrc.js

【讨论】:

我不得不忽略另一个文件,它抱怨的那个文件,一些 html 文件,但这种方法有效 +1【参考方案4】:

您需要将该文件添加到您的 tsconfig include 数组中。 详情请见typescript-eslint/typescript-eslint#967。

【讨论】:

但是如果我希望它被忽略怎么办?如果我将它添加到排除数组中,它仍然显示.. 我认为关键在于 typescript+eslint 依赖于 @typescript-eslint 解析器,因此明确要求 ts 可以看到 linted 文件。老实说,我不确定这个用例。为什么需要排除它?我相信 typescript-eslint 的理念是基于这样一个假设,即所有 linted 文件都应该是项目的一部分,并且作为项目一部分的所有文件都应该被 lint(即使它们不是生产代码的关键入口点) - 例如,如果在这种情况下您指定了错误的 babel 配置,则需要类型错误是有意义的。 可能会有更多有用的上下文,我也应该看看here 你不需要包含那个文件,只是不要解析它。看看my answer,我解释了它并分享了一个代码来解决这个问题:) 我将以下内容添加到 tsconfig.json 并解决了问题。 "include": ["./src/**/*ts", "./src/**/*.tsx"], // .eslintrc 的一部分 `` parser: '@typescript-eslint/parser' , parserOptions: project: './tsconfig.json', sourceType: 'module', createDefaultProgram: true, , ``【参考方案5】:

在我的 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

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

【讨论】:

如果您正在检查项目中不包含的文件(例如 babel.config.js、metro.config.js),这将不起作用,如果您遇到类似 The file must be included in at least one of the projects provided.eslint 的错误那么就是这样。【参考方案6】:

    运行npm i -D @types/node

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

tsconfig.json:


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

然后重新加载您的 IDE!

【讨论】:

对我来说,只需添加 .config 即可解决问题。最终修复版本 --> ` "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.config.js" ],` 就我而言,添加"**/*.eslintrc.js" 并重新启动IDE 修复了它。谢谢。【参考方案7】:

只需通过将ignorePatterns 选项添加到您的.eslintrc.js 来指示eslint 忽略它们:

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

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

另外,我不会让它们成为您的 tsconfig.json 的一部分,也不会创建一个特殊的 tsconfig.eslint.json 文件,仅用于 linting 目的。

【讨论】:

【参考方案8】:

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

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

将文件名添加到.eslintignore

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

你想编译文件

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

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

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

【讨论】:

【参考方案9】:

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

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

【讨论】:

【参考方案10】:

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

/opt/project/workspace => ~/worspace

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

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

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

【讨论】:

【参考方案11】:

我在将'@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',
      ,
    ,
  ],

【讨论】:

谢谢,解决了我的问题:?? You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.【参考方案12】:

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

这在 CLI 中运行良好,但不适用于 VS Code 的插件。

解决方法是将 ./client/.eslintrc project 值设置为相对于 root - 而不是相对于 .eslintrc 文件。将其从“./tsconfig.json”更改为“./client/tsconfig.json”后,IDE linting 将按预期工作。

【讨论】:

【参考方案13】:

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

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

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

【讨论】:

【参考方案14】:

此错误是否来自 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 .。 如果没有发现这些错误,您很可能是通过符号链接打开了项目。

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

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

在我的例子中,这会导致

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

通过此路径打开项目。

Eslint 应该不再显示错误。

【讨论】:

在多模块项目的 Intellj 中遇到此问题并使用 Fedora Silverblue 的人,事实证明主文件夹实际上是一个符号链接。 /home/foobar/var/home/foobar 的符号链接。如果您通过~/var/home 在 Intellj 中打开项目文件夹,它似乎可以正常工作。【参考方案15】:

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

tsconfig.json:

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

package.json:

"eslint": "eslint -c ./.eslintrc.js './newfolder/**/*.ts'"

【讨论】:

【参考方案16】:

我在这样一个问题上花了很多时间。

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

【讨论】:

【参考方案17】:

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

【讨论】:

【参考方案18】:

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

ignorePatterns: ['.eslintrc.js']

【讨论】:

【参考方案19】:

我在 phpStorm 中遇到的问题是我设置了工作主管:

我清除了,一切正常:\

【讨论】:

【参考方案20】:

在.eslintrc中设置parserOptions如下

"parserOptions": 
  "ecmaFeatures": 
    "jsx": true
  ,
  "ecmaVersion": 2019,
  "sourceType": "module"        
,

【讨论】:

以上是关于为@typescript-eslint/parser 设置了“parserOptions.project”的主要内容,如果未能解决你的问题,请参考以下文章

考试错题

SQL判断字段是不是为空,为NULL

2021-11-26:() 分值为2, (()) 分值为3, ((())) 分值为4, 也就是说,每包裹一层,分数就是里面的分值+1。 ()() 分值为2 * 2, (())() 分值为3 * 2。(

java怎么判断对象为null

码农八荣八耻

pandas把dataframe的数据列转化为索引列实战:单列转化为索引多列转化为复合索引