-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mjs
448 lines (410 loc) · 13.5 KB
/
main.mjs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import { browsers, features, groups, snapshots } from 'web-features';
import bcd from '@mdn/browser-compat-data' assert { type: 'json' };
import webSpecs from 'web-specs/index.json' assert { type: 'json' };
import assert from 'assert';
/**
* From a standardization perspective, a W3C specification can be considered to
* be interoperable once it reaches the Proposed Recommendation stage.
*/
const interoperableStatuses = [
'Recommendation',
'Proposed Recommendation'
];
/**
* From a standardization perspective, a W3C specification can be considered to
* be relatively stable once it reaches the Candidation Recommendation stage.
*/
const stableStatuses = [
'Candidate Recommendation Snapshot',
'Candidate Recommendation Draft'
].concat(interoperableStatuses);
/**
* Keep a mapping between a BCD key path and the key entry itself
*/
const bcdKeys = {};
/**
* Helper function to retrieve BCD support data from a key path such as
* `css.properties.display.grid`.
*/
function getBcdKey(key, { support } = { support: false }) {
const cachedKey = bcdKeys[key];
if (cachedKey) {
if (support) {
return cachedKey.__compat;
}
else {
return cachedKey;
}
}
const keyPath = key.split('.');
let currKey = bcd;
for (const level of keyPath) {
if (!level) {
break;
}
currKey = currKey[level];
if (!currKey) {
throw new Error(`BCD key "${key}" does not exist`);
}
}
bcdKeys[key] = currKey;
if (support) {
if (!currKey.__compat) {
throw new Error(`BCD key "${key}" does not have compat data`);
}
return currKey.__compat;
}
else {
return currKey;
}
}
/**
* Return true when the given web-specs entry is a good match for the given
* list of URLs. Used to map BCD `spec_url` properties to web-specs.
*/
function isRelevantSpec(spec, urls) {
return urls.find(url => url.startsWith(spec.nightly?.url)) ||
urls.find(url => url.startsWith(spec.release?.url)) ||
urls.find(url => url.startsWith(spec.url)) ||
(spec.shortname === spec.series.currentSpecification && urls.find(url => url.startsWith(spec.series?.nightlyUrl))) ||
(spec.shortname === spec.series.currentSpecification && urls.find(url => url.startsWith(spec.series?.releaseUrl)));
}
/**
* Complete the given spec with the list of compat features that it defines
* among the given list of compat features.
*
* The function returns a copy of the spec when it alters it to add a
* `compat_features` property, the spec itself otherwise.
*/
function completeWithCompatFeatures(spec, compat_features) {
let copy = null;
for (const feature of compat_features) {
const bcdKey = getBcdKey(feature, { support: true });
if (bcdKey?.spec_url) {
const urls = Array.isArray(bcdKey.spec_url) ? bcdKey.spec_url : [bcdKey.spec_url];
if (isRelevantSpec(spec, urls)) {
if (!copy) {
copy = Object.assign({}, spec);
}
if (!copy.compat_features) {
copy.compat_features = [];
}
copy.compat_features.push(feature);
}
}
}
return copy ?? spec;
}
/**
* Format a list of specs linked to a feature in web-feature
*/
const formatList = (title, list) => {
if (list.length === 0) {
return '';
}
list.sort((f1, f2) => f1.spec.shortname.localeCompare(f2.spec.shortname));
const markdownList = list
.map(f => {
if (f.spec.compat_features) {
const compat = f.spec.compat_features
.map(c => {
const key = getBcdKey(c, { support: true });
if (key?.mdn_url) {
return ' - [`' + c + '`](' + key.mdn_url + ')';
} else {
return ' - `' + c + '`';
}
})
.join('\n');
return `- [${f.spec.shortname}](${f.spec.url}) is referenced by BCD keys used to define feature \`${f.feature}\`:\n${compat}`;
}
else {
return `- [${f.spec.shortname}](${f.spec.url}) is referenced by feature \`${f.feature}\``;
}
})
.join('\n');
return `
<details>
<summary>${title} (${list.length})</summary>
${markdownList}
</details>
`;
};
/**
* Run an analysis starting from a collection of features, and output
* divergences that may be worth looking into, namely:
* - late incubations: well-supported features for which the underlying W3C
* spec is still in incubation phase (not yet published under /TR)
* - late working drafts: well-supported features for which the underlying W3C
* spec is on the Recommendation track, but not yet a Candidate Recommendation.
* - late implementations contains not-so-well-supported features for which the
* underlying W3C spec is a Proposed Recommendation or Recommendation.
*/
function analyzeFeatures(features, { useSpecsProperty } = { useSpecsProperty: false }) {
const worthChecking = {
lateIncubation: {
high: [],
low: [],
false: []
},
lateWorkingDrafts: {
high: [],
low: [],
false: []
},
interoperableStatuses: {
high: [],
low: [],
false: []
}
};
function recordPossibleAnomalies(anomaly, feature, baseline, specs) {
for (const spec of specs) {
worthChecking[anomaly][baseline].push({
feature,
spec: {
shortname: spec.shortname,
url: spec.url,
compat_features: spec.compat_features
}
});
}
}
for (const [feature, desc] of Object.entries(features)) {
if (!desc.status) {
continue;
}
// Assemble the list of specs that define (part of) the feature
let specs;
if (!useSpecsProperty && desc.compat_features) {
// Feature links to BCD keys, extract the list of specs from there as
// BCD is far more specific than web-features
specs = webSpecs
.map(s => completeWithCompatFeatures(s, desc.compat_features))
.filter(s => s.compat_features);
}
else {
// Feature does not link to BCD keys, use the info from the `spec`
// property directly.
assert(desc.spec, `"${feature}" does not link to any spec`);
const urls = Array.isArray(desc.spec) ? desc.spec : [desc.spec];
specs = webSpecs.filter(s => isRelevantSpec(s, urls));
if (urls.length > 0) {
assert(specs.length > 0, `No spec found in web-specs for "${feature}"`);
}
if (useSpecsProperty) {
specs = specs.map(spec => Object.assign({ compat_features: desc.compat_features }, spec));
}
}
// We're only interested in W3C specs for now
const w3cSpecs = specs.filter(s => s.organization === 'W3C');
if (w3cSpecs.length === 0) {
continue;
}
if ([false, 'low', 'high'].includes(desc.status.baseline)) {
// Assess whether feature is well supported or not-so-well supported.
if (desc.status.baseline === false) {
const codebases = new Set();
for (const browser of Object.keys(desc.status.support)) {
const codebase = browser === 'edge' ? 'chrome' : browser.split('_')[0];
codebases.add(codebase);
}
if (codebases.size <= 1) {
recordPossibleAnomalies(
'interoperableStatuses',
feature, desc.status.baseline,
w3cSpecs.filter(spec =>
spec.release &&
interoperableStatuses.includes(spec.release.status))
);
continue;
}
}
else {
recordPossibleAnomalies(
'interoperableStatuses',
feature, desc.status.baseline,
w3cSpecs.filter(spec =>
spec.release &&
interoperableStatuses.includes(spec.release.status))
);
}
recordPossibleAnomalies(
'lateIncubation',
feature, desc.status.baseline,
w3cSpecs.filter(spec => !spec.release)
);
recordPossibleAnomalies(
'lateWorkingDrafts',
feature, desc.status.baseline,
w3cSpecs.filter(spec =>
spec.release &&
!stableStatuses.includes(spec.release.status))
);
}
}
const formatAnomalies = (anomaly, { only } = { only: null }) => {
const markdown = '' +
(((only === null) || only === 'high') ? formatList('Specs linked to Baseline high features', worthChecking[anomaly].high) : '') +
(((only === null) || only === 'low') ? formatList('Specs linked to Baseline low features', worthChecking[anomaly].low) : '') +
(((only === null) || only === false) ? formatList('Specs linked to supported-though-not-yet-Baseline features', worthChecking[anomaly][false]) : '');
return markdown || '\n*No problems found.*';
};
console.log(`
## Late incubations?
W3C specs that are still in incubation and that define well-supported features.
${formatAnomalies('lateIncubation')}
## Worth publishing as Candidate Recommendation?
W3C specs that are still Working Drafts and that define well-supported features.
${formatAnomalies('lateWorkingDrafts')}
## Interoperable specs with missing implementations?
W3C specs that are already Recommendation (or Proposed Recommendation) and that define not-so-well supported features.
${formatAnomalies('interoperableStatuses', { only: false })}
`);
}
/**
* Retrieve all BCD keys that map to the given spec
*
* The function expects the bcdKeys mapping table to have been initialized.
*/
function getAllBcdKeys(spec) {
const keys = [];
for (const [key, desc] of Object.entries(bcdKeys)) {
if (!desc.__compat) {
continue;
}
if (!desc.__compat.spec_url) {
continue;
}
const urls = Array.isArray(desc.__compat.spec_url) ?
desc.__compat.spec_url :
[desc.__compat.spec_url];
if (isRelevantSpec(spec, urls)) {
keys.push(key);
}
}
return keys;
}
/**
* Traverse BCD features.
*
* Simplified version of:
* https://github.com/mdn/browser-compat-data/blob/main/scripts/traverse.ts#L22
*/
function* traverseBCDFeatures(key) {
if (key) {
const bcdKey = getBcdKey(key, { support: false });
for (const i in bcdKey) {
if (!!bcdKey[i] && typeof bcdKey[i] == 'object' && i !== '__compat') {
const subkey = key ? `${key}.${i}` : i;
yield subkey;
yield* traverseBCDFeatures(subkey);
}
}
}
else {
for (const rootLevel of [
'api', 'css', 'html', 'http', 'svg', 'javascript', 'mathml', 'webassembly', 'webdriver']
) {
yield* traverseBCDFeatures(rootLevel);
}
}
}
/**
* List of browsers in core Baseline browser set
*/
const baselineBrowsers = [
'chrome', 'chrome_android',
'edge',
'firefox', 'firefox_android',
'safari', 'safari_ios'
];
/**
* Compute the support across browsers from a list of keys
*/
function getBrowserSupport(keys) {
const support = {};
const compatData = keys
.map(key => getBcdKey(key, { support: true }))
.filter(data => !!data);
for (const browser of baselineBrowsers) {
support[browser] = '';
for (const data of compatData) {
let browserSupport = data.support?.[browser];
if (!browserSupport) {
support[browser] = '';
break;
}
if (Array.isArray(browserSupport)) {
browserSupport = browserSupport[0];
}
if (browserSupport.partial_implementation || browserSupport.flags) {
support[browser] = '';
break;
}
const versionAdded = browserSupport.version_added;
if (versionAdded) {
if (versionAdded > support[browser]) {
support[browser] = versionAdded;
}
}
else {
support[browser] = '';
break;
}
}
}
for (const browser of baselineBrowsers) {
if (support[browser] === '') {
delete support[browser];
}
}
return support;
}
/**
* Run a spec analysis starting from BCD directly
*/
function analyzeBCD() {
// Initialize the flat list of BCD keys
for (const key of traverseBCDFeatures()) {
getBcdKey(key, { support: false });
}
// Initialize the list of spec "features" that we're interested in
const w3cSpecs = webSpecs.filter(s => s.organization === 'W3C');
const specFeatures = {};
for (const spec of w3cSpecs) {
const compat_features = getAllBcdKeys(spec);
if (compat_features.length > 0) {
const support = getBrowserSupport(compat_features);
/*if (Object.keys(support).length > 6) {
console.warn(spec.shortname, support, compat_features);
}*/
if (Object.keys(support).length > 0) {
specFeatures[spec.shortname] = {
compat_features,
status: {
baseline: (Object.keys(support).length > 6) ? 'low' : false,
support
},
spec: spec.url
};
}
}
}
analyzeFeatures(specFeatures, { useSpecsProperty: true });
}
/**
* Main starting point, run analyses in order.
*/
console.log(`
# Analyzing features in \`web-features\`
Features in \`web-features\` reference BCD keys (and/or specs directly). BCD keys can be used to collect a list of W3C specs that define concepts that compose a given feature.
Comparing the Baseline status of the feature with the status of these specs on the [W3C Recommendation track](https://www.w3.org/2023/Process-20231103/#rec-track) yields the following lists of specs that may be worth looking into.
`);
analyzeFeatures(features);
console.log(`
# Analyzing W3C specs in web-specs
To try to give a more complete perspective and highlight the need to have features defined in \`web-features\`, we can also map BCD keys directly to specs in \`web-specs\`, and consider that each spec defines a feature in itself for which we can compute a rough Baseline status from BCD support data.
Running the same analysis with this new list of "features" yields the following lists of specs that may be worth looking into.
`);
analyzeBCD();