Ошибка синтаксического анализа: не была найдена службой проекта, но я проигнорировал эти файлы

Структура моего проекта

  • dist (скомпилированный код ts)
    • библиотека
      • lib.d.ts
      • lib.js
    • index.d.ts
    • index.js
  • src (исходный код)
    • библиотека
      • lib.ts
    • index.ts
  • eslint.config.mjs
  • packages.json
  • tsconfig.json
  • пряжа.замок

Мой tsconfig.json

{
    "compilerOptions": {
        "target": "ES2022",
        "module": "commonjs",
        "outDir": "./dist",
        "rootDir": "./",
        "strict": true,
        "esModuleInterop": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "baseUrl": "./src",
        "declaration": true,
        "allowJs": true
    },
    "include": [
        "src/**/*.ts",
    ],
    "exclude": [
        "eslint.config.mjs",
        "dist/**"
    ]
}

Мой eslint.config.mjs:

// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import tsParser from '@typescript-eslint/parser'

import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
    
const __dirname = dirname(fileURLToPath(import.meta.url));

export default tseslint.config(
  {
    files: [
      "src/**/*.ts"
    ],
    ignores: [
      'dist/**/*.ts',
      'dist/**',
      "**/*.mjs",
      "eslint.config.mjs",
      "**/*.js"
    ],
    rules: {
      "@typescript-eslint/no-unnecessary-condition": "error",
      "@typescript-eslint/no-unnecessary-type-assertion": "error",
    },
  },
  ...tseslint.configs.strictTypeChecked,
  ...tseslint.configs.recommendedTypeChecked,
  {
    languageOptions: {
      parserOptions: {
        project: "./tsconfig.json",
        projectService: true,
        tsconfigRootDir: __dirname,
        projectFolderIgnoreList: [
          "**dist**",
        ],
      },
    },
  },
);

Я хочу, чтобы tsc помещал скомпилированные результаты в папку dist (я новичок в TypeScript, поэтому не знаю, правильный ли это путь к проекту TS).

Я проигнорировал файлы в папке dist, но все равно получил следующие ошибки:

/Users/kainzhong/VSCode/eslintTmp/dist/src/index.d.ts
  0:0  error  Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/index.d.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

/Users/kainzhong/VSCode/eslintTmp/dist/src/index.js
  0:0  error  Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/index.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

/Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.d.ts
  0:0  error  Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.d.ts was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

/Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.js
  0:0  error  Parsing error: /Users/kainzhong/VSCode/eslintTmp/dist/src/lib/lib.js was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

/Users/kainzhong/VSCode/eslintTmp/eslint.config.mjs
  0:0  error  Parsing error: /Users/kainzhong/VSCode/eslintTmp/eslint.config.mjs was not found by the project service. Consider either including it in the tsconfig.json or including it in allowDefaultProject

Разве я уже не проигнорировал эти файлы? Почему Эслинт до сих пор на них жалуется?

🤔 А знаете ли вы, что...
JavaScript можно использовать для создания ботов и автоматизации задач в браузерах с помощью Puppeteer.


64
1

Ответ:

Решено

Эта конфигурация на самом деле не игнорирует эти файлы — в этом проблема. ignores имеет два способа поведения:

  • Находясь в блоке конфигурации с files, он не позволяет этому конкретному блоку конфигурации влиять на игнорируемые файлы.
  • Когда в их собственном блоке конфигурации нет files, он сообщает ESLint игнорировать эти файлы.

Переместите ignores в отдельный блок в начале:

export default tseslint.config(
  {
    ignores: [
      'dist/**/*.ts',
      'dist/**',
      "**/*.mjs",
      "eslint.config.mjs",
      "**/*.js"
    ],
  },
  {
    files: [
      "src/**/*.ts"
    ],
  // ...

Из https://eslint.org/docs/latest/use/configure/configuration-files#exclude-files-with-ignores.