Skip to content

Commit 81d0e44

Browse files
committed
review
1 parent 7f7fbd3 commit 81d0e44

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

src/rules/export.js

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,25 @@ const rootProgram = 'root';
2626
const tsTypePrefix = 'type:';
2727

2828
/**
29-
* Detect function overloads like:
29+
* remove function overloads like:
3030
* ```ts
3131
* export function foo(a: number);
3232
* export function foo(a: string);
33-
* export function foo(a: number|string) { return a; }
3433
* ```
3534
* @param {Set<Object>} nodes
36-
* @returns {boolean}
3735
*/
38-
function isTypescriptFunctionOverloads(nodes) {
39-
const nodesArr = Array.from(nodes);
40-
41-
if (nodesArr[0].type === 'ExportDefaultDeclaration') {
42-
let num = 0;
43-
for (let i = 0; i < nodesArr.length; i++) {
44-
const type = nodesArr[i].declaration.type;
45-
if (
46-
// eslint 6+
47-
type === 'TSDeclareFunction'
48-
// eslint 4-5
49-
|| type === 'TSEmptyBodyFunctionDeclaration'
50-
) {
51-
num++;
52-
}
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);
5346
}
54-
if (num === nodesArr.length - 1) {
55-
return true;
56-
}
57-
}
58-
59-
const types = new Set(nodesArr.map((node) => node.parent.type));
60-
if (!types.has('TSDeclareFunction')) {
61-
return false;
62-
}
63-
if (types.size === 1) {
64-
return true;
65-
}
66-
if (types.size === 2 && types.has('FunctionDeclaration')) {
67-
return true;
68-
}
69-
return false;
47+
});
7048
}
7149

7250
/**
@@ -231,9 +209,11 @@ module.exports = {
231209
'Program:exit'() {
232210
for (const [, named] of namespace) {
233211
for (const [name, nodes] of named) {
212+
removeTypescriptFunctionOverloads(nodes);
213+
234214
if (nodes.size <= 1) { continue; }
235215

236-
if (isTypescriptFunctionOverloads(nodes) || isTypescriptNamespaceMerging(nodes)) { continue; }
216+
if (isTypescriptNamespaceMerging(nodes)) { continue; }
237217

238218
for (const node of nodes) {
239219
if (shouldSkipTypescriptNamespace(node, nodes)) { continue; }

tests/src/rules/export.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ ruleTester.run('export', rule, {
163163
ecmaVersion: 2022,
164164
},
165165
})),
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+
})),
166179
),
167180
});
168181

@@ -519,7 +532,7 @@ context('TypeScript', function () {
519532
}),
520533
test({
521534
code: `
522-
export function Foo();
535+
export function Foo() { };
523536
export class Foo { }
524537
export namespace Foo { }
525538
`,
@@ -538,7 +551,7 @@ context('TypeScript', function () {
538551
test({
539552
code: `
540553
export const Foo = 'bar';
541-
export function Foo();
554+
export function Foo() { };
542555
export namespace Foo { }
543556
`,
544557
errors: [

0 commit comments

Comments
 (0)