Skip to content

Commit

Permalink
[Fix] export: do not error on TS export overloads
Browse files Browse the repository at this point in the history
Fixes #1590
  • Loading branch information
ljharb committed Aug 28, 2022
1 parent 9f401a8 commit 04e114b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`order`]: leave more space in rankings for consecutive path groups ([#2506], thanks [@Pearce-Ropion])
- [`no-cycle`]: add ExportNamedDeclaration statements to dependencies ([#2511], thanks [@BenoitZugmeyer])
- [`dynamic-import-chunkname`]: prevent false report on a valid webpack magic comment ([#2330], thanks [@mhmadhamster])
- [`export`]: do not error on TS export overloads ([#1590], thanks [@ljharb])

### Changed
- [Tests] [`named`]: Run all TypeScript test ([#2427], thanks [@ProdigySim])
Expand Down Expand Up @@ -1341,6 +1342,7 @@ for info on changes for earlier releases.
[#1631]: https://github.com/import-js/eslint-plugin-import/issues/1631
[#1616]: https://github.com/import-js/eslint-plugin-import/issues/1616
[#1613]: https://github.com/import-js/eslint-plugin-import/issues/1613
[#1590]: https://github.com/import-js/eslint-plugin-import/issues/1590
[#1589]: https://github.com/import-js/eslint-plugin-import/issues/1589
[#1565]: https://github.com/import-js/eslint-plugin-import/issues/1565
[#1366]: https://github.com/import-js/eslint-plugin-import/issues/1366
Expand Down
32 changes: 26 additions & 6 deletions src/rules/export.js
@@ -1,6 +1,7 @@
import ExportMap, { recursivePatternCapture } from '../ExportMap';
import docsUrl from '../docsUrl';
import includes from 'array-includes';
import flatMap from 'array.prototype.flatmap';

/*
Notes on TypeScript namespaces aka TSModuleDeclaration:
Expand Down Expand Up @@ -35,12 +36,31 @@ const tsTypePrefix = 'type:';
* @returns {boolean}
*/
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'))
);
const nodesArr = Array.from(nodes);
const types = new Set(nodesArr.map(node => node.parent.type));

const idents = flatMap(nodesArr, (node) => (
node.declaration && (
node.declaration.type === 'TSDeclareFunction' // eslint 6+
|| node.declaration.type === 'TSEmptyBodyFunctionDeclaration' // eslint 4-5
)
? node.declaration.id.name
: []
));
if (new Set(idents).size !== idents.length) {
return true;
}

if (!types.has('TSDeclareFunction')) {
return false;
}
if (types.size === 1) {
return true;
}
if (types.size === 2 && types.has('FunctionDeclaration')) {
return true;
}
return false;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/src/rules/export.js
Expand Up @@ -45,6 +45,17 @@ ruleTester.run('export', rule, {
ecmaVersion: 2020,
},
})) || [],

getTSParsers().map((parser) => ({
code: `
export default function foo(param: string): boolean;
export default function foo(param: string, param1: number): boolean;
export default function foo(param: string, param1?: number): boolean {
return param && param1;
}
`,
parser,
})),
),

invalid: [].concat(
Expand Down

0 comments on commit 04e114b

Please sign in to comment.