Skip to content
← Back to rules

import/no-self-import Suspicious

作用

禁止模块导入自身。这种情况有时会无意中发生, 尤其是在重构期间。

为什么这不好?

将模块导入自身会创建循环依赖,这可能会导致 运行时问题,包括无限循环、无法解析的导入或 undefined 值。

示例

以下是此规则的错误代码示例:

javascript
// foo.js
import foo from "./foo.js"; // 错误:模块导入自身
const foo = require("./foo"); // 错误:模块导入自身

以下是此规则的正确代码示例:

javascript
// foo.js
import bar from "./bar.js"; // 正确:模块导入另一个模块

如何使用

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

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

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

版本

此规则在 v0.0.13 中添加。

参考资料