Skip to content
← Back to rules

import/max-dependencies Pedantic

作用

禁止模块拥有过多依赖(仅 import 语句)。

为什么这不好?

这是一个有用的规则,因为依赖过多的模块是一种代码异味, 通常意味着该模块做了太多事情,和/或应该被拆分成 更小的模块。

注意:此规则只统计 import 语句,不统计来自 CommonJS require() 语句的依赖。这一点与原始的 eslint-import-plugin 规则不同。

示例

给定 { "max": 2 }

此规则的错误代码示例:

javascript
import a from "./a";
import b from "./b";
import c from "./c"; // 依赖过多:3(最大值:2)

此规则的正确代码示例:

javascript
import a from "./a";
import b from "./b"; // 允许:2 个依赖(最大值:2)

配置

此规则接受一个包含以下属性的配置对象:

ignoreTypeImports

类型:boolean

默认值:false

在统计依赖时是否忽略类型导入。

ts
// 如果 `ignoreTypeImports` 为 true,以下两者都不计为依赖:
import type { Foo } from "./foo";
import { type Foo } from "./foo";

max

类型:integer

默认值:10

文件中允许的最大依赖数量。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["import"],
  "rules": {
    "import/max-dependencies": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["import"],
  rules: {
    "import/max-dependencies": "error",
  },
});
bash
oxlint --deny import/max-dependencies --import-plugin

版本

此规则在 v0.5.0 中添加。

参考