Skip to content

Commit

Permalink
Merge pull request #1142 from rfermann/master
Browse files Browse the repository at this point in the history
New: `no-unused-modules` rule
  • Loading branch information
ljharb authored Apr 7, 2019
2 parents 0d812ad + d35b7ff commit 3aefa79
Show file tree
Hide file tree
Showing 29 changed files with 1,466 additions and 0 deletions.
101 changes: 101 additions & 0 deletions docs/rules/no-unused-modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# import/no-unused-modules

Reports:
- modules without any exports
- individual exports not being statically `import`ed or `require`ed from other modules in the same project

Note: dynamic imports are currently not supported.

## Rule Details


### Options

This rule takes the following option:

- `src`: an array with files/paths to be analyzed. It only applies to unused exports. Defaults to `process.cwd()`, if not provided
- `ignoreExports`: an array with files/paths for which unused exports will not be reported (e.g module entry points in a published package)
- `missingExports`: if `true`, files without any exports are reported
- `unusedExports`: if `true`, exports without any static usage within other modules are reported.


### Example for missing exports
#### The following will be reported
```js
const class MyClass { /*...*/ }

function makeClass() { return new MyClass(...arguments) }
```

#### The following will not be reported

```js
export default function () { /*...*/ }
```
```js
export const foo = function () { /*...*/ }
```
```js
export { foo, bar }
```
```js
export { foo as bar }
```

### Example for unused exports
given file-f:
```js
import { e } from 'file-a'
import { f } from 'file-b'
import * from 'file-c'
export * from 'file-d'
export { default, i0 } from 'file-e' // both will be reported

export const j = 99 // will be reported
```
and file-e:
```js
export const i0 = 9 // will not be reported
export const i1 = 9 // will be reported
export default () => {} // will not be reported
```
and file-d:
```js
export const h = 8 // will not be reported
export default () => {} // will be reported, as export * only considers named exports and ignores default exports
```
and file-c:
```js
export const g = 7 // will not be reported
export default () => {} // will not be reported
```
and file-b:
```js
import two, { b, c, doAnything } from 'file-a'

export const f = 6 // will not be reported
```
and file-a:
```js
const b = 2
const c = 3
const d = 4

export const a = 1 // will be reported

export { b, c } // will not be reported

export { d as e } // will not be reported

export function doAnything() {
// some code
} // will not be reported

export default 5 // will not be reported
```



## When not to use

If you don't mind having unused files or dead code within your codebase, you can disable this rule
13 changes: 13 additions & 0 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ ExportMap.parse = function (path, content, context) {

function captureDependency(declaration) {
if (declaration.source == null) return null
const importedSpecifiers = new Set()
const supportedTypes = new Set(['ImportDefaultSpecifier', 'ImportNamespaceSpecifier'])
if (declaration.specifiers) {
declaration.specifiers.forEach(specifier => {
if (supportedTypes.has(specifier.type)) {
importedSpecifiers.add(specifier.type)
}
if (specifier.type === 'ImportSpecifier') {
importedSpecifiers.add(specifier.local.name)
}
})
}

const p = remotePath(declaration.source.value)
if (p == null) return null
Expand All @@ -410,6 +422,7 @@ ExportMap.parse = function (path, content, context) {
value: declaration.source.value,
loc: declaration.source.loc,
},
importedSpecifiers,
})
return getter
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const rules = {
'no-named-as-default': require('./rules/no-named-as-default'),
'no-named-as-default-member': require('./rules/no-named-as-default-member'),
'no-anonymous-default-export': require('./rules/no-anonymous-default-export'),
'no-unused-modules': require('./rules/no-unused-modules'),

'no-commonjs': require('./rules/no-commonjs'),
'no-amd': require('./rules/no-amd'),
Expand Down
Loading

0 comments on commit 3aefa79

Please sign in to comment.