From 40d1e6785412892515c0a1800aae8a32494bde9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B6glund?= Date: Mon, 13 Apr 2020 23:20:59 +0200 Subject: [PATCH] [Fix] `no-unused-modules`: Count re-export as usage when used in combination with import --- CHANGELOG.md | 3 +++ src/rules/no-unused-modules.js | 8 +++++++- tests/files/no-unused-modules/import-export-1.js | 2 ++ tests/files/no-unused-modules/import-export-2.js | 2 ++ tests/src/rules/no-unused-modules.js | 11 +++++++++++ 5 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/files/no-unused-modules/import-export-1.js create mode 100644 tests/files/no-unused-modules/import-export-2.js diff --git a/CHANGELOG.md b/CHANGELOG.md index be86fcd50..5525a6f14 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 9468dc87d..60416d21c 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) 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 000000000..218c3cff7 --- /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 000000000..9cfb2747b --- /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 d1a7f62fd..702406316 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'})