首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含的tsconfig文件上的ESLint抛出错误

包含的tsconfig文件上的ESLint抛出错误
EN

Stack Overflow用户
提问于 2022-03-20 20:17:58
回答 1查看 1.3K关注 0票数 3

我有以下存储库结构:

cypress文件夹

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

src文件夹

.eslintrc.js

tsconfig.base.json

tsconfig.json

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

代码语言:javascript
复制
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

代码语言:javascript
复制
{
    "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文件:

代码语言:javascript
复制
{
    "extends": "./tsconfig.base.json",
    "include": ["next-env.d.ts", "src/**/*.ts", "src/**/*.tsx", "tests/**/*.ts"],
    "exclude": ["node_modules"]
}

.eslintrc.js文件:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "isolatedModules": false,
        "types": ["cypress"]
    },
    "include": ["../node_modules/cypress", "./**/*.ts"],
    "exclude": []
}

cypress/.eslintrc.js文件:

代码语言:javascript
复制
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文件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-25 20:28:59

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

代码语言:javascript
复制
parserOptions: {
        ecmaVersion: 12,
        project: './tsconfig.json',
        tsconfigRootDir: 'cypress',
        sourceType: 'module',
},

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

如需更多参考:https://github.com/typescript-eslint/typescript-eslint/issues/4732

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71550327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档