Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Sep 24, 2024
1 parent 7f7fbd3 commit 81d0e44
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
50 changes: 15 additions & 35 deletions src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,25 @@ const rootProgram = 'root';
const tsTypePrefix = 'type:';

/**
* Detect function overloads like:
* remove function overloads like:
* ```ts
* export function foo(a: number);
* export function foo(a: string);
* export function foo(a: number|string) { return a; }
* ```
* @param {Set<Object>} nodes
* @returns {boolean}
*/
function isTypescriptFunctionOverloads(nodes) {
const nodesArr = Array.from(nodes);

if (nodesArr[0].type === 'ExportDefaultDeclaration') {
let num = 0;
for (let i = 0; i < nodesArr.length; i++) {
const type = nodesArr[i].declaration.type;
if (
// eslint 6+
type === 'TSDeclareFunction'
// eslint 4-5
|| type === 'TSEmptyBodyFunctionDeclaration'
) {
num++;
}
function removeTypescriptFunctionOverloads(nodes) {
nodes.forEach((node) => {
const declType = node.type === 'ExportDefaultDeclaration' ? node.declaration.type : node.parent.type;
if (
// eslint 6+
declType === 'TSDeclareFunction'
// eslint 4-5
|| declType === 'TSEmptyBodyFunctionDeclaration'
) {
nodes.delete(node);
}
if (num === nodesArr.length - 1) {
return true;
}
}

const types = new Set(nodesArr.map((node) => node.parent.type));
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 Expand Up @@ -231,9 +209,11 @@ module.exports = {
'Program:exit'() {
for (const [, named] of namespace) {
for (const [name, nodes] of named) {
removeTypescriptFunctionOverloads(nodes);

if (nodes.size <= 1) { continue; }

if (isTypescriptFunctionOverloads(nodes) || isTypescriptNamespaceMerging(nodes)) { continue; }
if (isTypescriptNamespaceMerging(nodes)) { continue; }

for (const node of nodes) {
if (shouldSkipTypescriptNamespace(node, nodes)) { continue; }
Expand Down
17 changes: 15 additions & 2 deletions tests/src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ ruleTester.run('export', rule, {
ecmaVersion: 2022,
},
})),

getTSParsers().map((parser) => ({
code: `
export default function a(): void;
export default function a() {}
export { x as default };
`,
errors: [
'Multiple default exports.',
'Multiple default exports.',
],
parser,
})),
),
});

Expand Down Expand Up @@ -519,7 +532,7 @@ context('TypeScript', function () {
}),
test({
code: `
export function Foo();
export function Foo() { };
export class Foo { }
export namespace Foo { }
`,
Expand All @@ -538,7 +551,7 @@ context('TypeScript', function () {
test({
code: `
export const Foo = 'bar';
export function Foo();
export function Foo() { };
export namespace Foo { }
`,
errors: [
Expand Down

0 comments on commit 81d0e44

Please sign in to comment.