Skip to content

Commit

Permalink
[Fix] no-unused-modules: Count re-export as usage when used in comb…
Browse files Browse the repository at this point in the history
…ination with import
  • Loading branch information
Ephem committed Apr 13, 2020
1 parent 3b4487c commit 40d1e67
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
- [`group-exports`]: Flow type export awareness ([#1702], thanks [@ernestostifano])
- [`order`]: Recognize pathGroup config for first group ([#1719], [#1724], thanks [@forivall], [@xpl])
- [`no-unused-modules`]: Fix re-export not counting as usage when used in combination with import ([#1722], thanks [@Ephem])

### Changed
- TypeScript config: Disable [`named`][] ([#1726], thanks [@astorije])
Expand Down Expand Up @@ -669,6 +670,7 @@ for info on changes for earlier releases.

[#1726]: https://github.com/benmosher/eslint-plugin-import/issues/1726
[#1724]: https://github.com/benmosher/eslint-plugin-import/issues/1724
[#1722]: https://github.com/benmosher/eslint-plugin-import/issues/1722
[#1719]: https://github.com/benmosher/eslint-plugin-import/issues/1719
[#1702]: https://github.com/benmosher/eslint-plugin-import/issues/1702
[#1666]: https://github.com/benmosher/eslint-plugin-import/pull/1666
Expand Down Expand Up @@ -1138,3 +1140,4 @@ for info on changes for earlier releases.
[@forivall]: https://github.com/forivall
[@xpl]: https://github.com/xpl
[@astorije]: https://github.com/astorije
[@Ephem]: https://github.com/Ephem
8 changes: 7 additions & 1 deletion src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ const prepareImportsAndExports = (srcFiles, context) => {
if (isNodeModule(key)) {
return
}
imports.set(key, value.importedSpecifiers)
let localImport = imports.get(key)
if (typeof localImport !== 'undefined') {
localImport = new Set([...localImport, ...value.importedSpecifiers])
} else {
localImport = value.importedSpecifiers
}
imports.set(key, localImport)
})
importList.set(file, imports)

Expand Down
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/import-export-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const a = 5;
export const b = 'b';
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/import-export-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { a } from './import-export-1';
export { b } from './import-export-1';
11 changes: 11 additions & 0 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ ruleTester.run('no-unused-modules', rule, {
],
})

// Test that import and export in the same file both counts as usage
ruleTester.run('no-unused-modules', rule, {
valid: [
test({ options: unusedExportsOptions,
code: `export const a = 5;export const b = 't1'`,
filename: testFilePath('./no-unused-modules/import-export-1.js'),
}),
],
invalid: [],
})

describe('test behaviour for new file', () => {
before(() => {
fs.writeFileSync(testFilePath('./no-unused-modules/file-added-0.js'), '', {encoding: 'utf8'})
Expand Down

0 comments on commit 40d1e67

Please sign in to comment.