ome*_*ega 28 node.js typescript eslint
在我的打字稿项目中,我使用的是 eslint。下面的文件在根目录中,我还有子文件夹/dist和/src.
eslintrc.js
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
rules: {
strict: 'error',
semi: ['error', 'always'],
'no-cond-assign': ['error', 'always'],
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
}
Run Code Online (Sandbox Code Playgroud)
配置文件
{
"compilerOptions": {
"outDir": "dist",
"noImplicitAny": true,
"moduleResolution": "Node",
"resolveJsonModule": true,
"module": "ES2020",
"target": "ES2020",
"lib": ["ES2020"],
"allowJs": false,
"alwaysStrict": true
},
"include": ["src"]
}
Run Code Online (Sandbox Code Playgroud)
module顶部的单词有一条带有此错误的红线
Parsing error: "parserOptions.project" has been set for @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. eslint
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
Aid*_*din 90
该错误基本上是说文件eslintrc.js 本身是:
所以,ESLint 不知道该怎么办并且吓坏了!
请注意,虽然此文件不包含您的代码,但您应该决定是否要对该文件进行 linted(例如制表符与空格,或双引号与单引号)。
您可以从上面的冲突情况中省略 (1) 或 (2)。
更容易了。只需将文件名(例如.eslintrc.js)添加到.eslintignore.
您还可以查看我的答案的案例 1.3,了解不同的实现方式。
tsconfig.jsonTypescript-ESLint 从via获取要包含的文件列表parserOptions > project。
因此,您可以将该文件添加到现有文件tsconfig.json(解决方案 2.1)中,也可以创建一个辅助文件tsconfig.eslint.json来包含这样的文件,您希望通过 typescript-eslint 对该文件进行 linting,但不一定由 Typescript 编译器进行处理。
解决方案2.1:将其添加到现有的tsconfig.json中
// in tsconfig.json ...
"include": [
".eslintrc.js",
// ...
]
Run Code Online (Sandbox Code Playgroud)
解决方案 2.2:将其添加到单独的tsconfig.eslint.json文件中
创建一个包含以下内容的文件名tsconfig.eslint.json:
// new file: tsconfig.eslint.json
{
"include": [
".eslintrc.js"
]
}
Run Code Online (Sandbox Code Playgroud)
然后将其注入到eslintrc.js's parserOptions> project(是的,project接受字符串和字符串数组):
// in eslintrc.js ...
parserOptions: {
project: [
resolve(__dirname, './tsconfig.json'),
resolve(__dirname, './tsconfig.eslint.json'),
],
}
Run Code Online (Sandbox Code Playgroud)
附言。这个答案非常相似,我应该给予一些信任。
Joe*_*son 54
更新您的内容eslintrc.js以包括:
忽略模式:['.eslintrc.js']
现在,这已记录在 TypeScript ESLint 的文档中,我收到错误消息,告诉我“该文件必须包含在至少一个提供的项目中”
TLDR:请参阅下面的“附加选项”,了解我认为的最佳解决方案。
这是带有添加注释的相关文档。这是建立在之前的答案的基础上的。
此错误意味着正在检查的文件未包含在您提供给我们的任何 tsconfig 文件中。很多时候,当用户拥有未包含在正常 tsconfig 中的测试文件或类似文件时,就会发生这种情况。
取决于您想要实现的目标:
- 如果您不想检查文件:
- 使用ESLint 提供的选项之一来忽略文件,例如 .eslintignore 文件或ignorePatterns 配置。=> 请参阅此答案的解决方案 1
- 如果您确实想检查文件:
- 如果您不想使用类型感知 linting 对文件进行 lint:
- 使用ESLint的
overrides配置将文件配置为不使用类型信息进行解析。
- 一种流行的设置是从顶级配置中省略上述添加内容,并仅通过覆盖将它们应用到 TypeScript 文件。=> 请参阅下面的附加选项
- 或者,您可以
parserOptions: { project: null }为要排除的文件添加覆盖。请注意,这{ project: undefined }将不起作用。=> 请参阅下面的减法选项- 如果您确实想使用类型感知 linting 对文件进行 lint:
- 检查您提供的每个 tsconfig 的包含选项
parserOptions.project- 您必须确保所有文件都与includeglob 匹配,否则我们的工具将无法找到它。 =>参见这个答案的解决方案2.1- 如果您的文件不应该是现有 tsconfig 之一的一部分(例如,它是存储库本地的脚本/工具),请考虑
tsconfig.eslint.json在项目根目录中创建一个新的 tsconfig (我们建议调用它),其中列出了此文件文件包含在其中。有关示例,您可以查看我们在此存储库中使用的配置: => 请参阅此答案的解决方案 2.2 或下面的示例
使用此解决方案,您只需添加(即启用)parser: '@typescript-eslint/parser'TypeScript 源文件的选项(以及其他相关选项)。其他文件(如)eslintrc.js不会受到该配置的影响,但仍会根据配置的其余部分进行检查。
将其添加到eslintrc.js
overrides: [
{
files: ['src/**/*.ts'],
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
rules: {},
},
],
Run Code Online (Sandbox Code Playgroud)
上面的配置仅对src目录中的文件使用 TypeScript ESLint。
请注意,所有 TypeScript ESLint 规则也需要位于条目中overrides(即不是根rules选项),否则您将收到如下错误消息:
Error: Error while loading rule '@typescript-eslint/dot-notation': 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.
Run Code Online (Sandbox Code Playgroud)
使用此解决方案,您可以删除(或) 配置文件parser: '@typescript-eslint/parser'的选项。eslintrc.jseslint.cjs
将其添加到eslintrc.js
overrides: [
{
files: ['./.eslintrc.js'],
parserOptions: { project: null },
rules: {},
},
],
Run Code Online (Sandbox Code Playgroud)
上述配置禁用该eslintrc.js文件的 TypeScript ESLint。您还可以添加其他文件或文件夹来override.files扩展此行为。
缺点:所有 TypeScript ESLint 规则也需要在条目中删除overrides,否则您将收到如下错误消息:
Error: Error while loading rule '@typescript-eslint/dot-notation': 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.
Run Code Online (Sandbox Code Playgroud)
小智 -2
同样的错误,这对我有用: https: //github.com/nestjs/nest/issues/4900#issuecomment-669743374 https://github.com/nestjs/nest/issues/4900#issuecomment-707330674
看起来配置文件必须命名为 .eslintrc 并使用 json 格式而不是 .eslintrc.js 并且 parserOptions.createDefaultProgram 必须设置为 true
| 归档时间: |
|
| 查看次数: |
15702 次 |
| 最近记录: |