From 40ee069f107effe6a8a62f94607e91fad1da4eb6 Mon Sep 17 00:00:00 2001 From: Julien Blatecky Date: Wed, 13 May 2020 18:40:07 +0200 Subject: [PATCH] [Fix] `namespace`/`ExportMap`: Fix interface declarations for TypeScript See also #1528. --- CHANGELOG.md | 2 ++ src/ExportMap.js | 5 ++++- tests/files/typescript-declare-interface.d.ts | 11 +++++++++++ tests/src/rules/namespace.js | 9 +++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/files/typescript-declare-interface.d.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index c5634132e..9d9c2d01b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - [`newline-after-import`]: recognize decorators ([#1139], thanks [@atos1990]) - [`no-unused-modules`]: Revert "[flow] `no-unused-modules`: add flow type support" ([#1770], thanks [@Hypnosphi]) - TypeScript: Add nested namespace handling ([#1763], thanks [@julien1619]) +- [`namespace`]/`ExportMap`: Fix interface declarations for TypeScript ([#1764], thanks [@julien1619]) ### Changed - TypeScript config: Disable [`named`][] ([#1726], thanks [@astorije]) @@ -685,6 +686,7 @@ for info on changes for earlier releases. [#1786]: https://github.com/benmosher/eslint-plugin-import/pull/1786 [#1785]: https://github.com/benmosher/eslint-plugin-import/pull/1785 [#1770]: https://github.com/benmosher/eslint-plugin-import/pull/1770 +[#1764]: https://github.com/benmosher/eslint-plugin-import/pull/1764 [#1763]: https://github.com/benmosher/eslint-plugin-import/pull/1763 [#1736]: https://github.com/benmosher/eslint-plugin-import/pull/1736 [#1726]: https://github.com/benmosher/eslint-plugin-import/pull/1726 diff --git a/src/ExportMap.js b/src/ExportMap.js index b53d252a3..56f9f58b5 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -543,7 +543,10 @@ ExportMap.parse = function (path, content, context) { ] const exportedDecls = ast.body.filter(({ type, id, declarations }) => declTypes.includes(type) && - (id && id.name === exportedName || declarations.find(d => d.id.name === exportedName)) + ( + (id && id.name === exportedName) || + (declarations && declarations.find(d => d.id.name === exportedName)) + ) ) if (exportedDecls.length === 0) { // Export is not referencing any local declaration, must be re-exporting diff --git a/tests/files/typescript-declare-interface.d.ts b/tests/files/typescript-declare-interface.d.ts new file mode 100644 index 000000000..b572b62e9 --- /dev/null +++ b/tests/files/typescript-declare-interface.d.ts @@ -0,0 +1,11 @@ +declare interface foo { + a: string; +} + +declare namespace SomeNamespace { + type foobar = foo & { + b: string; + } +} + +export = SomeNamespace diff --git a/tests/src/rules/namespace.js b/tests/src/rules/namespace.js index cac18c62a..ba8c9145d 100644 --- a/tests/src/rules/namespace.js +++ b/tests/src/rules/namespace.js @@ -133,6 +133,15 @@ const valid = [ }, })), + ...getTSParsers().map((parser) => test({ + code: `import { foobar } from "./typescript-declare-interface"`, + parser: parser, + settings: { + 'import/parsers': { [parser]: ['.ts'] }, + 'import/resolver': { 'eslint-import-resolver-typescript': true }, + }, + })), + ...SYNTAX_CASES, ]