forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-prefix.js
More file actions
88 lines (80 loc) · 2.12 KB
/
test-prefix.js
File metadata and controls
88 lines (80 loc) · 2.12 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
'use strict';
const path = require('path');
const chalk = require('chalk');
/**
* @typedef {import('../../types').Identifier} Identifier
*/
/**
* @param {Identifier} data
* @param {string} category
* @param {string[]} errors
* @param {string} prefix
* @param {string} [path]
*/
function checkPrefix(data, category, errors, prefix, path = '') {
for (const key in data) {
if (key === 'prefix' && typeof data[key] === 'string') {
if (data[key].includes(prefix)) {
const error = chalk`{red → {bold ${prefix}} prefix is wrong for key: {bold ${path}}}`;
const rules = [
category == 'api' && !data[key].startsWith(prefix),
category == 'css' && !data[key].startsWith(`-${prefix}`),
];
if (rules.some(x => x === true)) {
errors.push(error);
}
}
} else {
if (typeof data[key] === 'object') {
const curr_path = path.length > 0 ? `${path}.${key}` : key;
checkPrefix(data[key], category, errors, prefix, curr_path);
}
}
}
return errors;
}
/**
* @param {Identifier} data
* @param {string} category
* @return {string[]}
*/
function processData(data, category) {
let errors = [];
let prefixes = [];
if (category === 'api') {
prefixes = ['moz', 'Moz', 'webkit', 'WebKit', 'webKit', 'ms', 'MS'];
}
if (category === 'css') {
prefixes = ['webkit', 'moz', 'ms'];
}
for (const prefix of prefixes) {
checkPrefix(data, category, errors, prefix);
}
return errors;
}
/**
* @param {string} filename
*/
function testPrefix(filename) {
const relativePath = path.relative(
path.resolve(__dirname, '..', '..'),
filename,
);
const category =
relativePath.includes(path.sep) && relativePath.split(path.sep)[0];
const data = require(filename);
const errors = processData(data, category);
if (errors.length) {
console.error(
chalk`{red Prefix – {bold ${errors.length}} ${
errors.length === 1 ? 'error' : 'errors'
}:}`,
);
for (const error of errors) {
console.error(` ${error}`);
}
return true;
}
return false;
}
module.exports = testPrefix;