Skip to content

Commit

Permalink
[Fix] no-unused-modules: fix crash due to export *
Browse files Browse the repository at this point in the history
  • Loading branch information
Taranys authored and ljharb committed Oct 8, 2019
1 parent 05085bb commit 0cd5e43
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`import/order`]: fix autofix to not move imports across fn calls ([#1253], thanks [@tihonove])
- [`prefer-default-export`]: fix false positive with type export ([#1506], thanks [@golopot])
- [`extensions`]: Fix `ignorePackages` to produce errors ([#1521], thanks [@saschanaz])
- [`no-unused-modules`]: fix crash due to `export *` ([#1496], thanks [@Taranys])

### Docs
- [`no-useless-path-segments`]: add docs for option `commonjs` ([#1507], thanks [@golopot])
Expand Down Expand Up @@ -626,6 +627,7 @@ for info on changes for earlier releases.
[#1519]: https://github.com/benmosher/eslint-plugin-import/pull/1519
[#1507]: https://github.com/benmosher/eslint-plugin-import/pull/1507
[#1506]: https://github.com/benmosher/eslint-plugin-import/pull/1506
[#1496]: https://github.com/benmosher/eslint-plugin-import/pull/1496
[#1495]: https://github.com/benmosher/eslint-plugin-import/pull/1495
[#1472]: https://github.com/benmosher/eslint-plugin-import/pull/1472
[#1470]: https://github.com/benmosher/eslint-plugin-import/pull/1470
Expand Down Expand Up @@ -1016,3 +1018,4 @@ for info on changes for earlier releases.
[@brendo]: https://github.com/brendo
[@saschanaz]: https://github.com/saschanaz
[@brettz9]: https://github.com/brettz9
[@Taranys]: https://github.com/Taranys
9 changes: 7 additions & 2 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ const prepareImportsAndExports = (srcFiles, context) => {

// dependencies === export * from
const currentExportAll = new Set()
dependencies.forEach(value => {
currentExportAll.add(value().path)
dependencies.forEach(getDependency => {
const dependency = getDependency()
if (dependency === null) {
return
}

currentExportAll.add(dependency.path)
})
exportAll.set(file, currentExportAll)

Expand Down
7 changes: 7 additions & 0 deletions tests/files/no-unused-modules/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Simple import extracted from 'redux-starter-kit' compiled file

function isPlain(val) {
return true;
}

exports.isPlain = isPlain;
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/filte-r.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cjs'
16 changes: 16 additions & 0 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ describe('test behaviour for new file', () => {
test({ options: unusedExportsOptions,
code: `export * from '${testFilePath('./no-unused-modules/file-added-0.js')}'`,
filename: testFilePath('./no-unused-modules/file-0.js')}),
// Test export * from 'external-compiled-library'
test({ options: unusedExportsOptions,
code: `export * from 'external-compiled-library'`,
filename: testFilePath('./no-unused-modules/file-r.js'),
}),
],
invalid: [
test({ options: unusedExportsOptions,
Expand Down Expand Up @@ -670,3 +675,14 @@ describe('correctly report flow types', () => {
],
})
})

describe('Avoid errors if re-export all from umd compiled library', () => {
ruleTester.run('no-unused-modules', rule, {
valid: [
test({ options: unusedExportsOptions,
code: `export * from '${testFilePath('./no-unused-modules/bin.js')}'`,
filename: testFilePath('./no-unused-modules/main/index.js')}),
],
invalid: [],
})
})

0 comments on commit 0cd5e43

Please sign in to comment.