|
| 1 | +# import/no-unused-modules |
| 2 | + |
| 3 | +Reports: |
| 4 | + - modules without any exports |
| 5 | + - individual exports not being statically `import`ed or `require`ed from other modules in the same project |
| 6 | + |
| 7 | +Note: dynamic imports are currently not supported. |
| 8 | + |
| 9 | +## Rule Details |
| 10 | + |
| 11 | + |
| 12 | +### Options |
| 13 | + |
| 14 | +This rule takes the following option: |
| 15 | + |
| 16 | +- `src`: an array with files/paths to be analyzed. It only applies to unused exports. Defaults to `process.cwd()`, if not provided |
| 17 | +- `ignoreExports`: an array with files/paths for which unused exports will not be reported (e.g module entry points in a published package) |
| 18 | +- `missingExports`: if `true`, files without any exports are reported |
| 19 | +- `unusedExports`: if `true`, exports without any static usage within other modules are reported. |
| 20 | + |
| 21 | + |
| 22 | +### Example for missing exports |
| 23 | +#### The following will be reported |
| 24 | +```js |
| 25 | +const class MyClass { /*...*/ } |
| 26 | + |
| 27 | +function makeClass() { return new MyClass(...arguments) } |
| 28 | +``` |
| 29 | + |
| 30 | +#### The following will not be reported |
| 31 | + |
| 32 | +```js |
| 33 | +export default function () { /*...*/ } |
| 34 | +``` |
| 35 | +```js |
| 36 | +export const foo = function () { /*...*/ } |
| 37 | +``` |
| 38 | +```js |
| 39 | +export { foo, bar } |
| 40 | +``` |
| 41 | +```js |
| 42 | +export { foo as bar } |
| 43 | +``` |
| 44 | + |
| 45 | +### Example for unused exports |
| 46 | +given file-f: |
| 47 | +```js |
| 48 | +import { e } from 'file-a' |
| 49 | +import { f } from 'file-b' |
| 50 | +import * from 'file-c' |
| 51 | +export * from 'file-d' |
| 52 | +export { default, i0 } from 'file-e' // both will be reported |
| 53 | + |
| 54 | +export const j = 99 // will be reported |
| 55 | +``` |
| 56 | +and file-e: |
| 57 | +```js |
| 58 | +export const i0 = 9 // will not be reported |
| 59 | +export const i1 = 9 // will be reported |
| 60 | +export default () => {} // will not be reported |
| 61 | +``` |
| 62 | +and file-d: |
| 63 | +```js |
| 64 | +export const h = 8 // will not be reported |
| 65 | +export default () => {} // will be reported, as export * only considers named exports and ignores default exports |
| 66 | +``` |
| 67 | +and file-c: |
| 68 | +```js |
| 69 | +export const g = 7 // will not be reported |
| 70 | +export default () => {} // will not be reported |
| 71 | +``` |
| 72 | +and file-b: |
| 73 | +```js |
| 74 | +import two, { b, c, doAnything } from 'file-a' |
| 75 | + |
| 76 | +export const f = 6 // will not be reported |
| 77 | +``` |
| 78 | +and file-a: |
| 79 | +```js |
| 80 | +const b = 2 |
| 81 | +const c = 3 |
| 82 | +const d = 4 |
| 83 | + |
| 84 | +export const a = 1 // will be reported |
| 85 | + |
| 86 | +export { b, c } // will not be reported |
| 87 | + |
| 88 | +export { d as e } // will not be reported |
| 89 | + |
| 90 | +export function doAnything() { |
| 91 | + // some code |
| 92 | +} // will not be reported |
| 93 | + |
| 94 | +export default 5 // will not be reported |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +## When not to use |
| 100 | + |
| 101 | +If you don't mind having unused files or dead code within your codebase, you can disable this rule |
0 commit comments