typescript - ESLint kastar fel på inkluderad tsconfig-fil


Jag har följande mappstruktur:

cypress-mapp

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

src-mapp

.eslintrc.js
tsconfig.base.json
tsconfig.json

Min avsikt är att ställa in root-tsconfig.json endast för src-mappen, och samma gäller för .eslintrc.js. Sedan försöker jag konfigurera tsconfig.json och .eslintrc.js även för cypressmappen. Men jag får följande felmeddelande när jag kör 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

här är några utdrag från konfigurationsfilerna:

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-fil:

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

Fil .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'],
};

filen cypress/tsconfig.json:

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

cypress/.eslintrc.js-fil:

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'],
};

Så jag förstår inte varför jag fick felet, eftersom basic.spec.ts är inkluderad i cypress/tsconfig.json-filen.

Svar #1

Så problemet är att ESLint som standard upptäcker nuvarande arbetskatalog som rotmappen. Så detta ledde till att ESLint upptäckte rotens tsconfig.json. Problemet löstes genom att göra följande i filen cypress/.eslintrc.js:

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

(kan också lösas genom att ställa in projektvärdet till cypress/tsconfig.json)

För mer referens: https://github.com/typescript-eslint/typescript-eslint/issues/4732

Svar #2

För SvelteKit-utvecklare

Lägg till detta i din tsconfig.json:

{
    "include": [
        "./.svelte-kit/ambient.d.ts",
        "./.svelte-kit/types/**/$types.d.ts",
        "./vite.config.ts",
        "./src/**/*.js",
        "./src/**/*.ts",
        "./src/**/*.svelte",
        "./tests/**/*.js",
        "./tests/**/*.ts",
        "./tests/**/*.svelte",

        "**/.*.cjs",
        "**/*.cjs",
        "**/*.js",
        "**/*.ts"
    ]
}

Var uppmärksam på vägar, det finns inget stavfel.

Relaterade artiklar