-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.js
65 lines (59 loc) · 1.67 KB
/
transforms.js
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
/**
* Transforms rules object to eslint rules object
*
* @param {object} rules rules object
* @param {string} severity severity level
* @return {object} eslint rules object
*/
const transform = (pluginName, rules, severity) => {
return Object.keys(rules).reduce((accumulator, ruleName) => {
const ruleOptions = rules[ruleName];
const ruleKey = (pluginName === 'eslint') ? ruleName : `${pluginName}/${ruleName}`;
if (ruleOptions === 'warn' || ruleOptions === 'error') {
accumulator[ruleKey] = ruleOptions;
return accumulator;
}
if (Array.isArray(ruleOptions)) {
accumulator[ruleKey] = [severity, ...ruleOptions];
return accumulator;
}
accumulator[ruleKey] = [severity, ruleOptions];
return accumulator;
}, {});
};
/**
* Transforms rules object to eslint rules object
*
* @param {object} offRules Rules to turn off
* @return {object} eslint rules object
*/
const transformOff = (pluginName, offRules) => {
const accumulator = {};
offRules.forEach((ruleName) => {
accumulator[`${pluginName}/${ruleName}`] = 'off';
});
return accumulator;
};
/**
* Output rules in eslint format
*
* @param {string} pluginName The name of the plugin to prefix the rules with
* @param {object} rules The rules to output
* @return {object} The rules in eslint format
*/
const outputRules = function (pluginName, rules) {
return Object.keys(rules).reduce((accumulator, severity) => {
const rulesData = rules[severity];
if (severity === 'off') {
return {
...accumulator,
...transformOff(pluginName, rulesData),
};
}
return {
...accumulator,
...transform(pluginName, rulesData, severity),
};
}, {});
};
export { outputRules };