forked from foolip/mdn-bcd-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-missing-features.ts
176 lines (155 loc) · 4.83 KB
/
find-missing-features.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
//
// mdn-bcd-collector: find-missing-features.ts
// Script to find features that are in the collector or BCD but not the other
//
// © Gooborg Studios, Google LLC
// See the LICENSE file for copyright details
//
import {CompatData, CompatStatement} from '@mdn/browser-compat-data/types';
import {Tests} from './types/types.js';
import {fileURLToPath} from 'node:url';
import esMain from 'es-main';
import fs from 'fs-extra';
import yargs from 'yargs';
import {hideBin} from 'yargs/helpers';
const traverseFeatures = (obj: any, path: string, includeAliases: boolean) => {
const features: string[] = [];
for (const id of Object.keys(obj)) {
if (!obj[id] || typeof obj[id] !== 'object') {
continue;
}
const compat: CompatStatement = obj[id].__compat;
if (compat) {
features.push(`${path}${id}`);
if (includeAliases && !id.endsWith('_event')) {
const aliases = new Set();
for (let statements of Object.values(compat.support)) {
if (!Array.isArray(statements)) {
statements = [statements];
}
for (const statement of statements) {
if (statement.flags) {
continue;
}
if (statement.alternative_name) {
aliases.add(statement.alternative_name);
}
if (statement.prefix) {
let name = id;
if (path.startsWith('api.')) {
name = name[0].toUpperCase() + name.substr(1);
}
aliases.add(statement.prefix + name);
}
}
}
for (const alias of aliases) {
features.push(`${path}${alias}`);
}
}
}
features.push(
...traverseFeatures(obj[id], path + id + '.', includeAliases)
);
}
return features;
};
const findMissing = (entries: string[], allEntries: string[]) => {
const missingEntries: string[] = [];
for (const entry of allEntries) {
if (!entries.includes(entry)) {
missingEntries.push(entry);
}
}
return {missingEntries, total: allEntries.length};
};
const getMissing = (
bcd: CompatData,
tests: Tests,
direction = 'collector-from-bcd',
category: string[] = [],
includeAliases = false
) => {
const filterCategory = (item) => {
return (
!category.length || category.some((cat) => item.startsWith(`${cat}.`))
);
};
const bcdEntries = [
...traverseFeatures(bcd.api, 'api.', includeAliases),
...traverseFeatures(bcd.css.properties, 'css.properties.', includeAliases),
...traverseFeatures(
bcd.javascript.builtins,
'javascript.builtins.',
includeAliases
)
].filter(filterCategory);
const collectorEntries = Object.keys(tests).filter(filterCategory);
switch (direction) {
case 'bcd-from-collector':
return findMissing(bcdEntries, collectorEntries);
default:
console.log(
`Direction '${direction}' is unknown; defaulting to collector <- bcd`
);
// eslint-disable-next-line no-fallthrough
case 'collector-from-bcd':
return findMissing(collectorEntries, bcdEntries);
}
};
/* c8 ignore start */
const main = (bcd: CompatData, tests: Tests) => {
const {argv}: any = yargs(hideBin(process.argv)).command(
'$0 [--direction]',
'Find missing entries between BCD and the collector tests',
(yargs) => {
yargs
.option('include-aliases', {
alias: 'a',
describe: 'Include BCD entries using prefix or alternative_name',
type: 'boolean',
default: false
})
.option('direction', {
alias: 'd',
describe:
'Which direction to find missing entries from ("a-from-b" will check what is missing in a but present in b)',
choices: ['bcd-from-collector', 'collector-from-bcd'],
nargs: 1,
type: 'string',
default: 'collector-from-bcd'
})
.option('category', {
alias: 'c',
describe: 'The BCD categories to filter',
type: 'array',
choices: ['api', 'css.properties', 'javascript.builtins'],
default: ['api', 'css.properties', 'javascript.builtins']
});
}
);
const {missingEntries, total} = getMissing(
bcd,
tests,
argv.direction,
argv.category,
argv.includeAliases
);
console.log(missingEntries.join('\n'));
console.log(
`\n${missingEntries.length}/${total} (${(
(missingEntries.length / total) *
100.0
).toFixed(2)}%) missing`
);
};
if (esMain(import.meta)) {
const BCD_DIR = fileURLToPath(
new URL(process.env.BCD_DIR || `../browser-compat-data`, import.meta.url)
);
const {default: bcd} = await import(`${BCD_DIR}/index.js`);
const tests = await fs.readJson(new URL('./tests.json', import.meta.url));
main(bcd, tests);
}
/* c8 ignore stop */
export {traverseFeatures, findMissing, getMissing};