/
normalizeRuleSettings.js
61 lines (53 loc) · 1.65 KB
/
normalizeRuleSettings.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
/* @flow */
"use strict"
const _ = require("lodash")
// Rule settings can take a number of forms, e.g.
// a. "rule-name": null
// b. "rule-name": [null, ...]
// c. "rule-name": primaryOption
// d. "rule-name": [primaryOption]
// e. "rule-name": [primaryOption, secondaryOption]
// Where primaryOption can be anything: primitive, Object, or Array.
//
// This function normalizes all the possibilities into the
// standard form: [primaryOption, secondaryOption]
// Except in the cases with null, a & b, in which case
// null is returned
module.exports = function (
rawSettings/*: stylelint$configRuleSettings*/,
ruleName/*: string*/,
primaryOptionArray/*:: ?: boolean*/
)/*: ?Array<any | [any, Object]>*/ {
if (rawSettings === null) {
return null
}
if (!Array.isArray(rawSettings)) {
return [rawSettings]
}
// Everything below is an array ...
if (rawSettings[0] === null) {
return null
}
// This cursed rule needs a special case
if (ruleName === "declaration-block-properties-order") {
if (rawSettings[0] === "alphabetical") {
return rawSettings
}
if (typeof rawSettings[0] === "string") {
return [rawSettings]
}
}
if (!primaryOptionArray) {
return rawSettings
}
// Everything below is a rule that CAN have an array for a primary option ...
// (they might also have something else, e.g. rule-properties-order can
// have the string "alphabetical")
if (rawSettings.length === 1 && Array.isArray(rawSettings[0])) {
return rawSettings
}
if (rawSettings.length === 2 && !_.isPlainObject(rawSettings[0]) && _.isPlainObject(rawSettings[1])) {
return rawSettings
}
return [rawSettings]
}