Skip to content

Commit 40d1e67

Browse files
committed
[Fix] no-unused-modules: Count re-export as usage when used in combination with import
1 parent 3b4487c commit 40d1e67

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
99
### Fixed
1010
- [`group-exports`]: Flow type export awareness ([#1702], thanks [@ernestostifano])
1111
- [`order`]: Recognize pathGroup config for first group ([#1719], [#1724], thanks [@forivall], [@xpl])
12+
- [`no-unused-modules`]: Fix re-export not counting as usage when used in combination with import ([#1722], thanks [@Ephem])
1213

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

670671
[#1726]: https://github.com/benmosher/eslint-plugin-import/issues/1726
671672
[#1724]: https://github.com/benmosher/eslint-plugin-import/issues/1724
673+
[#1722]: https://github.com/benmosher/eslint-plugin-import/issues/1722
672674
[#1719]: https://github.com/benmosher/eslint-plugin-import/issues/1719
673675
[#1702]: https://github.com/benmosher/eslint-plugin-import/issues/1702
674676
[#1666]: https://github.com/benmosher/eslint-plugin-import/pull/1666
@@ -1138,3 +1140,4 @@ for info on changes for earlier releases.
11381140
[@forivall]: https://github.com/forivall
11391141
[@xpl]: https://github.com/xpl
11401142
[@astorije]: https://github.com/astorije
1143+
[@Ephem]: https://github.com/Ephem

src/rules/no-unused-modules.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ const prepareImportsAndExports = (srcFiles, context) => {
196196
if (isNodeModule(key)) {
197197
return
198198
}
199-
imports.set(key, value.importedSpecifiers)
199+
let localImport = imports.get(key)
200+
if (typeof localImport !== 'undefined') {
201+
localImport = new Set([...localImport, ...value.importedSpecifiers])
202+
} else {
203+
localImport = value.importedSpecifiers
204+
}
205+
imports.set(key, localImport)
200206
})
201207
importList.set(file, imports)
202208

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const a = 5;
2+
export const b = 'b';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { a } from './import-export-1';
2+
export { b } from './import-export-1';

tests/src/rules/no-unused-modules.js

+11
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,17 @@ ruleTester.run('no-unused-modules', rule, {
427427
],
428428
})
429429

430+
// Test that import and export in the same file both counts as usage
431+
ruleTester.run('no-unused-modules', rule, {
432+
valid: [
433+
test({ options: unusedExportsOptions,
434+
code: `export const a = 5;export const b = 't1'`,
435+
filename: testFilePath('./no-unused-modules/import-export-1.js'),
436+
}),
437+
],
438+
invalid: [],
439+
})
440+
430441
describe('test behaviour for new file', () => {
431442
before(() => {
432443
fs.writeFileSync(testFilePath('./no-unused-modules/file-added-0.js'), '', {encoding: 'utf8'})

0 commit comments

Comments
 (0)