forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.ts
99 lines (91 loc) · 3.23 KB
/
validate.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
import {
DuplicateTracker,
getKeyName,
getTypeName,
listServices,
Model,
navigateTypesInNamespace,
Program,
} from "@typespec/compiler";
import { isSharedRoute } from "@typespec/http";
import { reportDiagnostic } from "./lib.js";
import { getParentResource, getResourceTypeKey, ResourceKey } from "./resource.js";
import { getActionDetails, getCollectionActionDetails } from "./rest.js";
function checkForDuplicateResourceKeyNames(program: Program) {
const seenTypes = new Set<string>();
function checkResourceModelKeys(model: Model) {
if (model.name === "") {
return;
}
let currentType: Model | undefined = model;
const keyProperties = new DuplicateTracker<string, ResourceKey>();
while (currentType) {
const resourceKey = getResourceTypeKey(program, currentType);
if (resourceKey) {
const keyName = getKeyName(program, resourceKey.keyProperty);
keyProperties.track(keyName, resourceKey);
}
currentType = getParentResource(program, currentType);
}
// Report a diagnostic for each duplicate key
for (const [keyName, dupes] of keyProperties.entries()) {
for (const key of dupes) {
// Make sure we don't report a duplicate for a particular
// resource type and key name more than once
const fullName = `${getTypeName(key.resourceType)}.${keyName}`;
if (!seenTypes.has(fullName)) {
seenTypes.add(fullName);
reportDiagnostic(program, {
code: "duplicate-parent-key",
format: {
resourceName: key.resourceType.name,
keyName,
},
target: key.keyProperty,
});
}
}
}
}
for (const service of listServices(program)) {
// If the model type is defined under the service namespace, check that the
// parent resource type(s) don't have the same key name as the
// current resource type.
navigateTypesInNamespace(service.type, {
model: (model) => checkResourceModelKeys(model),
});
}
}
function checkForSharedRouteUnnamedActions(program: Program) {
for (const service of listServices(program)) {
// If the model type is defined under the service namespace, check that the
// parent resource type(s) don't have the same key name as the
// current resource type.
navigateTypesInNamespace(service.type, {
operation: (op) => {
const actionDetails = getActionDetails(program, op);
if (
isSharedRoute(program, op) &&
(actionDetails?.kind === "automatic" ||
getCollectionActionDetails(program, op)?.kind === "automatic")
) {
reportDiagnostic(program, {
code: "shared-route-unspecified-action-name",
target: op,
format: {
decoratorName: actionDetails ? "@action" : "@collectionAction",
},
});
}
},
});
}
}
export function $onValidate(program: Program) {
// Make sure any defined resource types don't have any conflicts with parent
// resource type key names
checkForDuplicateResourceKeyNames(program);
// Ensure that all shared routes with an `@action` decorator have an
// explicitly named action
checkForSharedRouteUnnamedActions(program);
}