-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathparse.ts
More file actions
221 lines (201 loc) · 6.48 KB
/
Copy pathparse.ts
File metadata and controls
221 lines (201 loc) · 6.48 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
import winston from "winston";
import { tagsToFeatures } from "./compat-helpers.ts";
import type { FeatureData } from "./types.ts";
const logger = winston.createLogger({
level: "error",
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple(),
),
transports: new winston.transports.Console(),
});
interface CompatSets {
core: string[];
modifier: string[];
spare: string[];
}
interface AuthoredCompatSets {
core: string[];
modifier?: string[];
spare?: string[];
}
export interface ParsedAuthoredData extends Partial<FeatureData> {
compatFeatures: CompatSets & { all: string[] };
}
export function parseAuthoring(
id: string,
authored: unknown,
): ParsedAuthoredData {
// NB: This function doesn't actually parse most of a feature entry. Right
// now, it handles compat_features and compute_from statuses. There's more to
// be done here to get complete parsing, but it's probably not time well spent
// before writing a formal schema for authored YAML files and perhaps adopting
// a validation library like zod.
if (!isRecord(authored)) {
throw new Error(`Expected authored data object, got ${authored}`);
}
// There are six ways to specify the compat keys for a feature and which of
// those keys to use to compute a status. In order of precedence, they are:
//
// - compat sets
// compat_features object with sets of keys in an authored YAML file
// - compute_from and flat compat_features
// compute_from and compat_features array in an authored YAML file
// - compute_from and implicit keys
// compute_from in an authored YAML file and keys from a BCD tag
// - flat compat_features
// compat_features array in an authored YAML file
// - implicit keys
// keys from a BCD tag
// - keyless
// no enumerated keys
//
// This following if statement finds the first matching style, then sorts the
// keys into buckets accordingly.
let compatFeatures: CompatSets;
if (isAuthoredCompatSets(authored.compat_features)) {
logger.debug(`${id} has explicit compat sets`);
compatFeatures = makeCompatSets(authored.compat_features);
} else if (
isComputeFromOverride(authored.status) &&
Array.isArray(authored.compat_features)
) {
logger.debug(`${id} has compute_from and flat compat_features`);
compatFeatures = makeCompatSets(
authored.compat_features,
parseComputeFrom(authored.status.compute_from),
);
} else if (
isComputeFromOverride(authored.status) &&
authored.compat_features === undefined
) {
logger.debug(`${id} has compute_from and implicit keys`);
compatFeatures = makeCompatSets(
id,
parseComputeFrom(authored.status.compute_from),
);
} else if (Array.isArray(authored.compat_features)) {
logger.debug(`${id} has flat compat_features`);
compatFeatures = makeCompatSets(authored.compat_features);
} else {
logger.debug(`${id} has implicit keys or is keyless`);
compatFeatures = makeCompatSets(id);
}
return {
compatFeatures: {
all: [
...compatFeatures.core,
...compatFeatures.modifier,
...compatFeatures.spare,
].toSorted(),
...compatFeatures,
},
};
}
// FIXME: `makeCompatSets()` has several signatures, which made it easier to
// write tests and get tab completion. Admittedly, it's a lot of not-terribly
// pretty lines at the point of implementation. I think the pros outweigh the
// cons here, but we can drop this before merging the PR, if desired.
/*
* Construct a CompatSets object from one of the authorable structures or data
* sources.
*/
export function makeCompatSets(set: AuthoredCompatSets): CompatSets;
export function makeCompatSets(
keys: string[],
computeFrom: [string, ...string[]],
): CompatSets;
export function makeCompatSets(
id: string,
computeFrom: [string, ...string[]],
): CompatSets;
export function makeCompatSets(keys: string[]): CompatSets;
export function makeCompatSets(
id: string,
computeFrom: [string, ...string[]],
): CompatSets;
export function makeCompatSets(id: string): CompatSets;
export function makeCompatSets(): CompatSets;
export function makeCompatSets(
setsOrKeysOrId?: AuthoredCompatSets | string[] | string,
computeFrom?: [string, ...string[]],
): CompatSets {
if (setsOrKeysOrId === undefined) {
return {
core: [],
modifier: [],
spare: [],
};
}
if (isAuthoredCompatSets(setsOrKeysOrId)) {
const sets = setsOrKeysOrId;
return {
core: (sets.core ? sets.core : []).toSorted(),
modifier: (sets.modifier ? sets.modifier : []).toSorted(),
spare: (sets.spare ? sets.spare : []).toSorted(),
};
}
const keys: string[] = Array.isArray(setsOrKeysOrId)
? setsOrKeysOrId
: idToKeys(setsOrKeysOrId);
if (computeFrom) {
for (const key of computeFrom) {
if (!keys.includes(key)) {
throw new Error(`${key} is not a member of ${keys}`);
}
}
return {
core: computeFrom.toSorted(),
modifier: [],
spare: keys.filter((k) => !computeFrom.includes(k)).toSorted(),
};
}
return {
core: keys.toSorted(),
modifier: [],
spare: [],
};
}
function idToKeys(id: string): string[] {
const expectedTag = `web-features:${id}`;
const bcdFeatures = tagsToFeatures.get(expectedTag) ?? [];
return bcdFeatures.map((f) => f.id);
}
function isRecord(
value: unknown,
): value is Record<string | number | symbol, unknown> {
return typeof value === "object" && value !== null;
}
function isComputeFromOverride(
value: unknown,
): value is { compute_from: string[] | string } {
if (isRecord(value) && "compute_from" in value) {
const { compute_from } = value;
return Array.isArray(compute_from) || typeof compute_from === "string";
}
}
function isAuthoredCompatSets(value: unknown): value is AuthoredCompatSets {
return (
isRecord(value) && ["core", "modifier", "spare"].some((key) => key in value)
);
}
function parseComputeFrom(value: unknown): [string, ...string[]] {
if (typeof value === "string") {
return [value];
}
if (Array.isArray(value)) {
const first = value[0];
if (first === undefined) {
throw new Error(`compute_from must have a minimum length of 1`);
}
for (const key of value) {
if (typeof key !== "string") {
throw new Error(
`${key} in compute value array ${JSON.stringify(value)} is not a string`,
);
}
}
return [first, ...value.slice(1)];
}
throw new Error(`${value} is not a well-formed compute_from`);
}