source: frontend/node_modules/eslint-plugin-import/docs/rules/no-cycle.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.7 KB
Line 
1# import/no-cycle
2
3<!-- end auto-generated rule header -->
4
5Ensures that there is no resolvable path back to this module via its dependencies.
6
7This includes cycles of depth 1 (imported module imports me) to `"∞"` (or `Infinity`), if the
8[`maxDepth`](#maxdepth) option is not set.
9
10```js
11// dep-b.js
12import './dep-a.js'
13
14export function b() { /* ... */ }
15```
16
17```js
18// dep-a.js
19import { b } from './dep-b.js' // reported: Dependency cycle detected.
20```
21
22This rule does _not_ detect imports that resolve directly to the linted module;
23for that, see [`no-self-import`].
24
25This rule ignores type-only imports in Flow and TypeScript syntax (`import type` and `import typeof`), which have no runtime effect.
26
27## Rule Details
28
29### Options
30
31By default, this rule only detects cycles for ES6 imports, but see the [`no-unresolved` options](./no-unresolved.md#options) as this rule also supports the same `commonjs` and `amd` flags. However, these flags only impact which import types are _linted_; the
32import/export infrastructure only registers `import` statements in dependencies, so
33cycles created by `require` within imported modules may not be detected.
34
35#### `maxDepth`
36
37There is a `maxDepth` option available to prevent full expansion of very deep dependency trees:
38
39```js
40/*eslint import/no-cycle: [2, { maxDepth: 1 }]*/
41
42// dep-c.js
43import './dep-a.js'
44```
45
46```js
47// dep-b.js
48import './dep-c.js'
49
50export function b() { /* ... */ }
51```
52
53```js
54// dep-a.js
55import { b } from './dep-b.js' // not reported as the cycle is at depth 2
56```
57
58This is not necessarily recommended, but available as a cost/benefit tradeoff mechanism
59for reducing total project lint time, if needed.
60
61#### `ignoreExternal`
62
63An `ignoreExternal` option is available to prevent the cycle detection to expand to external modules:
64
65```js
66/*eslint import/no-cycle: [2, { ignoreExternal: true }]*/
67
68// dep-a.js
69import 'module-b/dep-b.js'
70
71export function a() { /* ... */ }
72```
73
74```js
75// node_modules/module-b/dep-b.js
76import { a } from './dep-a.js' // not reported as this module is external
77```
78
79Its value is `false` by default, but can be set to `true` for reducing total project lint time, if needed.
80
81#### `allowUnsafeDynamicCyclicDependency`
82
83This option disable reporting of errors if a cycle is detected with at least one dynamic import.
84
85```js
86// bar.js
87import { foo } from './foo';
88export const bar = foo;
89
90// foo.js
91export const foo = 'Foo';
92export function getBar() { return import('./bar'); }
93```
94
95> Cyclic dependency are **always** a dangerous anti-pattern as discussed extensively in [#2265](https://github.com/import-js/eslint-plugin-import/issues/2265). Please be extra careful about using this option.
96
97#### `disableScc`
98
99This option disables a pre-processing step that calculates [Strongly Connected Components](https://en.wikipedia.org/wiki/Strongly_connected_component), which are used for avoiding unnecessary work checking files in different SCCs for cycles.
100
101However, under some configurations, this pre-processing may be more expensive than the time it saves.
102
103When this option is `true`, we don't calculate any SCC graph, and check all files for cycles (leading to higher time-complexity). Default is `false`.
104
105## When Not To Use It
106
107This rule is comparatively computationally expensive. If you are pressed for lint
108time, or don't think you have an issue with dependency cycles, you may not want
109this rule enabled.
110
111## Further Reading
112
113 - [Original inspiring issue](https://github.com/import-js/eslint-plugin-import/issues/941)
114 - Rule to detect that module imports itself: [`no-self-import`]
115 - [`import/external-module-folders`] setting
116
117[`no-self-import`]: ./no-self-import.md
118
119[`import/external-module-folders`]: ../../README.md#importexternal-module-folders
Note: See TracBrowser for help on using the repository browser.