Skip to content

Commit

Permalink
[Fix] export: false positive for typescript namespace merging
Browse files Browse the repository at this point in the history
  • Loading branch information
magarcia authored and ljharb committed Jan 27, 2022
1 parent fc98de2 commit e9518c4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -10,6 +10,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`no-named-default`, `no-default-export`, `prefer-default-export`, `no-named-export`, `export`, `named`, `namespace`, `no-unused-modules`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
- [`no-dynamic-require`]: support dynamic import with espree ([#2371], thanks [@sosukesuzuki])

### Fixed
- [`export`]/TypeScript: false positive for typescript namespace merging ([#2375], thanks [@magarcia])

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
- [Tests] `default`, `no-anonymous-default-export`, `no-mutable-exports`, `no-named-as-default-member`, `no-named-as-default`: add tests for arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
Expand Down Expand Up @@ -964,6 +967,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#2375]: https://github.com/import-js/eslint-plugin-import/pull/2375
[#2371]: https://github.com/import-js/eslint-plugin-import/pull/2371
[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367
[#2332]: https://github.com/import-js/eslint-plugin-import/pull/2332
Expand Down Expand Up @@ -1571,6 +1575,7 @@ for info on changes for earlier releases.
[@ludofischer]: https://github.com/ludofischer
[@lukeapage]: https://github.com/lukeapage
[@lydell]: https://github.com/lydell
[@magarcia]: https://github.com/magarcia
[@Mairu]: https://github.com/Mairu
[@malykhinvi]: https://github.com/malykhinvi
[@manovotny]: https://github.com/manovotny
Expand Down Expand Up @@ -1661,4 +1666,4 @@ for info on changes for earlier releases.
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
[@xpl]: https://github.com/xpl
[@yordis]: https://github.com/yordis
[@zloirock]: https://github.com/zloirock
[@zloirock]: https://github.com/zloirock
23 changes: 22 additions & 1 deletion src/rules/export.js
Expand Up @@ -45,6 +45,27 @@ function isTypescriptFunctionOverloads(nodes) {
);
}

/**
* Detect merging Namespaces with Classes, Functions, or Enums like:
* ```ts
* export class Foo { }
* export namespace Foo { }
* ```
* @param {Set<Object>} nodes
* @returns {boolean}
*/
function isTypescriptNamespaceMerging(nodes) {
const types = new Set(Array.from(nodes, (node) => node.parent.type));
return (
types.has('TSModuleDeclaration') &&
(types.size === 1 ||
(types.size === 2 && (
types.has('ClassDeclaration') || types.has('FunctionDeclaration') || types.has('TSEnumDeclaration')
))
)
);
}

module.exports = {
meta: {
type: 'problem',
Expand Down Expand Up @@ -156,7 +177,7 @@ module.exports = {
for (const [name, nodes] of named) {
if (nodes.size <= 1) continue;

if (isTypescriptFunctionOverloads(nodes)) continue;
if (isTypescriptFunctionOverloads(nodes) || isTypescriptNamespaceMerging(nodes)) continue;

for (const node of nodes) {
if (name === 'default') {
Expand Down
21 changes: 21 additions & 0 deletions tests/src/rules/export.js
Expand Up @@ -219,6 +219,27 @@ context('TypeScript', function () {
}
`,
}, parserConfig)),
test(Object.assign({
code: `
export class Foo { }
export namespace Foo { }
export namespace Foo {
export class Bar {}
}
`,
}, parserConfig)),
test(Object.assign({
code: `
export function Foo() { }
export namespace Foo { }
`,
}, parserConfig)),
test(Object.assign({
code: `
export enum Foo { }
export namespace Foo { }
`,
}, parserConfig)),
test(Object.assign({
code: 'export * from "./file1.ts"',
filename: testFilePath('typescript-d-ts/file-2.ts'),
Expand Down

0 comments on commit e9518c4

Please sign in to comment.