.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / normalizeRuleSettings.js
1 /* @flow */
2 "use strict";
3 const _ = require("lodash");
4 const rules /*: Object*/ = require("./rules");
5
6 // Rule settings can take a number of forms, e.g.
7 // a. "rule-name": null
8 // b. "rule-name": [null, ...]
9 // c. "rule-name": primaryOption
10 // d. "rule-name": [primaryOption]
11 // e. "rule-name": [primaryOption, secondaryOption]
12 // Where primaryOption can be anything: primitive, Object, or Array.
13 //
14 // This function normalizes all the possibilities into the
15 // standard form: [primaryOption, secondaryOption]
16 // Except in the cases with null, a & b, in which case
17 // null is returned
18 module.exports = function(
19   rawSettings /*: ?stylelint$configRuleSettings*/,
20   ruleName /*: string*/,
21   // If primaryOptionArray is not provided, we try to get it from the
22   // rules themselves, which will not work for plugins
23   primaryOptionArray /*:: ?: boolean*/
24 ) /*: [any, Object] | Array<any | [any, Object]> | null*/ {
25   if (rawSettings === null) {
26     return null;
27   }
28
29   if (!Array.isArray(rawSettings)) {
30     return [rawSettings];
31   }
32   // Everything below is an array ...
33
34   if (rawSettings[0] === null) {
35     return null;
36   }
37
38   if (primaryOptionArray === undefined) {
39     const rule /*: Function*/ = rules[ruleName];
40     primaryOptionArray = _.get(rule, "primaryOptionArray");
41   }
42
43   if (!primaryOptionArray) {
44     return rawSettings;
45   }
46   // Everything below is a rule that CAN have an array for a primary option ...
47   // (they might also have something else, e.g. rule-properties-order can
48   // have the string "alphabetical")
49
50   if (rawSettings.length === 1 && Array.isArray(rawSettings[0])) {
51     return rawSettings;
52   }
53
54   if (
55     rawSettings.length === 2 &&
56     !_.isPlainObject(rawSettings[0]) &&
57     _.isPlainObject(rawSettings[1])
58   ) {
59     return rawSettings;
60   }
61
62   return [rawSettings];
63 };