Skip to content
← Back to rules

import/no-cycle Restriction

Qué hace

Ensures that there is no resolvable path back to this module via its dependencies.

This includes cycles of depth 1 (imported module imports me) to an effectively infinite value, if the maxDepth option is not set.

¿Por qué es problemático?

Dependency cycles lead to confusing architectures where bugs become hard to find. It is common to import an undefined value that is caused by a cyclic dependency.

Ejemplos

Ejemplos de código incorrecto para esta regla:

javascript
// dep-b.js
import "./dep-a.js";
export function b() {
  /* ... */
}
javascript
// dep-a.js
import { b } from "./dep-b.js"; // reported: Dependency cycle detected.
export function a() {
  /* ... */
}

In this example, dep-a.js and dep-b.js import each other, creating a circular dependency, which is problematic.

Ejemplos de código correcto para esta regla:

javascript
// dep-b.js
export function b() {
  /* ... */
}
javascript
// dep-a.js
import { b } from "./dep-b.js"; // no circular dependency
export function a() {
  /* ... */
}

In this corrected version, dep-b.js no longer imports dep-a.js, breaking the cycle.

Configuración

Esta regla acepta un objeto de configuración con las siguientes propiedades:

allowUnsafeDynamicCyclicDependency

tipo: boolean

predeterminado: false

Allow cyclic dependency if there is at least one dynamic import in the chain

ignoreExternal

tipo: boolean

predeterminado: false

Ignore external modules

ignoreTypes

tipo: boolean

predeterminado: true

Ignore type-only imports

maxDepth

tipo: integer

predeterminado: 4294967295

Maximum dependency depth to traverse

Cómo usarla

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

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

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

Versión

Esta regla se añadió en v0.0.13.

Referencias