typescript - خطای eslint هنگام فعال کردن tseslint type checked و نادیده گرفتن فایل eslint.config.js
من در حال تلاش برای پیکربندی eslint برای تایپاسکریپت با بررسی نوع هستم و نمیتوانم آن را درست کار کنم. اگر ...tseslint.configs.recommendedTypeChecked را اضافه کنم و فایل eslint.config.js را نادیده بگیرم، خطای زیر را دریافت میکنم:
Error: Error while loading rule '@typescript-eslint/await-thenable': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Parser: undefined
Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.
Occurred while linting /home/...
...
در زیر تمام فایلهایی که ممکن است مرتبط باشند آمده است:
eslint.config.js
// @ts-check
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import eslintConfigPrettier from "eslint-config-prettier";
import globals from "globals";
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
eslintConfigPrettier,
{
plugins: {
"@typescript-eslint": tseslint.plugin,
},
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
},
globals: {
...globals.node,
},
},
ignores: ["**/dist/**", "**/node_modules/**", "**/eslint.config.js"],
files: ["src/**/*.ts"],
},
{
rules: {
"@typescript-eslint/no-unused-vars": "warn",
},
},
);
tsconfig.json
{
"extends": "@tsconfig/node20/tsconfig.json",
"include": ["src"],
"compilerOptions": {
"outDir": "dist"
}
}
prettier.config.js
/** @type {import("prettier").Config} */
const config = {
experimentalTernaries: true,
proseWrap: "always",
};
export default config;
دلیل نادیده گرفتن فایل eslint.config.js جلوگیری از "ESLint برای اجرا پیکربندی شده بود... با این حال، TSConfig این فایل را شامل نمی شود/هیچکدام از آن TSConfig ها شامل این فایل نمی شوند" است، همانطور که در سوالات متداول اینجا توصیه می شود و در اینجا حل می شود.
پاسخ شماره ۱
من فکر میکنم مشکل بالا به نحوه عملکرد کلیدهای پیکربندی ignores و files مربوط میشود. به طور مشخص، من معتقدم files نمیتواند به صورت جهانی اعمال شود و ignores تنها زمانی به صورت جهانی اعمال میشود که بدون هیچ کلید دیگری استفاده شود. در پیکربندی اصلی من ...tseslint.configs.recommendedTypeChecked به تمام فایلها اعمال میشود اما تجزیهکننده فقط به فایلهای ts داخل دایرکتوری src اعمال میشود.
من این کار را دوباره از ابتدا انجام دادهام و پایین دیگر هیچ خطا یا هشداری نمیدهد:
// @ts-check
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import prettier from "eslint-config-prettier";
import globals from "globals";
export default tseslint.config(
// global ignores
{
ignores: ["dist/", "node_modules/"],
},
// applies to everything
eslint.configs.recommended,
// applies only to ts files
{
name: "tseslint",
files: ["src/**/*.ts"],
extends: [
//
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.strictTypeChecked,
],
plugins: {
"@typescript-eslint": tseslint.plugin,
},
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: true,
},
},
rules: {
"@typescript-eslint/no-unused-vars": "warn",
},
},
// global variables, applies to everything
{
languageOptions: {
globals: {
...globals.node,
},
},
},
// prettier config
prettier
);
