Skip to content

Commit 3aefa79

Browse files
authored
Merge pull request #1142 from rfermann/master
New: `no-unused-modules` rule
2 parents 0d812ad + d35b7ff commit 3aefa79

29 files changed

+1466
-0
lines changed

docs/rules/no-unused-modules.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

src/ExportMap.js

+13
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ ExportMap.parse = function (path, content, context) {
397397

398398
function captureDependency(declaration) {
399399
if (declaration.source == null) return null
400+
const importedSpecifiers = new Set()
401+
const supportedTypes = new Set(['ImportDefaultSpecifier', 'ImportNamespaceSpecifier'])
402+
if (declaration.specifiers) {
403+
declaration.specifiers.forEach(specifier => {
404+
if (supportedTypes.has(specifier.type)) {
405+
importedSpecifiers.add(specifier.type)
406+
}
407+
if (specifier.type === 'ImportSpecifier') {
408+
importedSpecifiers.add(specifier.local.name)
409+
}
410+
})
411+
}
400412

401413
const p = remotePath(declaration.source.value)
402414
if (p == null) return null
@@ -410,6 +422,7 @@ ExportMap.parse = function (path, content, context) {
410422
value: declaration.source.value,
411423
loc: declaration.source.loc,
412424
},
425+
importedSpecifiers,
413426
})
414427
return getter
415428
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const rules = {
1818
'no-named-as-default': require('./rules/no-named-as-default'),
1919
'no-named-as-default-member': require('./rules/no-named-as-default-member'),
2020
'no-anonymous-default-export': require('./rules/no-anonymous-default-export'),
21+
'no-unused-modules': require('./rules/no-unused-modules'),
2122

2223
'no-commonjs': require('./rules/no-commonjs'),
2324
'no-amd': require('./rules/no-amd'),

0 commit comments

Comments
 (0)