【发布时间】:2021-07-08 13:27:05
【问题描述】:
似乎每次我开始使用某种新库时,我都会遇到 ESLint 问题,我永远无法真正弄清楚原因。我跌跌撞撞直到错误和警告消失,然后再处理。我希望我能得到一些关于它应该如何在这里工作的答案。
我有一个使用 Typescript 的 React 项目。我们使用 react-testing-library 进行测试(使用 Jest 类型)以及 Cypress 进行测试(使用 Cypress 类型)。 Jest 和 Cypress 会发生冲突,因为它们使用了许多相同的关键字,但来自不同的库(即 describe、expect、context 等)。
此外,使用 cypress 可以定义您自己的函数,这些函数将扩展用于所有 cypress 测试的全局 cy 对象,但是这也必须输入并要求您编写自己的定义文件。
一切都很顺利,直到我尝试添加自己的类型定义,此时它又开始抱怨了。 (我想指出,我的项目确实按照我目前所做的一切按预期编译和运行。这只是 ESLint 不高兴)。
我试图解决的主要问题是为什么类型定义似乎没有包含在项目中。我收到以下错误:
Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: cypress\support\index.d.ts.
The file must be included in at least one of the projects provided.
以及是否有更好的方法来布局。
感谢任何真正花时间阅读并尝试的人。
文件夹结构如下:
cypress
- fixtures
- integrations
- - file.spec.ts <- Uses the custom functions in a test
- plugins
- support
- - commands.ts <- Contains my custom functions
- - index.d.ts <- Types for custom functions
- - index.ts
- .eslintrc.js (A)
- tsconfig.json (B)
src (contains all the jest tests)
.eslintrc.js (C)
tsconfig.json (D)
tsconfig.eslint.json
.eslintrc.js (A)
module.exports = {
extends: ['../.eslintrc.js'],
parserOptions: {
project: 'cypress/tsconfig.json',
},
}
tsconfig.json (B)
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
// be explicit about types included
// to avoid clashing with Jest types
"types": ["cypress", "cypress-axe"],
"allowJs": true
},
"include": [".eslintrc.js", "../node_modules/cypress", "./**/*"]
}
.eslintrc.js (C)
module.exports = {
parser: '@typescript-eslint/parser',
root: true,
env: {
es6: true,
node: true,
browser: true,
},
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
tsconfigRootDir: __dirname,
project: './tsconfig.eslint.json',
ecmaFeatures: {
jsx: true,
},
},
plugins: ['react', 'react-hooks', '@typescript-eslint', 'cypress'],
rules: {
'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
...
},
}
tsconfig.json (D)
{
"compilerOptions": {
"target": "es5",
"lib": ["es6", "dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"strict": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"baseUrl": "src",
"types": ["jest"]
},
"include": [
"build-system",
"src",
".eslintrc.js",
"pretty.js",
"gulpfile.js"
],
"exclude": ["node_modules", "src/*.test.ts", "src/*.test.tsx"]
}
tsconfig.eslint.json
{
// extend your base config so you don't have to redefine your compilerOptions
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true
},
"exclude": []
}
【问题讨论】:
-
尝试平分 - 禁用一半的配置,以获得仍然有效的最小配置。通过查看 stackoverflow 问题,您现在所拥有的确实有很多需要调试的地方。 eslint 扩展,并且 tsconfig 语法规则显然是删除的候选者,因为它们只会给问题增加噪音。
-
那里有很多事情要做。我设置了github.com/textbook/starter-kit-ts,它使用 ESLint/React/Cypress/TS/... - 查看配置我还没有为 ESLint 解析器设置显式项目。在 ESLint 中使用覆盖是值得的,这样您就可以更具体地了解哪些插件适用于何处(Jest -> src/ 中的测试文件,Cypress -> cypress/ 中的文件)。
-
我很抱歉,我知道这是一团糟。我试图稍微简化配置,但我不确定哪些信息最相关,所以我不确定我可以省略什么。
标签: reactjs typescript eslint