ค้นหาเว็บไซต์

typescript - ปัญหาการย้ายไปใช้ eslint 9.0.0


ฉันมีปัญหาต่อไปนี้ขณะอัปเกรด eslint เป็น 9.0.0

⋊> ~/A/fusion on turborepo ⨯ bun lint                                                          22:21:58
eslint packages/*/src/**/*

Oops! Something went wrong! :(

ESLint: 9.0.0

ConfigError: Config (unnamed): Key "plugins": Cannot redefine plugin "@typescript-eslint".
    at rethrowConfigError (/Users/aleksandrkireev/Apps/fusion/node_modules/@humanwhocodes/config-array/api.js:225:8)
    at /Users/aleksandrkireev/Apps/fusion/node_modules/@humanwhocodes/config-array/api.js:1018:5
    at Array.reduce (<anonymous>)
    at FlatConfigArray.getConfig (/Users/aleksandrkireev/Apps/fusion/node_modules/@humanwhocodes/config-array/api.js:1014:39)
    at FlatConfigArray.isFileIgnored (/Users/aleksandrkireev/Apps/fusion/node_modules/@humanwhocodes/config-array/api.js:1046:15)
    at /Users/aleksandrkireev/Apps/fusion/node_modules/eslint/lib/eslint/eslint-helpers.js:514:38
    at Array.forEach (<anonymous>)
    at findFiles (/Users/aleksandrkireev/Apps/fusion/node_modules/eslint/lib/eslint/eslint-helpers.js:503:11)
    at async ESLint.lintFiles (/Users/aleksandrkireev/Apps/fusion/node_modules/eslint/lib/eslint/eslint.js:846:27)
    at async Object.execute (/Users/aleksandrkireev/Apps/fusion/node_modules/eslint/lib/cli.js:461:23)
⋊> ~/A/fusion on turborepo ⨯             

ฉันไม่ได้ใช้แพ็กเกจ @humanwhocodes แต่ดูเหมือนว่าแพ็กเกจอื่นบางตัวจะใช้มัน

Deps :

  "dependencies": {
    "alor-api": "1.0.47",
    "axios": "1.6.8",
    "cache-manager": "5.5.1",
    "core-js": "3.36.1",
    "create-hmac": "1.1.7",
    "crypto-hash": "3.0.0",
    "date-fns": "^2.28.0",
    "eslint-plugin-react-hooks": "4.6.0",
    "moment": "2.30.1",
    "msgpack5": "6.0.2",
    "node-cron": "3.0.3",
    "node-fetch": "3.3.2",
    "node-yaml-config": "1.0.0",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-error-overlay": "6.0.11",
    "reflect-metadata": "0.2.2",
    "regenerator-runtime": "0.14.1",
    "request": "2.88.2",
    "swr": "2.2.5",
    "tslib": "2.6.2",
    "winston": "^3.8.2",
    "winston-daily-rotate-file": "^4.7.1",
    "winston-loki": "^6.0.6",
    "winston-telegram": "^2.6.0"
  },
  "devDependencies": {
    "@testing-library/react": "14.3.0",
    "@types/bun": "^1.0.12",
    "@types/jest": "29.5.12",
    "@types/node": "20.12.5",
    "@types/node-fetch": "^2.6.11",
    "@types/react": "18.2.74",
    "@types/react-dom": "18.2.24",
    "@typescript-eslint/parser": "^7.5.0",
    "babel-jest": "29.7.0",
    "create-ts-index": "^1.14.0",
    "cypress": "13.7.2",
    "esbuild": "^0.20.2",
    "eslint": "^9.0.0",
    "eslint-config-next": "^14.1.4",
    "eslint-config-prettier": "^9.1.0",
    "eslint-config-standard-with-typescript": "^43.0.1",
    "eslint-plugin-cypress": "^2.10.3",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-jsx-a11y": "^6.8.0",
    "eslint-plugin-n": "^15.0.0 || ^16.0.0",
    "eslint-plugin-promise": "^6.0.0",
    "eslint-plugin-react": "^7.34.1",
    "eslint-plugin-unused-imports": "^3.1.0",
    "globals": "^15.0.0",
    "husky": "^9.0.11",
    "jest": "29.4.3",
    "jest-environment-jsdom": "29.4.3",
    "jest-environment-node": "^29.4.1",
    "lint-staged": "^13.1.0",
    "sass": "1.74.1",
    "ts-jest": "29.1.2",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.4.4",
    "typescript-eslint": "^7.5.0",
    "url-loader": "^4.1.1"
  }

eslint.config.js:

import globals from "globals";
import tseslint from "typescript-eslint";
import pluginReactConfig from "eslint-plugin-react/configs/recommended.js";

import path from "path";
import { fileURLToPath } from "url";
import { FlatCompat } from "@eslint/eslintrc";
import pluginJs from "@eslint/js";

// mimic CommonJS variables -- not needed if using CommonJS
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});

export default [
  {languageOptions: { globals: globals.browser }},
  ...compat.extends("standard-with-typescript"),
  ...tseslint.configs.recommendedTypeChecked,
  {
    languageOptions: {
      parserOptions: {
        project: true,
        tsconfigRootDir: import.meta.dirname,
      },
    },
  },
  pluginReactConfig,
];

คำตอบ #1

ฉันคิดว่าปัญหาอาจเกิดจากคุณใส่ @typescript-eslint สองครั้งโดยใช้ตัวกระจาย (spread operator) กับทั้ง compat.extends("standard-with-typescript") และ tseslint.configs.recommendedTypeChecked


ถ้าคุณตรวจสอบ recommendedTypeChecked ที่นี่: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended-type-checked.ts

คุณสามารถเห็นได้ว่ามันขยายการตั้งค่าพื้นฐาน: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/base.ts

มันรวมปลั๊กอิน @typescript-eslint ด้วย


ฉันไม่แน่ใจว่าเกิดอะไรขึ้นกับสิ่งที่เรียกว่า FlatCompat ที่คุณใช้งานอยู่ตรงนั้น แต่ก็เป็นไปได้ว่ามันทำให้คุณรวมปลั๊กอิน @typescript-eslint เดิมอีกครั้ง ซึ่งส่งผลให้เกิดข้อผิดพลาดที่คุณเจอ

บทความที่เกี่ยวข้อง