UNPKG

4.2 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4
5const ignoredOptions = ['severity', 'message', 'reportDisables'];
6
7/** @typedef {{possible: any, actual: any, optional: boolean}} Options */
8
9/**
10 * Validate a rule's options.
11 *
12 * See existing rules for examples.
13 *
14 * @param {import('stylelint').PostcssResult} result - postcss result
15 * @param {string} ruleName
16 * @param {...Options} optionDescriptions - Each optionDescription can
17 * have the following properties:
18 * - `actual` (required): the actual passed option value or object.
19 * - `possible` (required): a schema representation of what values are
20 * valid for those options. `possible` should be an object if the
21 * options are an object, with corresponding keys; if the options are not an
22 * object, `possible` isn't, either. All `possible` value representations
23 * should be **arrays of either values or functions**. Values are === checked
24 * against `actual`. Functions are fed `actual` as an argument and their
25 * return value is interpreted: truthy = valid, falsy = invalid.
26 * - `optional` (optional): If this is `true`, `actual` can be undefined.
27 * @return {boolean} Whether or not the options are valid (true = valid)
28 */
29
30module.exports = function (result, ruleName, ...optionDescriptions) {
31 let noErrors = true;
32
33 optionDescriptions.forEach((optionDescription) => {
34 validate(optionDescription, ruleName, complain);
35 });
36
37 /**
38 * @param {string} message
39 */
40 function complain(message) {
41 noErrors = false;
42 result.warn(message, {
43 stylelintType: 'invalidOption',
44 });
45 _.set(result, 'stylelint.stylelintError', true);
46 }
47
48 return noErrors;
49};
50
51/**
52 * @param {Options} opts
53 * @param {string} ruleName
54 * @param {(s: string) => void} complain
55 */
56function validate(opts, ruleName, complain) {
57 const possible = opts.possible;
58 const actual = opts.actual;
59 const optional = opts.optional;
60
61 if (actual === null || _.isEqual(actual, [null])) {
62 return;
63 }
64
65 const nothingPossible =
66 possible === undefined || (Array.isArray(possible) && possible.length === 0);
67
68 if (nothingPossible && actual === true) {
69 return;
70 }
71
72 if (actual === undefined) {
73 if (nothingPossible || optional) {
74 return;
75 }
76
77 complain(`Expected option value for rule "${ruleName}"`);
78
79 return;
80 }
81
82 if (nothingPossible) {
83 if (optional) {
84 complain(
85 `Incorrect configuration for rule "${ruleName}". Rule should have "possible" values for options validation`,
86 );
87
88 return;
89 }
90
91 complain(`Unexpected option value "${String(actual)}" for rule "${ruleName}"`);
92
93 return;
94 }
95
96 // If `possible` is a function ...
97 if (_.isFunction(possible)) {
98 if (!possible(actual)) {
99 complain(`Invalid option "${JSON.stringify(actual)}" for rule ${ruleName}`);
100 }
101
102 return;
103 }
104
105 // If `possible` is an array instead of an object ...
106 if (!_.isPlainObject(possible)) {
107 [].concat(actual).forEach((a) => {
108 if (isValid(possible, a)) {
109 return;
110 }
111
112 complain(`Invalid option value "${String(a)}" for rule "${ruleName}"`);
113 });
114
115 return;
116 }
117
118 // If actual is NOT an object ...
119 if (typeof actual !== 'object') {
120 complain(
121 `Invalid option value ${JSON.stringify(actual)} for rule "${ruleName}": should be an object`,
122 );
123
124 return;
125 }
126
127 Object.keys(actual).forEach((optionName) => {
128 if (ignoredOptions.includes(optionName)) {
129 return;
130 }
131
132 if (!possible[optionName]) {
133 complain(`Invalid option name "${optionName}" for rule "${ruleName}"`);
134
135 return;
136 }
137
138 const actualOptionValue = actual[optionName];
139
140 [].concat(actualOptionValue).forEach((a) => {
141 if (isValid(possible[optionName], a)) {
142 return;
143 }
144
145 complain(`Invalid value "${a}" for option "${optionName}" of rule "${ruleName}"`);
146 });
147 });
148}
149
150/**
151 * @param {any|Function} possible
152 * @param {any} actual
153 * @returns {boolean}
154 */
155function isValid(possible, actual) {
156 const possibleList = /** @type {Array<any|Function>} */ ([]).concat(possible);
157
158 for (let i = 0, l = possibleList.length; i < l; i++) {
159 const possibility = possibleList[i];
160
161 if (typeof possibility === 'function' && possibility(actual)) {
162 return true;
163 }
164
165 if (actual === possibility) {
166 return true;
167 }
168 }
169
170 return false;
171}