如何在 eslint-plugin-import 中为 typescript 配置 `no-cycle` 规则?

时间:2021-03-13 04:30:44

标签: typescript eslint

共有三个.ts文件,a.tsb.ts相互循环导入,而c.ts同时导入a.tsb.ts

在检查 c.ts 时,我希望 ESLint 报告循环依赖。

但出于某种原因,运行 yarn eslint src/c.ts 不会引发任何错误!!!

a.ts

import y from "./b";

const x: number = y + 1;
export default x;

b.ts

import x from "./a";

const y: number = x + 1;
export default y;

c.ts

import x from "./a";
import y from "./b";

console.log(x, y);

.eslintrc.json

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript"
  ],
  "plugins": ["@typescript-eslint", "import"],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "settings": {
    "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    }
  },
  "rules": {
    "import/no-cycle": [
      "error",
      {
        "maxDepth": 10,
        "ignoreExternal": true
      }
    ]
  }
}

github 存储库:https://github.com/Yaojian/no-cycle-test

1 个答案:

答案 0 :(得分:0)

它不会在 c.ts 文件中引发 lint 错误,因为循环依赖位于 a.tsb.ts 之间。 ESLint no-cycle rule description.

a.ts file enter image description here

b.ts file enter image description here