Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] export: false positive for typescript namespace merging #2375

Merged
merged 2 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Expand Up @@ -10,9 +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
- [`default`]: `typescript-eslint-parser`: avoid a crash on exporting as namespace (thanks [@ljharb])
- [`export`]/TypeScript: false positive for typescript namespace merging ([#1964], thanks [@magarcia])

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
Expand Down Expand Up @@ -1575,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 @@ -1665,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
84 changes: 68 additions & 16 deletions src/rules/export.js
Expand Up @@ -36,13 +36,59 @@ const tsTypePrefix = 'type:';
*/
function isTypescriptFunctionOverloads(nodes) {
const types = new Set(Array.from(nodes, node => node.parent.type));
return (
types.has('TSDeclareFunction') &&
(
types.size === 1 ||
(types.size === 2 && types.has('FunctionDeclaration'))
)
);
return types.has('TSDeclareFunction')
&& (
types.size === 1
|| (types.size === 2 && types.has('FunctionDeclaration'))
);
}

/**
* 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));
const noNamespaceNodes = Array.from(nodes).filter((node) => node.parent.type !== 'TSModuleDeclaration');

return types.has('TSModuleDeclaration')
&& (
types.size === 1
// Merging with functions
|| (types.size === 2 && (types.has('FunctionDeclaration') || types.has('TSDeclareFunction')))
|| (types.size === 3 && types.has('FunctionDeclaration') && types.has('TSDeclareFunction'))
// Merging with classes or enums
|| (types.size === 2 && (types.has('ClassDeclaration') || types.has('TSEnumDeclaration')) && noNamespaceNodes.length === 1)
);
}

/**
* Detect if a typescript namespace node should be reported as multiple export:
* ```ts
* export class Foo { }
* export function Foo();
* export namespace Foo { }
* ```
* @param {Object} node
* @param {Set<Object>} nodes
* @returns {boolean}
*/
function shouldSkipTypescriptNamespace(node, nodes) {
const types = new Set(Array.from(nodes, node => node.parent.type));

return !isTypescriptNamespaceMerging(nodes)
&& node.parent.type === 'TSModuleDeclaration'
&& (
types.has('TSEnumDeclaration')
|| types.has('ClassDeclaration')
|| types.has('FunctionDeclaration')
|| types.has('TSDeclareFunction')
);
}

module.exports = {
Expand Down Expand Up @@ -85,15 +131,19 @@ module.exports = {
}

return {
'ExportDefaultDeclaration': (node) => addNamed('default', node, getParent(node)),
ExportDefaultDeclaration(node) {
addNamed('default', node, getParent(node));
},

'ExportSpecifier': (node) => addNamed(
node.exported.name || node.exported.value,
node.exported,
getParent(node.parent),
),
ExportSpecifier(node) {
addNamed(
node.exported.name || node.exported.value,
node.exported,
getParent(node.parent),
);
},

'ExportNamedDeclaration': function (node) {
ExportNamedDeclaration(node) {
if (node.declaration == null) return;

const parent = getParent(node);
Expand All @@ -119,7 +169,7 @@ module.exports = {
}
},

'ExportAllDeclaration': function (node) {
ExportAllDeclaration(node) {
if (node.source == null) return; // not sure if this is ever true

// `export * as X from 'path'` does not conflict
Expand Down Expand Up @@ -156,9 +206,11 @@ 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 (shouldSkipTypescriptNamespace(node, nodes)) continue;

if (name === 'default') {
context.report(node, 'Multiple default exports.');
} else {
Expand Down