| 1 | # import/no-unused-modules
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | Reports:
|
|---|
| 6 |
|
|---|
| 7 | - modules without any exports
|
|---|
| 8 | - individual exports not being statically `import`ed or `require`ed from other modules in the same project
|
|---|
| 9 | - dynamic imports are supported if argument is a literal string
|
|---|
| 10 |
|
|---|
| 11 | ## Rule Details
|
|---|
| 12 |
|
|---|
| 13 | ### Usage
|
|---|
| 14 |
|
|---|
| 15 | In order for this plugin to work, at least one of the options `missingExports` or `unusedExports` must be enabled (see "Options" section below). In the future, these options will be enabled by default (see <https://github.com/import-js/eslint-plugin-import/issues/1324>)
|
|---|
| 16 |
|
|---|
| 17 | Example:
|
|---|
| 18 |
|
|---|
| 19 | ```json
|
|---|
| 20 | "rules": {
|
|---|
| 21 | ...otherRules,
|
|---|
| 22 | "import/no-unused-modules": [1, {"unusedExports": true}]
|
|---|
| 23 | }
|
|---|
| 24 | ```
|
|---|
| 25 |
|
|---|
| 26 | ### Options
|
|---|
| 27 |
|
|---|
| 28 | This rule takes the following option:
|
|---|
| 29 |
|
|---|
| 30 | - **`missingExports`**: if `true`, files without any exports are reported (defaults to `false`)
|
|---|
| 31 | - **`unusedExports`**: if `true`, exports without any static usage within other modules are reported (defaults to `false`)
|
|---|
| 32 | - **`ignoreUnusedTypeExports`**: if `true`, TypeScript type exports without any static usage within other modules are reported (defaults to `false` and has no effect unless `unusedExports` is `true`)
|
|---|
| 33 | - **`src`**: an array with files/paths to be analyzed. It only applies to unused exports. Defaults to `process.cwd()`, if not provided
|
|---|
| 34 | - **`ignoreExports`**: an array with files/paths for which unused exports will not be reported (e.g module entry points in a published package)
|
|---|
| 35 |
|
|---|
| 36 | ### Example for missing exports
|
|---|
| 37 |
|
|---|
| 38 | #### The following will be reported
|
|---|
| 39 |
|
|---|
| 40 | ```js
|
|---|
| 41 | const class MyClass { /*...*/ }
|
|---|
| 42 |
|
|---|
| 43 | function makeClass() { return new MyClass(...arguments) }
|
|---|
| 44 | ```
|
|---|
| 45 |
|
|---|
| 46 | #### The following will not be reported
|
|---|
| 47 |
|
|---|
| 48 | ```js
|
|---|
| 49 | export default function () { /*...*/ }
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | ```js
|
|---|
| 53 | export const foo = function () { /*...*/ }
|
|---|
| 54 | ```
|
|---|
| 55 |
|
|---|
| 56 | ```js
|
|---|
| 57 | export { foo, bar }
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | ```js
|
|---|
| 61 | export { foo as bar }
|
|---|
| 62 | ```
|
|---|
| 63 |
|
|---|
| 64 | ### Example for unused exports
|
|---|
| 65 |
|
|---|
| 66 | given file-f:
|
|---|
| 67 |
|
|---|
| 68 | ```js
|
|---|
| 69 | import { e } from 'file-a'
|
|---|
| 70 | import { f } from 'file-b'
|
|---|
| 71 | import * as fileC from 'file-c'
|
|---|
| 72 | export { default, i0 } from 'file-d' // both will be reported
|
|---|
| 73 |
|
|---|
| 74 | export const j = 99 // will be reported
|
|---|
| 75 | ```
|
|---|
| 76 |
|
|---|
| 77 | and file-d:
|
|---|
| 78 |
|
|---|
| 79 | ```js
|
|---|
| 80 | export const i0 = 9 // will not be reported
|
|---|
| 81 | export const i1 = 9 // will be reported
|
|---|
| 82 | export default () => {} // will not be reported
|
|---|
| 83 | ```
|
|---|
| 84 |
|
|---|
| 85 | and file-c:
|
|---|
| 86 |
|
|---|
| 87 | ```js
|
|---|
| 88 | export const h = 8 // will not be reported
|
|---|
| 89 | export default () => {} // will be reported, as export * only considers named exports and ignores default exports
|
|---|
| 90 | ```
|
|---|
| 91 |
|
|---|
| 92 | and file-b:
|
|---|
| 93 |
|
|---|
| 94 | ```js
|
|---|
| 95 | import two, { b, c, doAnything } from 'file-a'
|
|---|
| 96 |
|
|---|
| 97 | export const f = 6 // will not be reported
|
|---|
| 98 | ```
|
|---|
| 99 |
|
|---|
| 100 | and file-a:
|
|---|
| 101 |
|
|---|
| 102 | ```js
|
|---|
| 103 | const b = 2
|
|---|
| 104 | const c = 3
|
|---|
| 105 | const d = 4
|
|---|
| 106 |
|
|---|
| 107 | export const a = 1 // will be reported
|
|---|
| 108 |
|
|---|
| 109 | export { b, c } // will not be reported
|
|---|
| 110 |
|
|---|
| 111 | export { d as e } // will not be reported
|
|---|
| 112 |
|
|---|
| 113 | export function doAnything() {
|
|---|
| 114 | // some code
|
|---|
| 115 | } // will not be reported
|
|---|
| 116 |
|
|---|
| 117 | export default 5 // will not be reported
|
|---|
| 118 | ```
|
|---|
| 119 |
|
|---|
| 120 | ### Unused exports with `ignoreUnusedTypeExports` set to `true`
|
|---|
| 121 |
|
|---|
| 122 | The following will not be reported:
|
|---|
| 123 |
|
|---|
| 124 | ```ts
|
|---|
| 125 | export type Foo = {}; // will not be reported
|
|---|
| 126 | export interface Foo = {}; // will not be reported
|
|---|
| 127 | export enum Foo {}; // will not be reported
|
|---|
| 128 | ```
|
|---|
| 129 |
|
|---|
| 130 | #### Important Note
|
|---|
| 131 |
|
|---|
| 132 | Exports from files listed as a main file (`main`, `browser`, or `bin` fields in `package.json`) will be ignored by default. This only applies if the `package.json` is not set to `private: true`
|
|---|
| 133 |
|
|---|
| 134 | ## When not to use
|
|---|
| 135 |
|
|---|
| 136 | If you don't mind having unused files or dead code within your codebase, you can disable this rule
|
|---|