gpt4 book ai didi

typescript - ESLint 在包含的 tsconfig 文件上抛出错误

转载 作者:行者123 更新时间:2023-12-05 05:44:40 29 4
gpt4 key购买 nike

我有以下存储库结构:

cypress 文件夹

  • .eslintrc.js
  • tsconfig.json
  • basic.spec.ts

src 文件夹

.eslintrc.js

tsconfig.base.json

tsconfig.json

我的意图是只为 src 文件夹设置根 tsconfig.json.eslintrc.js 也是如此。然后我尝试为 cypress 文件夹配置 tsconfig.json.eslintrc.js 。但是在运行 ESLint 时出现以下错误:

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

这里是配置文件的一些片段:

tsconfig.base.json:

{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": false,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"removeComments": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"alwaysStrict": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": "./",
"paths": {
"@/containers/*": ["src/components/containers/*"],
"@/layout/*": ["src/components/layout/*"],
"@/ui/*": ["src/components/ui/*"],
"@/utils/*": ["src/utils/*"],
"@/images/*": ["public/images/*"],
"@/models/*": ["src/models/*"],
"@/data/*": ["src/data/*"],
"@/hooks/*": ["src/hooks/*"]
},
"typeRoots": ["./node_modules/@types", "./@types"]
}
}

tsconfig.json 文件:

{
"extends": "./tsconfig.base.json",
"include": ["next-env.d.ts", "src/**/*.ts", "src/**/*.tsx", "tests/**/*.ts"],
"exclude": ["node_modules"]
}

.eslintrc.js 文件:

module.exports = {
root: true,
env: {
browser: true,
es2021: true,
},
extends: [
'eslint:recommended',
'next/core-web-vitals',
'plugin:@typescript-eslint/recommended',
'plugin:import/typescript',
'prettier',
],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'unused-imports', 'react-hooks', 'node'],
};

cypress/tsconfig.json 文件:

{
"extends": "../tsconfig.json",
"compilerOptions": {
"isolatedModules": false,
"types": ["cypress"]
},
"include": ["../node_modules/cypress", "./**/*.ts"],
"exclude": []
}

cypress/.eslintrc.js 文件:

module.exports = {
root: true,
env: {
es2021: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/typescript',
'prettier',
],
parserOptions: {
ecmaVersion: 12,
project: './tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint', 'unused-imports', 'node'],
};

所以我不明白为什么会出现错误,因为 basic.spec.ts 包含在 cypress/tsconfig.json 文件中。

最佳答案

所以问题是,ESLint 默认检测当前工作目录作为根文件夹。所以这导致 ESLint 检测根 tsconfig.json。通过在 cypress/.eslintrc.js 文件中解决问题:

parserOptions: {
ecmaVersion: 12,
project: './tsconfig.json',
tsconfigRootDir: 'cypress',
sourceType: 'module',
},

(也可以通过将项目值设置为 cypress/tsconfig.json 来解决)

更多引用: https://github.com/typescript-eslint/typescript-eslint/issues/4732

关于typescript - ESLint 在包含的 tsconfig 文件上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71550327/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com