@@ -26,47 +26,25 @@ const rootProgram = 'root';
2626const 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 ; }
0 commit comments