ESLint 9.x.x 错误 typescript-eslint/解析器

问题描述 投票:0回答:1

使用 TypeScript 为 ESLint 创建 9.x.x 配置时,我开始收到错误:

Error: Error while loading rule '@typescript-eslint/dot-notation': 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: typescript-eslint/parser`

同时,

eslint.config.js
看起来像这样:

export default tseslint.config(
  { ignores: ["dist", "assets", "build", "node_modules", "public", "vite.config.ts"] },
  {
    extends: [
      js.configs.recommended,
      ...tseslint.configs.recommended,
      eslintConfigPrettier
    ],
    settings: {
      "import/resolver": {
        typescript: {
          project: "./tsconfig.json",
        },
      },
    },
    files: ["**/*.{ts,tsx}"],
    languageOptions: {
      sourceType: "module",
      parserOptions: {
        ecmaFeatures: {
          jsx: true,
        },
        ecmaVersion: "latest",
        project: ["./tsconfig.json", "tsconfig.node.json", "tsconfig.app.json"], // <- Here it is
      },
      globals: globals.browser,
    },
    plugins: {
      "react-hooks": reactHooks,
    },
    rules: {
      ...reactHooks.configs.recommended.rules,
      "@typescript-eslint/dot-notation": "error",
    }
  }
);

parserOptions.project
属性已被注册,但错误仍然存在。

eslint typescript-eslint
1个回答
0
投票

原来不是

parseOptions.project
,而是没有
tsconfigRootDir: __dirname

添加后

import { fileURLToPath } from "url";
import { dirname } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// ...
      parserOptions: {
        ecmaFeatures: {
          jsx: true,
        },
        ecmaVersion: "latest",
        project: ["./tsconfig.json", "tsconfig.node.json", "tsconfig.app.json"],
        tsconfigRootDir: __dirname,
      }
// ...

一切顺利

© www.soinside.com 2019 - 2024. All rights reserved.