简体   繁体   中英

How to config the `no-cycle` rule in eslint-plugin-import for typescript?

There are three .ts files, a.ts and b.ts are circular imported each other, while c.ts imports both a.ts and b.ts .

When checking c.ts , I expect that ESLint reports the circular depencency.

But for some reason, running yarn eslint src/c.ts does NOT raise any error !!!

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);

and the .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
      }
    ]
  }
}

The github repo: https://github.com/Yaojian/no-cycle-test

It won't raise the lint error in c.ts file because the circular dependency is between a.ts and b.ts . ESLint no-cycle rule description.

a.ts file 在此处输入图像描述

b.ts file 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM