UNPKG

2.26 kBJavaScriptView Raw
1'use strict';
2
3const rules = require('./rules');
4const { isPlainObject } = require('./utils/validateTypes');
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 * @template T
20 * @template {Object} O
21 * @param {import('stylelint').ConfigRuleSettings<T, O>} rawSettings
22 * @param {string} ruleName
23 * @param {boolean} [primaryOptionArray] If primaryOptionArray is not provided, we try to get it from the rules themselves, which will not work for plugins
24 * @return {[T] | [T, O] | null}
25 */
26module.exports = function normalizeRuleSettings(
27 rawSettings,
28 ruleName,
29 // If primaryOptionArray is not provided, we try to get it from the
30 // rules themselves, which will not work for plugins
31 primaryOptionArray,
32) {
33 if (rawSettings === null || rawSettings === undefined) {
34 return null;
35 }
36
37 if (!Array.isArray(rawSettings)) {
38 return [rawSettings];
39 }
40 // Everything below is an array ...
41
42 if (rawSettings.length > 0 && (rawSettings[0] === null || rawSettings[0] === undefined)) {
43 return null;
44 }
45
46 if (primaryOptionArray === undefined) {
47 const rule = rules[ruleName];
48
49 if (rule && 'primaryOptionArray' in rule) {
50 primaryOptionArray = rule.primaryOptionArray;
51 }
52 }
53
54 if (!primaryOptionArray) {
55 return rawSettings;
56 }
57 // Everything below is a rule that CAN have an array for a primary option ...
58 // (they might also have something else, e.g. rule-properties-order can
59 // have the string "alphabetical")
60
61 if (rawSettings.length === 1 && Array.isArray(rawSettings[0])) {
62 return rawSettings;
63 }
64
65 if (rawSettings.length === 2 && !isPlainObject(rawSettings[0]) && isPlainObject(rawSettings[1])) {
66 return rawSettings;
67 }
68
69 // `T` must be an array type, but TSC thinks it's probably invalid to
70 // cast `[T]` to `T` so we cast through `any` first.
71 return [/** @type {T} */ (/** @type {any} */ (rawSettings))];
72};