import/no-cycle Restriction
何をするか
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.
なぜ問題なのか
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.
例
このルールで適合しないコード例:
// dep-b.js
import "./dep-a.js";
export function b() {
/* ... */
}// 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.
このルールで適合するコード例:
// dep-b.js
export function b() {
/* ... */
}// 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.
設定
このルールは、次のプロパティを持つ設定オブジェクトを受け付けます。
allowUnsafeDynamicCyclicDependency
型: boolean
既定値: false
Allow cyclic dependency if there is at least one dynamic import in the chain
ignoreExternal
型: boolean
既定値: false
Ignore external modules
ignoreTypes
型: boolean
既定値: true
Ignore type-only imports
maxDepth
type: integer
default: 4294967295
Maximum dependency depth to traverse
使い方
To enable this rule using the config file or in the CLI, you can use:
{
"plugins": ["import"],
"rules": {
"import/no-cycle": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["import"],
rules: {
"import/no-cycle": "error",
},
});oxlint --deny import/no-cycle --import-pluginバージョン
このルールは v0.0.13 で追加されました。