Skip to content

Commit 04e114b

Browse files
committed
[Fix] export: do not error on TS export overloads
Fixes #1590
1 parent 9f401a8 commit 04e114b

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2121
- [`order`]: leave more space in rankings for consecutive path groups ([#2506], thanks [@Pearce-Ropion])
2222
- [`no-cycle`]: add ExportNamedDeclaration statements to dependencies ([#2511], thanks [@BenoitZugmeyer])
2323
- [`dynamic-import-chunkname`]: prevent false report on a valid webpack magic comment ([#2330], thanks [@mhmadhamster])
24+
- [`export`]: do not error on TS export overloads ([#1590], thanks [@ljharb])
2425

2526
### Changed
2627
- [Tests] [`named`]: Run all TypeScript test ([#2427], thanks [@ProdigySim])
@@ -1341,6 +1342,7 @@ for info on changes for earlier releases.
13411342
[#1631]: https://github.com/import-js/eslint-plugin-import/issues/1631
13421343
[#1616]: https://github.com/import-js/eslint-plugin-import/issues/1616
13431344
[#1613]: https://github.com/import-js/eslint-plugin-import/issues/1613
1345+
[#1590]: https://github.com/import-js/eslint-plugin-import/issues/1590
13441346
[#1589]: https://github.com/import-js/eslint-plugin-import/issues/1589
13451347
[#1565]: https://github.com/import-js/eslint-plugin-import/issues/1565
13461348
[#1366]: https://github.com/import-js/eslint-plugin-import/issues/1366

src/rules/export.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ExportMap, { recursivePatternCapture } from '../ExportMap';
22
import docsUrl from '../docsUrl';
33
import includes from 'array-includes';
4+
import flatMap from 'array.prototype.flatmap';
45

56
/*
67
Notes on TypeScript namespaces aka TSModuleDeclaration:
@@ -35,12 +36,31 @@ const tsTypePrefix = 'type:';
3536
* @returns {boolean}
3637
*/
3738
function isTypescriptFunctionOverloads(nodes) {
38-
const types = new Set(Array.from(nodes, node => node.parent.type));
39-
return types.has('TSDeclareFunction')
40-
&& (
41-
types.size === 1
42-
|| (types.size === 2 && types.has('FunctionDeclaration'))
43-
);
39+
const nodesArr = Array.from(nodes);
40+
const types = new Set(nodesArr.map(node => node.parent.type));
41+
42+
const idents = flatMap(nodesArr, (node) => (
43+
node.declaration && (
44+
node.declaration.type === 'TSDeclareFunction' // eslint 6+
45+
|| node.declaration.type === 'TSEmptyBodyFunctionDeclaration' // eslint 4-5
46+
)
47+
? node.declaration.id.name
48+
: []
49+
));
50+
if (new Set(idents).size !== idents.length) {
51+
return true;
52+
}
53+
54+
if (!types.has('TSDeclareFunction')) {
55+
return false;
56+
}
57+
if (types.size === 1) {
58+
return true;
59+
}
60+
if (types.size === 2 && types.has('FunctionDeclaration')) {
61+
return true;
62+
}
63+
return false;
4464
}
4565

4666
/**

tests/src/rules/export.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ ruleTester.run('export', rule, {
4545
ecmaVersion: 2020,
4646
},
4747
})) || [],
48+
49+
getTSParsers().map((parser) => ({
50+
code: `
51+
export default function foo(param: string): boolean;
52+
export default function foo(param: string, param1: number): boolean;
53+
export default function foo(param: string, param1?: number): boolean {
54+
return param && param1;
55+
}
56+
`,
57+
parser,
58+
})),
4859
),
4960

5061
invalid: [].concat(

0 commit comments

Comments
 (0)