Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: no-unused-modules rule #1142

Merged
merged 20 commits into from
Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
75a9eab
New: `no-unused-modules` rule
rfermann Jul 23, 2018
1d8ddf7
New: `no-unused-modules` rule - minor refactoring
rfermann Jul 30, 2018
153599d
New: `no-unused-modules` rule - added tests
rfermann Jul 30, 2018
fd1ccc0
New: `no-unused-modules` rule - removed destructoring of context.getF…
rfermann Jul 31, 2018
a402023
New: `no-unused-modules` rule - minor refactoring
rfermann Jul 31, 2018
230122b
New: `no-unused-modules` rule - added more tests
rfermann Jul 31, 2018
aa7253b
New: `no-unused-modules` rule - added default src, more comprehensive…
rfermann Aug 5, 2018
d47ccdf
New: `no-unused-modules` rule - add support for 'import *'
rfermann Aug 11, 2018
06a0976
New: rule - added support for 'export * from' and 'export { stuff } …
rfermann Aug 19, 2018
c483514
New: `no-unused-modules` rule - renamed 'ignore' option to 'ignoreExp…
rfermann Sep 12, 2018
e04c87c
New: `no-unused-modules` rule - corrected typo in docs
rfermann Sep 12, 2018
2922910
New: `no-unused-modules` rule - reworked schema, removed UNDEFINED va…
Oct 18, 2018
0f5c2a5
New: `no-unused-modules` rule - implemented latest feedback
rfermann Dec 18, 2018
191c77b
New: `no-unused-modules` rule - added trailing newlines
rfermann Dec 23, 2018
ae9942f
New: `no-unused-modules` rule - added stricter check for node_modules
rfermann Dec 23, 2018
32f4c23
New: `no-unused-modules` rule - revert changing eslint version
rfermann Dec 23, 2018
90f7217
New: `no-unused-modules` rule - removed whitespace, replaced if-stmt
rfermann Dec 23, 2018
0dd398c
ew: `no-unused-modules` rule - remove unnecessary exports
rfermann Mar 31, 2019
1ac4bd7
ew: `no-unused-modules` rule - remove unnecessary exports
rfermann Mar 31, 2019
d35b7ff
Merge branches 'master' and 'master' of https://github.com/rfermann/e…
rfermann Mar 31, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
rfermann marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"chai": "^3.5.0",
"coveralls": "^3.0.0",
"cross-env": "^4.0.0",
"eslint": "2.x - 5.x",
"eslint": "^5.11.0",
rfermann marked this conversation as resolved.
Show resolved Hide resolved
"eslint-import-resolver-node": "file:./resolvers/node",
"eslint-import-resolver-typescript": "^1.0.2",
"eslint-import-resolver-webpack": "file:./resolvers/webpack",
Expand Down
13 changes: 13 additions & 0 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,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 @@ -392,6 +404,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