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

Fix counting re-export as usage when used in combination with import #1727

Merged
merged 1 commit into from Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
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
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
@@ -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
@@ -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
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