| 1 | # import/max-dependencies
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | Forbid modules to have too many dependencies (`import` or `require` statements).
|
|---|
| 6 |
|
|---|
| 7 | This is a useful rule because a module with too many dependencies is a code smell, and usually indicates the module is doing too much and/or should be broken up into smaller modules.
|
|---|
| 8 |
|
|---|
| 9 | Importing multiple named exports from a single module will only count once (e.g. `import {x, y, z} from './foo'` will only count as a single dependency).
|
|---|
| 10 |
|
|---|
| 11 | ## Options
|
|---|
| 12 |
|
|---|
| 13 | This rule has the following options, with these defaults:
|
|---|
| 14 |
|
|---|
| 15 | ```js
|
|---|
| 16 | "import/max-dependencies": ["error", {
|
|---|
| 17 | "max": 10,
|
|---|
| 18 | "ignoreTypeImports": false,
|
|---|
| 19 | }]
|
|---|
| 20 | ```
|
|---|
| 21 |
|
|---|
| 22 | ### `max`
|
|---|
| 23 |
|
|---|
| 24 | This option sets the maximum number of dependencies allowed. Anything over will trigger the rule. **Default is 10** if the rule is enabled and no `max` is specified.
|
|---|
| 25 |
|
|---|
| 26 | Given a max value of `{"max": 2}`:
|
|---|
| 27 |
|
|---|
| 28 | ### Fail
|
|---|
| 29 |
|
|---|
| 30 | ```js
|
|---|
| 31 | import a from './a'; // 1
|
|---|
| 32 | const b = require('./b'); // 2
|
|---|
| 33 | import c from './c'; // 3 - exceeds max!
|
|---|
| 34 | ```
|
|---|
| 35 |
|
|---|
| 36 | ### Pass
|
|---|
| 37 |
|
|---|
| 38 | ```js
|
|---|
| 39 | import a from './a'; // 1
|
|---|
| 40 | const anotherA = require('./a'); // still 1
|
|---|
| 41 | import {x, y, z} from './foo'; // 2
|
|---|
| 42 | ```
|
|---|
| 43 |
|
|---|
| 44 | ### `ignoreTypeImports`
|
|---|
| 45 |
|
|---|
| 46 | Ignores `type` imports. Type imports are a feature released in TypeScript 3.8, you can [read more here](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export). Defaults to `false`.
|
|---|
| 47 |
|
|---|
| 48 | Given `{"max": 2, "ignoreTypeImports": true}`:
|
|---|
| 49 |
|
|---|
| 50 | <!-- markdownlint-disable-next-line MD024 -- duplicate header -->
|
|---|
| 51 | ### Fail
|
|---|
| 52 |
|
|---|
| 53 | ```ts
|
|---|
| 54 | import a from './a';
|
|---|
| 55 | import b from './b';
|
|---|
| 56 | import c from './c';
|
|---|
| 57 | ```
|
|---|
| 58 |
|
|---|
| 59 | <!-- markdownlint-disable-next-line MD024 -- duplicate header -->
|
|---|
| 60 | ### Pass
|
|---|
| 61 |
|
|---|
| 62 | ```ts
|
|---|
| 63 | import a from './a';
|
|---|
| 64 | import b from './b';
|
|---|
| 65 | import type c from './c'; // Doesn't count against max
|
|---|
| 66 | ```
|
|---|
| 67 |
|
|---|
| 68 | ## When Not To Use It
|
|---|
| 69 |
|
|---|
| 70 | If you don't care how many dependencies a module has.
|
|---|