Skip to content
← Back to rules

import/no-relative-parent-imports Restriction

它的作用

禁止使用相对路径从父目录导入模块。

为什么这不好?

这一限制强制使用树状的文件夹结构,而不是复杂的 图状结构,从而让大型代码库更易于维护。 依赖关系单向流动(从父到子),这使得 模块关系更加清晰。

示例

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

javascript
import foo from "../bar";
import foo from "../../utils/helper";
const baz = require("../config");
export { qux } from "../shared";

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

javascript
import foo from "lodash";
import a from "./lib/a";
import b from "./b";

如何使用

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

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

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

版本

此规则于 v1.43.0 中添加。

参考资料