/
removeTypeDuplicates.ts
93 lines (77 loc) 路 2.1 KB
/
removeTypeDuplicates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
isAnyTypeAnnotation,
isGenericTypeAnnotation,
isUnionTypeAnnotation,
isFlowBaseAnnotation,
isIdentifier,
} from "../../validators/generated";
import type * as t from "../..";
function getQualifiedName(node: t.GenericTypeAnnotation["id"]) {
return isIdentifier(node)
? node.name
: `${node.id.name}.${getQualifiedName(node.qualification)}`;
}
/**
* Dedupe type annotations.
*/
export default function removeTypeDuplicates(
nodes: ReadonlyArray<t.FlowType | false | null | undefined>,
): t.FlowType[] {
const generics = {};
const bases = {};
// store union type groups to circular references
const typeGroups = [];
const types = [];
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (!node) continue;
// detect duplicates
if (types.indexOf(node) >= 0) {
continue;
}
// this type matches anything
if (isAnyTypeAnnotation(node)) {
return [node];
}
if (isFlowBaseAnnotation(node)) {
bases[node.type] = node;
continue;
}
if (isUnionTypeAnnotation(node)) {
if (typeGroups.indexOf(node.types) < 0) {
Array.prototype.push.apply(nodes, node.types);
typeGroups.push(node.types);
}
continue;
}
// find a matching generic type and merge and deduplicate the type parameters
if (isGenericTypeAnnotation(node)) {
const name = getQualifiedName(node.id);
if (generics[name]) {
let existing = generics[name];
if (existing.typeParameters) {
if (node.typeParameters) {
existing.typeParameters.params = removeTypeDuplicates(
existing.typeParameters.params.concat(node.typeParameters.params),
);
}
} else {
existing = node.typeParameters;
}
} else {
generics[name] = node;
}
continue;
}
types.push(node);
}
// add back in bases
for (const type of Object.keys(bases)) {
types.push(bases[type]);
}
// add back in generics
for (const name of Object.keys(generics)) {
types.push(generics[name]);
}
return types;
}