|
| 1 | +import { AST_NODE_TYPES } from '@typescript-eslint/utils' |
1 | 2 | import type { TSESTree } from '@typescript-eslint/utils' |
2 | 3 |
|
3 | 4 | import { |
@@ -30,37 +31,24 @@ const rootProgram = 'root' |
30 | 31 | const tsTypePrefix = 'type:' |
31 | 32 |
|
32 | 33 | /** |
33 | | - * Detect function overloads like: |
| 34 | + * Remove function overloads like: |
| 35 | + * |
34 | 36 | * ```ts |
35 | 37 | * export function foo(a: number); |
36 | 38 | * export function foo(a: string); |
37 | 39 | * export function foo(a: number|string) { return a; } |
38 | 40 | * ``` |
39 | 41 | */ |
40 | | -function isTypescriptFunctionOverloads(nodes: Set<TSESTree.Node>) { |
41 | | - const nodesArr = [...nodes] |
42 | | - |
43 | | - const idents = nodesArr.flatMap(node => |
44 | | - 'declaration' in node && node.declaration?.type === 'TSDeclareFunction' |
45 | | - ? node.declaration.id!.name |
46 | | - : [], |
47 | | - ) |
48 | | - |
49 | | - if (new Set(idents).size !== idents.length) { |
50 | | - return true |
51 | | - } |
52 | | - |
53 | | - const types = new Set(nodesArr.map(node => `${node.parent!.type}` as const)) |
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 |
| 42 | +function removeTypescriptFunctionOverloads(nodes: Set<TSESTree.Node>) { |
| 43 | + for (const node of nodes) { |
| 44 | + const declType = |
| 45 | + node.type === AST_NODE_TYPES.ExportDefaultDeclaration |
| 46 | + ? node.declaration.type |
| 47 | + : node.parent?.type |
| 48 | + if (declType === AST_NODE_TYPES.TSDeclareFunction) { |
| 49 | + nodes.delete(node) |
| 50 | + } |
62 | 51 | } |
63 | | - return false |
64 | 52 | } |
65 | 53 |
|
66 | 54 | /** |
@@ -277,14 +265,17 @@ export = createRule<[], MessageId>({ |
277 | 265 | 'Program:exit'() { |
278 | 266 | for (const [, named] of namespace) { |
279 | 267 | for (const [name, nodes] of named) { |
| 268 | + if (nodes.size === 0) { |
| 269 | + continue |
| 270 | + } |
| 271 | + |
| 272 | + removeTypescriptFunctionOverloads(nodes) |
| 273 | + |
280 | 274 | if (nodes.size <= 1) { |
281 | 275 | continue |
282 | 276 | } |
283 | 277 |
|
284 | | - if ( |
285 | | - isTypescriptFunctionOverloads(nodes) || |
286 | | - isTypescriptNamespaceMerging(nodes) |
287 | | - ) { |
| 278 | + if (isTypescriptNamespaceMerging(nodes)) { |
288 | 279 | continue |
289 | 280 | } |
290 | 281 |
|
|
0 commit comments