Skip to content

Commit

Permalink
[Fix] no-unused-modules: avoid a crash when processing re-exports
Browse files Browse the repository at this point in the history
Fixes #2388.
  • Loading branch information
ljharb committed Mar 13, 2022
1 parent 747d6dc commit 8b7000e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
};
Expand Down
5 changes: 5 additions & 0 deletions tests/files/unused-modules-reexport-crash/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { hello } from './magic/test'

hello();

export default function App() {};
3 changes: 3 additions & 0 deletions tests/files/unused-modules-reexport-crash/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import App from './App';

export const x = App
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './test'
7 changes: 7 additions & 0 deletions tests/files/unused-modules-reexport-crash/src/magic/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function hello() {
console.log('hello!!');
}

export function unused() {
console.log('im unused!!');
}
13 changes: 12 additions & 1 deletion tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ruleTester.run('no-unused-modules', rule, {
});


// tests for exports
// tests for exports
ruleTester.run('no-unused-modules', rule, {
valid: [
test({
Expand Down Expand Up @@ -301,6 +301,17 @@ describe('dynamic imports', () => {
parser: parsers.TS_NEW,
filename: testFilePath('./no-unused-modules/typescript/exports-for-dynamic-ts.ts'),
}),
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: [
],
Expand Down

0 comments on commit 8b7000e

Please sign in to comment.