From aa8d566b288eb0fdce24f3a51f8a65cf520c2133 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 22 Aug 2021 08:54:35 -0700 Subject: [PATCH] [Fix] `no-duplicates`: avoid crash with empty `import type {}` Fixes #2201 --- CHANGELOG.md | 2 ++ src/rules/no-duplicates.js | 2 +- tests/src/rules/no-duplicates.js | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4991b7a9a..59bcfb4be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - `ExportMap`: Add default export when esModuleInterop is true and anything is exported ([#2184], thanks [@Maxim-Mazurok]) - [`named`], [`namespace`]: properly set reexports on `export * as … from` ([#1998], [#2161], thanks [@ljharb]) - [`no-duplicates`]: correctly handle case of mixed default/named type imports ([#2149], thanks [@GoodForOneFare], [@nwalters512]) +- [`no-duplicates`]: avoid crash with empty `import type {}` ([#2201], thanks [@ljharb]) ### Changed - [Docs] `max-dependencies`: 📖 Document `ignoreTypeImports` option ([#2196], thanks [@himynameisdave]) @@ -1150,6 +1151,7 @@ for info on changes for earlier releases. [#211]: https://github.com/import-js/eslint-plugin-import/pull/211 [#164]: https://github.com/import-js/eslint-plugin-import/pull/164 [#157]: https://github.com/import-js/eslint-plugin-import/pull/157 +[#2201]: https://github.com/import-js/eslint-plugin-import/issues/2201 [#2161]: https://github.com/import-js/eslint-plugin-import/issues/2161 [#2118]: https://github.com/import-js/eslint-plugin-import/issues/2118 [#2067]: https://github.com/import-js/eslint-plugin-import/issues/2067 diff --git a/src/rules/no-duplicates.js b/src/rules/no-duplicates.js index 634410197..6e03fcaef 100644 --- a/src/rules/no-duplicates.js +++ b/src/rules/no-duplicates.js @@ -281,7 +281,7 @@ module.exports = { function getImportMap(n) { if (n.importKind === 'type') { - return n.specifiers[0].type === 'ImportDefaultSpecifier' ? defaultTypesImported : namedTypesImported; + return n.specifiers.length > 0 && n.specifiers[0].type === 'ImportDefaultSpecifier' ? defaultTypesImported : namedTypesImported; } return hasNamespace(n) ? nsImported : imported; diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index c6b355ab8..faf096ace 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -446,6 +446,13 @@ context('TypeScript', function() { code: "import type x from './foo'; import type {y} from './foo'", ...parserConfig, }), + test({ + code: ` + import type {} from './module'; + import {} from './module2'; + `, + ...parserConfig, + }), ], invalid: [ test({