-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathcommon-errors.js
More file actions
57 lines (49 loc) · 1.68 KB
/
common-errors.js
File metadata and controls
57 lines (49 loc) · 1.68 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
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import { walk } from '../../utils/index.js';
/** @import {InternalCompatStatement} from '../../types/index.js' */
/**
* Fixes common errors in InternalCompatStatements.
*
* - Replaces `browser: { version_added: "mirror" }` with `browser: "mirror"`
* - Wraps `browser: false` with `browser: `{ version_added: false }`
* @param {Pick<InternalCompatStatement, "support">} compat The compat statement to fix
* @returns {void}
*/
export const fixCommonErrorsInCompatStatement = (compat) => {
for (const browser of Object.keys(compat.support)) {
if (compat.support[browser] === false) {
compat.support[browser] = {
version_added: false,
};
} else if (
typeof compat.support[browser] === 'object' &&
JSON.stringify(compat.support[browser]) === '{"version_added":"mirror"}'
) {
compat.support[browser] = 'mirror';
}
if (
browser == 'ie' &&
JSON.stringify(compat.support[browser]) === '{"version_added":false}'
) {
Reflect.deleteProperty(compat.support, browser);
}
}
};
/**
* Update compat data to 'mirror' if the statement matches mirroring
* @param {string} filename The name of the file to fix
* @param {string} actual The current content of the file
* @returns {string} expected content of the file
*/
const fixCommonErrors = (filename, actual) => {
if (filename.includes('/browsers/')) {
return actual;
}
const bcd = JSON.parse(actual);
for (const { compat } of walk(undefined, bcd)) {
fixCommonErrorsInCompatStatement(compat);
}
return JSON.stringify(bcd, null, 2);
};
export default fixCommonErrors;