Skip to content

Commit a9815da

Browse files
liuxingbaoyuljharb
authored andcommitted
[Fix] export: False positive for exported overloaded functions in TS
1 parent 91f944d commit a9815da

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1212
### Fixed
1313
- `ExportMap` / flat config: include `languageOptions` in context ([#3052], thanks [@michaelfaith])
1414
- [`no-named-as-default`]: Allow using an identifier if the export is both a named and a default export ([#3032], thanks [@akwodkiewicz])
15+
- [`export`]: False positive for exported overloaded functions in TS ([#3065], thanks [@liuxingbaoyu])
1516

1617
### Changed
1718
- [Docs] [`no-relative-packages`]: fix typo ([#3066], thanks [@joshuaobrien])
@@ -1140,6 +1141,7 @@ for info on changes for earlier releases.
11401141
[`memo-parser`]: ./memo-parser/README.md
11411142

11421143
[#3066]: https://github.com/import-js/eslint-plugin-import/pull/3066
1144+
[#3065]: https://github.com/import-js/eslint-plugin-import/pull/3065
11431145
[#3052]: https://github.com/import-js/eslint-plugin-import/pull/3052
11441146
[#3043]: https://github.com/import-js/eslint-plugin-import/pull/3043
11451147
[#3036]: https://github.com/import-js/eslint-plugin-import/pull/3036
@@ -1875,6 +1877,7 @@ for info on changes for earlier releases.
18751877
[@lilling]: https://github.com/lilling
18761878
[@ljharb]: https://github.com/ljharb
18771879
[@ljqx]: https://github.com/ljqx
1880+
[@liuxingbaoyu]: https://github.com/liuxingbaoyu
18781881
[@lo1tuma]: https://github.com/lo1tuma
18791882
[@loganfsmyth]: https://github.com/loganfsmyth
18801883
[@luczsoma]: https://github.com/luczsoma

src/rules/export.js

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import ExportMapBuilder from '../exportMap/builder';
22
import recursivePatternCapture from '../exportMap/patternCapture';
33
import docsUrl from '../docsUrl';
44
import includes from 'array-includes';
5-
import flatMap from 'array.prototype.flatmap';
65

76
/*
87
Notes on TypeScript namespaces aka TSModuleDeclaration:
@@ -27,42 +26,25 @@ const rootProgram = 'root';
2726
const tsTypePrefix = 'type:';
2827

2928
/**
30-
* Detect function overloads like:
29+
* remove function overloads like:
3130
* ```ts
3231
* export function foo(a: number);
3332
* export function foo(a: string);
34-
* export function foo(a: number|string) { return a; }
3533
* ```
3634
* @param {Set<Object>} nodes
37-
* @returns {boolean}
3835
*/
39-
function isTypescriptFunctionOverloads(nodes) {
40-
const nodesArr = Array.from(nodes);
41-
42-
const idents = flatMap(
43-
nodesArr,
44-
(node) => node.declaration && (
45-
node.declaration.type === 'TSDeclareFunction' // eslint 6+
46-
|| node.declaration.type === 'TSEmptyBodyFunctionDeclaration' // eslint 4-5
47-
)
48-
? node.declaration.id.name
49-
: [],
50-
);
51-
if (new Set(idents).size !== idents.length) {
52-
return true;
53-
}
54-
55-
const types = new Set(nodesArr.map((node) => node.parent.type));
56-
if (!types.has('TSDeclareFunction')) {
57-
return false;
58-
}
59-
if (types.size === 1) {
60-
return true;
61-
}
62-
if (types.size === 2 && types.has('FunctionDeclaration')) {
63-
return true;
64-
}
65-
return false;
36+
function removeTypescriptFunctionOverloads(nodes) {
37+
nodes.forEach((node) => {
38+
const declType = node.type === 'ExportDefaultDeclaration' ? node.declaration.type : node.parent.type;
39+
if (
40+
// eslint 6+
41+
declType === 'TSDeclareFunction'
42+
// eslint 4-5
43+
|| declType === 'TSEmptyBodyFunctionDeclaration'
44+
) {
45+
nodes.delete(node);
46+
}
47+
});
6648
}
6749

6850
/**
@@ -227,9 +209,11 @@ module.exports = {
227209
'Program:exit'() {
228210
for (const [, named] of namespace) {
229211
for (const [name, nodes] of named) {
212+
removeTypescriptFunctionOverloads(nodes);
213+
230214
if (nodes.size <= 1) { continue; }
231215

232-
if (isTypescriptFunctionOverloads(nodes) || isTypescriptNamespaceMerging(nodes)) { continue; }
216+
if (isTypescriptNamespaceMerging(nodes)) { continue; }
233217

234218
for (const node of nodes) {
235219
if (shouldSkipTypescriptNamespace(node, nodes)) { continue; }

tests/src/rules/export.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ ruleTester.run('export', rule, {
5656
`,
5757
parser,
5858
})),
59+
getTSParsers().map((parser) => ({
60+
code: `
61+
export default function foo(param: string): boolean;
62+
export default function foo(param: string, param1?: number): boolean {
63+
return param && param1;
64+
}
65+
`,
66+
parser,
67+
})),
5968
),
6069

6170
invalid: [].concat(
@@ -154,6 +163,19 @@ ruleTester.run('export', rule, {
154163
ecmaVersion: 2022,
155164
},
156165
})),
166+
167+
getTSParsers().map((parser) => ({
168+
code: `
169+
export default function a(): void;
170+
export default function a() {}
171+
export { x as default };
172+
`,
173+
errors: [
174+
'Multiple default exports.',
175+
'Multiple default exports.',
176+
],
177+
parser,
178+
})),
157179
),
158180
});
159181

@@ -510,7 +532,7 @@ context('TypeScript', function () {
510532
}),
511533
test({
512534
code: `
513-
export function Foo();
535+
export function Foo() { };
514536
export class Foo { }
515537
export namespace Foo { }
516538
`,
@@ -529,7 +551,7 @@ context('TypeScript', function () {
529551
test({
530552
code: `
531553
export const Foo = 'bar';
532-
export function Foo();
554+
export function Foo() { };
533555
export namespace Foo { }
534556
`,
535557
errors: [

0 commit comments

Comments
 (0)