Skip to content

Commit 8b7000e

Browse files
committed
[Fix] no-unused-modules: avoid a crash when processing re-exports
Fixes #2388.
1 parent 747d6dc commit 8b7000e

File tree

7 files changed

+34
-3
lines changed

7 files changed

+34
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1515
- [`default`]: `typescript-eslint-parser`: avoid a crash on exporting as namespace (thanks [@ljharb])
1616
- [`export`]/TypeScript: false positive for typescript namespace merging ([#1964], thanks [@magarcia])
1717
- [`no-duplicates`]: ignore duplicate modules in different TypeScript module declarations ([#2378], thanks [@remcohaszing])
18+
- [`no-unused-modules`]: avoid a crash when processing re-exports ([#2388], thanks [@ljharb])
1819

1920
### Changed
2021
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
@@ -973,6 +974,7 @@ for info on changes for earlier releases.
973974
[`memo-parser`]: ./memo-parser/README.md
974975

975976
[#2393]: https://github.com/import-js/eslint-plugin-import/pull/2393
977+
[#2388]: https://github.com/import-js/eslint-plugin-import/pull/2388
976978
[#2381]: https://github.com/import-js/eslint-plugin-import/pull/2381
977979
[#2378]: https://github.com/import-js/eslint-plugin-import/pull/2378
978980
[#2371]: https://github.com/import-js/eslint-plugin-import/pull/2371

src/rules/no-unused-modules.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ const prepareImportsAndExports = (srcFiles, context) => {
273273
exportAll.forEach((value, key) => {
274274
value.forEach(val => {
275275
const currentExports = exportList.get(val);
276-
const currentExport = currentExports.get(EXPORT_ALL_DECLARATION);
277-
currentExport.whereUsed.add(key);
276+
if (currentExports) {
277+
const currentExport = currentExports.get(EXPORT_ALL_DECLARATION);
278+
currentExport.whereUsed.add(key);
279+
}
278280
});
279281
});
280282
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { hello } from './magic/test'
2+
3+
hello();
4+
5+
export default function App() {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import App from './App';
2+
3+
export const x = App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './test'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function hello() {
2+
console.log('hello!!');
3+
}
4+
5+
export function unused() {
6+
console.log('im unused!!');
7+
}

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ ruleTester.run('no-unused-modules', rule, {
105105
});
106106

107107

108-
// tests for exports
108+
// tests for exports
109109
ruleTester.run('no-unused-modules', rule, {
110110
valid: [
111111
test({
@@ -301,6 +301,17 @@ describe('dynamic imports', () => {
301301
parser: parsers.TS_NEW,
302302
filename: testFilePath('./no-unused-modules/typescript/exports-for-dynamic-ts.ts'),
303303
}),
304+
test({
305+
code: `
306+
import App from './App';
307+
`,
308+
filename: testFilePath('./unused-modules-reexport-crash/src/index.tsx'),
309+
parser: parsers.TS_NEW,
310+
options: [{
311+
unusedExports: true,
312+
ignoreExports: ['**/magic/**'],
313+
}],
314+
}),
304315
],
305316
invalid: [
306317
],

0 commit comments

Comments
 (0)