Repository: import-js/eslint-plugin-import (JavaScript)

Issue:
[no-unused-modules] Imports overriding reexports
I noticed that `no-unused-modules` with unusedExports trips up on this syntax:

a.js:
```js
export function a() {}

// The following line will wrongly error as unused:
export function aa() {}
```

b.js:
```js
import { a } from './a';

export { aa } from './a';
```

I've tracked this down to [this line](https://github.com/benmosher/eslint-plugin-import/blob/master/src/rules/no-unused-modules.js#L199) in the src, which overwrites any previously detected reexports. The proposed fix is to do something like what is already being done in [these lines](https://github.com/benmosher/eslint-plugin-import/blob/master/src/rules/no-unused-modules.js#L187-L191) to keep both reexported entries and imports.

I can give a shot at throwing together a PR if this looks reasonable?


Discussion:
The example is working as intended. Do you mean 
```diff
- export { aa } from './b';
+ export { aa } from './a';
```
?
Ah, yes, sorry for the typo. I updated the first post to be correct. 😄 Thanks for noticing!
Yes, that seems like a good fix - the code was probably assuming that duplicate imports were always merged.
Yeah, having a import and a reexport from the same file isn't a super common pattern. 😄

I have some code that works, just need to write a test and I'll send a PR within the next few days.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index be86fcd50a..5525a6f14e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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])
@@ -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
@@ -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
diff --git a/src/rules/no-unused-modules.js b/src/rules/no-unused-modules.js
index 9468dc87d0..60416d21cd 100644
--- a/src/rules/no-unused-modules.js
+++ b/src/rules/no-unused-modules.js
@@ -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)
 


Test patch:
diff --git a/tests/files/no-unused-modules/import-export-1.js b/tests/files/no-unused-modules/import-export-1.js
new file mode 100644
index 0000000000..218c3cff7c
--- /dev/null
+++ b/tests/files/no-unused-modules/import-export-1.js
@@ -0,0 +1,2 @@
+export const a = 5;
+export const b = 'b';
diff --git a/tests/files/no-unused-modules/import-export-2.js b/tests/files/no-unused-modules/import-export-2.js
new file mode 100644
index 0000000000..9cfb2747b5
--- /dev/null
+++ b/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';
diff --git a/tests/src/rules/no-unused-modules.js b/tests/src/rules/no-unused-modules.js
index d1a7f62fd3..702406316e 100644
--- a/tests/src/rules/no-unused-modules.js
+++ b/tests/src/rules/no-unused-modules.js
@@ -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'})