forked from web-platform-dx/web-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertions.ts
More file actions
61 lines (58 loc) · 1.69 KB
/
assertions.ts
File metadata and controls
61 lines (58 loc) · 1.69 KB
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
import { isOrdinaryFeatureData } from "./type-guards";
import { FeatureData } from "./types";
import { WebFeaturesData } from "./types.quicktype";
/**
* Assert that a reference from one feature to another is an ordinary feature
* reference (i.e., it's defined and not some kind of redirect).
*
* @export
* @param {string} sourceId The feature that is referencing another feature
* @param {string} targetId The feature being referenced
* @param {WebFeaturesData["features"]} features Feature data
*/
export function assertValidFeatureReference(
sourceId: string,
targetId: string,
features: WebFeaturesData["features"],
) {
const target: unknown = features[targetId];
if (target === undefined) {
throw new Error(`${sourceId} references a non-existent feature`);
}
if (!isOrdinaryFeatureData(target)) {
throw new Error(
`${sourceId} references a redirect "${targetId} instead of an ordinary feature ID`,
);
}
}
/**
* Assert that a discouraged feature with no supporting browsers has a
* `removal_date`.
*
* @export
* @param {string} id The ID of the feature to be checked
* @param {FeatureData} feature The ordinary feature data to be checked
*/
export function assertRequiredRemovalDateSet(
id: string,
feature: FeatureData,
): void {
if (!feature.discouraged) {
return;
}
if (feature.discouraged.removal_date) {
return;
}
if (Object.keys(feature.status.support).length > 0) {
return;
}
if (
feature.compat_features &&
Object.keys(feature.status.by_compat_key).length > 0
)
return;
throw new Error(
`${id} is discouraged and has no reported support, so a removal date is required`,
);
}
// TODO: assertValidSnapshotReference