fork of hey-api/openapi-ts because I need some additional things
1import type { Language } from '../languages/types';
2import type { SymbolKind } from '../symbols/types';
3
4const typescriptMergeKindRank: Record<SymbolKind, number> = {
5 class: 3,
6 enum: 4,
7 function: 5,
8 interface: 1,
9 namespace: 0,
10 type: 2,
11 var: 6,
12};
13
14/**
15 * Returns true if two declarations of given kinds
16 * are allowed to share the same identifier in TypeScript.
17 */
18function canTypeScriptDeclarationsShareIdentifier(a: SymbolKind, b: SymbolKind): boolean {
19 // sort based on TypeScript merge precedence so `a` is always the weaker merge candidate
20 // ensures that asymmetric merges like `type + var` are correctly handled
21 if (typescriptMergeKindRank[a] > typescriptMergeKindRank[b]) {
22 [a, b] = [b, a];
23 }
24
25 switch (a) {
26 case 'interface':
27 return b === 'class' || b === 'interface';
28 case 'namespace':
29 return b === 'class' || b === 'enum' || b === 'function' || b === 'namespace';
30 case 'type':
31 // type can only merge with value-only declarations
32 return b === 'function' || b === 'var';
33 default:
34 return false;
35 }
36}
37
38export function canDeclarationsShareIdentifier(
39 language: Language | undefined,
40 a: SymbolKind,
41 b: SymbolKind,
42): boolean {
43 if (language === 'typescript') {
44 return canTypeScriptDeclarationsShareIdentifier(a, b);
45 }
46
47 return false;
48}