forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisibility-usage.ts
208 lines (190 loc) · 5.67 KB
/
visibility-usage.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
Interface,
Namespace,
Operation,
Program,
TwoLevelMap,
Type,
ignoreDiagnostics,
navigateTypesInNamespace,
} from "@typespec/compiler";
import {
MetadataInfo,
Visibility,
getHttpOperation,
resolveRequestVisibility,
} from "@typespec/http";
export interface VisibilityUsageTracker {
getUsage(type: Type): Set<Visibility> | undefined;
isUnreachable(type: Type): boolean;
}
export type OperationContainer = Namespace | Interface | Operation;
export function resolveVisibilityUsage(
program: Program,
metadataInfo: MetadataInfo,
root: Namespace,
omitUnreachableTypes: boolean
): VisibilityUsageTracker {
const usages = new Map<Type, Set<Visibility>>();
addUsagesInContainer(program, metadataInfo, root, usages);
const reachableTypes = new Set<Type>(usages.keys());
if (!omitUnreachableTypes) {
// Evaluate all unreferenced types and the types they reference with Visibility.All
const trackType = (type: Type) => {
if (!usages.has(type)) {
navigateReferencedTypes(type, Visibility.All, (type, vis) =>
trackUsageExact(usages, type, vis)
);
}
};
navigateTypesInNamespace(root, {
model: trackType,
scalar: trackType,
enum: trackType,
union: trackType,
});
}
return {
getUsage: (type: Type) => {
const used = usages.get(type);
if (used === undefined) {
return undefined;
}
return usages.get(type);
},
isUnreachable: (type: Type) => {
return !reachableTypes.has(type);
},
};
}
function addUsagesInContainer(
program: Program,
metadataInfo: MetadataInfo,
type: OperationContainer,
usages: Map<Type, Set<Visibility>>
) {
switch (type.kind) {
case "Namespace":
addUsagesInNamespace(program, metadataInfo, type, usages);
break;
case "Interface":
addUsagesInInterface(program, metadataInfo, type, usages);
break;
case "Operation":
addUsagesInOperation(program, metadataInfo, type, usages);
break;
}
}
function trackUsage(
metadataInfo: MetadataInfo,
usages: Map<Type, Set<Visibility>>,
type: Type,
usage: Visibility
) {
const effective = metadataInfo.getEffectivePayloadType(type, usage);
trackUsageExact(usages, type, usage);
if (effective !== type) {
trackUsageExact(usages, effective, usage);
}
}
function trackUsageExact(usages: Map<Type, Set<Visibility>>, type: Type, usage: Visibility) {
const existingFlag = usages.get(type) ?? new Set();
existingFlag.add(usage);
usages.set(type, existingFlag);
}
function addUsagesInNamespace(
program: Program,
metadataInfo: MetadataInfo,
namespace: Namespace,
usages: Map<Type, Set<Visibility>>
): void {
for (const subNamespace of namespace.namespaces.values()) {
addUsagesInNamespace(program, metadataInfo, subNamespace, usages);
}
for (const Interface of namespace.interfaces.values()) {
addUsagesInInterface(program, metadataInfo, Interface, usages);
}
for (const operation of namespace.operations.values()) {
addUsagesInOperation(program, metadataInfo, operation, usages);
}
}
function addUsagesInInterface(
program: Program,
metadataInfo: MetadataInfo,
Interface: Interface,
usages: Map<Type, Set<Visibility>>
): void {
for (const operation of Interface.operations.values()) {
addUsagesInOperation(program, metadataInfo, operation, usages);
}
}
function addUsagesInOperation(
program: Program,
metadataInfo: MetadataInfo,
operation: Operation,
usages: Map<Type, Set<Visibility>>
): void {
const httpOperation = ignoreDiagnostics(getHttpOperation(program, operation));
const visibility = resolveRequestVisibility(program, operation, httpOperation.verb);
if (httpOperation.parameters.body) {
navigateReferencedTypes(httpOperation.parameters.body.type, visibility, (type, vis) =>
trackUsage(metadataInfo, usages, type, vis)
);
}
for (const param of httpOperation.parameters.parameters) {
navigateReferencedTypes(param.param, visibility, (type, vis) =>
trackUsage(metadataInfo, usages, type, vis)
);
}
navigateReferencedTypes(operation.returnType, Visibility.Read, (type, vis) =>
trackUsage(metadataInfo, usages, type, vis)
);
}
function navigateReferencedTypes(
type: Type,
usage: Visibility,
callback: (type: Type, visibility: Visibility) => void,
visited: TwoLevelMap<Type, Visibility, true> = new TwoLevelMap<Type, Visibility, true>()
) {
if (visited.get(type)?.get(usage)) {
return;
}
visited.getOrAdd(type, usage, () => true);
switch (type.kind) {
case "Model":
callback(type, usage);
navigateIterable(type.properties, usage, callback, visited);
navigateIterable(type.derivedModels, usage, callback, visited);
if (type.indexer) {
if (type.indexer.key.name === "integer") {
navigateReferencedTypes(type.indexer.value, usage | Visibility.Item, callback, visited);
} else {
navigateReferencedTypes(type.indexer.value, usage, callback, visited);
}
}
break;
case "ModelProperty":
navigateReferencedTypes(type.type, usage, callback, visited);
break;
case "Union":
callback(type, usage);
navigateIterable(type.variants, usage, callback, visited);
break;
case "UnionVariant":
navigateReferencedTypes(type.type, usage, callback, visited);
break;
default:
callback(type, usage);
break;
}
}
function navigateIterable<T extends Type>(
map: Map<string | symbol, T> | T[],
usage: Visibility,
callback: (type: Type, visibility: Visibility) => void,
visited: TwoLevelMap<Type, Visibility, true>
) {
for (const type of map.values()) {
navigateReferencedTypes(type, usage, callback, visited);
}
}