From 2352c1fa25e0e73750132925bb3ee790475bd9c9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 13 Mar 2022 13:37:07 -0700 Subject: [PATCH] [Fix] `no-unused-modules`: avoid a crash when processing re-exports Fixes #2388. --- CHANGELOG.md | 2 ++ src/rules/no-unused-modules.js | 6 ++++-- tests/files/unused-modules-reexport-crash/src/App.tsx | 5 +++++ .../files/unused-modules-reexport-crash/src/index.tsx | 3 +++ .../unused-modules-reexport-crash/src/magic/index.js | 1 + .../unused-modules-reexport-crash/src/magic/test.js | 7 +++++++ tests/src/rules/no-unused-modules.js | 11 +++++++++++ 7 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/files/unused-modules-reexport-crash/src/App.tsx create mode 100644 tests/files/unused-modules-reexport-crash/src/index.tsx create mode 100644 tests/files/unused-modules-reexport-crash/src/magic/index.js create mode 100644 tests/files/unused-modules-reexport-crash/src/magic/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c9357bf18..59ab25da95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange - [`default`]: `typescript-eslint-parser`: avoid a crash on exporting as namespace (thanks [@ljharb]) - [`export`]/TypeScript: false positive for typescript namespace merging ([#1964], thanks [@magarcia]) - [`no-duplicates`]: ignore duplicate modules in different TypeScript module declarations ([#2378], thanks [@remcohaszing]) +- [`no-unused-modules`]: avoid a crash when processing re-exports ([#2388], thanks [@ljharb]) ### Changed - [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki]) @@ -973,6 +974,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md [#2393]: https://github.com/import-js/eslint-plugin-import/pull/2393 +[#2388]: https://github.com/import-js/eslint-plugin-import/pull/2388 [#2381]: https://github.com/import-js/eslint-plugin-import/pull/2381 [#2378]: https://github.com/import-js/eslint-plugin-import/pull/2378 [#2371]: https://github.com/import-js/eslint-plugin-import/pull/2371 diff --git a/src/rules/no-unused-modules.js b/src/rules/no-unused-modules.js index efc2169946..5feb319036 100644 --- a/src/rules/no-unused-modules.js +++ b/src/rules/no-unused-modules.js @@ -273,8 +273,10 @@ const prepareImportsAndExports = (srcFiles, context) => { exportAll.forEach((value, key) => { value.forEach(val => { const currentExports = exportList.get(val); - const currentExport = currentExports.get(EXPORT_ALL_DECLARATION); - currentExport.whereUsed.add(key); + if (currentExports) { + const currentExport = currentExports.get(EXPORT_ALL_DECLARATION); + currentExport.whereUsed.add(key); + } }); }); }; diff --git a/tests/files/unused-modules-reexport-crash/src/App.tsx b/tests/files/unused-modules-reexport-crash/src/App.tsx new file mode 100644 index 0000000000..c797a976cb --- /dev/null +++ b/tests/files/unused-modules-reexport-crash/src/App.tsx @@ -0,0 +1,5 @@ +import { hello } from './magic/test' + +hello(); + +export default function App() {}; diff --git a/tests/files/unused-modules-reexport-crash/src/index.tsx b/tests/files/unused-modules-reexport-crash/src/index.tsx new file mode 100644 index 0000000000..124b1745d2 --- /dev/null +++ b/tests/files/unused-modules-reexport-crash/src/index.tsx @@ -0,0 +1,3 @@ +import App from './App'; + +export const x = App \ No newline at end of file diff --git a/tests/files/unused-modules-reexport-crash/src/magic/index.js b/tests/files/unused-modules-reexport-crash/src/magic/index.js new file mode 100644 index 0000000000..ac3f46bb1b --- /dev/null +++ b/tests/files/unused-modules-reexport-crash/src/magic/index.js @@ -0,0 +1 @@ +export * from './test' diff --git a/tests/files/unused-modules-reexport-crash/src/magic/test.js b/tests/files/unused-modules-reexport-crash/src/magic/test.js new file mode 100644 index 0000000000..a6d74afd9b --- /dev/null +++ b/tests/files/unused-modules-reexport-crash/src/magic/test.js @@ -0,0 +1,7 @@ +export function hello() { + console.log('hello!!'); +} + +export function unused() { + console.log('im unused!!'); +} \ No newline at end of file diff --git a/tests/src/rules/no-unused-modules.js b/tests/src/rules/no-unused-modules.js index aa0e123c2b..cc5131e75c 100644 --- a/tests/src/rules/no-unused-modules.js +++ b/tests/src/rules/no-unused-modules.js @@ -156,6 +156,17 @@ ruleTester.run('no-unused-modules', rule, { filename: testFilePath('./no-unused-modules/file-o.js'), parser: parsers.BABEL_OLD, }), + test({ + code: ` + import App from './App'; + `, + filename: testFilePath('./unused-modules-reexport-crash/src/index.tsx'), + parser: parsers.TS_NEW, + options: [{ + unusedExports: true, + ignoreExports: ['**/magic/**'], + }], + }), ], invalid: [ test({