1 | ;
|
2 |
|
3 | const _ = require('lodash');
|
4 | const requireRule = require('./requireRule');
|
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 | /**
|
15 | * This function normalizes all the possibilities into the
|
16 | * standard form: [primaryOption, secondaryOption]
|
17 | * Except in the cases with null, a & b, in which case
|
18 | * null is returned
|
19 | * @param {import('stylelint').StylelintConfigRuleSettings} rawSettings
|
20 | * @param {string} ruleName
|
21 | * @param {boolean} [primaryOptionArray] If primaryOptionArray is not provided, we try to get it from the, rules themselves, which will not work for plugins
|
22 | * @return {[any, Object] | Array<any | [any, Object]> | null}
|
23 | */
|
24 | module.exports = function (
|
25 | rawSettings,
|
26 | ruleName,
|
27 | // If primaryOptionArray is not provided, we try to get it from the
|
28 | // rules themselves, which will not work for plugins
|
29 | primaryOptionArray,
|
30 | ) {
|
31 | if (rawSettings === null) {
|
32 | return null;
|
33 | }
|
34 |
|
35 | if (!Array.isArray(rawSettings)) {
|
36 | return [rawSettings];
|
37 | }
|
38 | // Everything below is an array ...
|
39 |
|
40 | if (rawSettings[0] === null) {
|
41 | return null;
|
42 | }
|
43 |
|
44 | if (primaryOptionArray === undefined) {
|
45 | const rule = requireRule(ruleName);
|
46 |
|
47 | primaryOptionArray = _.get(rule, 'primaryOptionArray');
|
48 | }
|
49 |
|
50 | if (!primaryOptionArray) {
|
51 | return rawSettings;
|
52 | }
|
53 | // Everything below is a rule that CAN have an array for a primary option ...
|
54 | // (they might also have something else, e.g. rule-properties-order can
|
55 | // have the string "alphabetical")
|
56 |
|
57 | if (rawSettings.length === 1 && Array.isArray(rawSettings[0])) {
|
58 | return rawSettings;
|
59 | }
|
60 |
|
61 | if (
|
62 | rawSettings.length === 2 &&
|
63 | !_.isPlainObject(rawSettings[0]) &&
|
64 | _.isPlainObject(rawSettings[1])
|
65 | ) {
|
66 | return rawSettings;
|
67 | }
|
68 |
|
69 | return [rawSettings];
|
70 | };
|