簡體   English   中英

如何在 eslint-plugin-import 中為 typescript 配置 `no-cycle` 規則?

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

一共有三個.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 repo: https://github.com/Yaojian/no-cycle-test

它不會在c.ts文件中引發 lint 錯誤,因為循環依賴關系在a.tsb.ts之間。 ESLint 無循環規則說明。

a.ts file 在此處輸入圖像描述

b.ts file 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM