forked from stylelint/stylelint
/
validateOptions.js
175 lines (139 loc) · 4.42 KB
/
validateOptions.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
const arrayEqual = require('./arrayEqual');
const { isPlainObject } = require('is-plain-object');
const IGNORED_OPTIONS = new Set(['severity', 'message', 'reportDisables', 'disableFix']);
/** @typedef {(value: unknown) => boolean} PossibleFunc */
/** @typedef {boolean | number | string | PossibleFunc} Possible */
/** @typedef {{possible?: (undefined | PossibleFunc | Possible[] | Record<string, Possible[]>), actual: unknown, optional?: boolean}} Options */
/**
* Validate a rule's options.
*
* See existing rules for examples.
*
* @param {import('stylelint').PostcssResult} result - postcss result
* @param {string} ruleName
* @param {...Options} optionDescriptions - Each optionDescription can
* have the following properties:
* - `actual` (required): the actual passed option value or object.
* - `possible` (required): a schema representation of what values are
* valid for those options. `possible` should be an object if the
* options are an object, with corresponding keys; if the options are not an
* object, `possible` isn't, either. All `possible` value representations
* should be **arrays of either values or functions**. Values are === checked
* against `actual`. Functions are fed `actual` as an argument and their
* return value is interpreted: truthy = valid, falsy = invalid.
* - `optional` (optional): If this is `true`, `actual` can be undefined.
* @return {boolean} Whether or not the options are valid (true = valid)
*/
module.exports = function validateOptions(result, ruleName, ...optionDescriptions) {
let noErrors = true;
optionDescriptions.forEach((optionDescription) => {
validate(optionDescription, ruleName, complain);
});
/**
* @param {string} message
*/
function complain(message) {
noErrors = false;
result.warn(message, {
stylelintType: 'invalidOption',
});
result.stylelint = result.stylelint || {
disabledRanges: {},
ruleSeverities: {},
customMessages: {},
};
result.stylelint.stylelintError = true;
}
return noErrors;
};
/**
* @param {Options} opts
* @param {string} ruleName
* @param {(message: string) => void} complain
*/
function validate(opts, ruleName, complain) {
const possible = opts.possible;
const actual = opts.actual;
const optional = opts.optional;
if (actual === null || arrayEqual(actual, [null])) {
return;
}
const nothingPossible =
possible === undefined || (Array.isArray(possible) && possible.length === 0);
if (nothingPossible && actual === true) {
return;
}
if (actual === undefined) {
if (nothingPossible || optional) {
return;
}
complain(`Expected option value for rule "${ruleName}"`);
return;
}
if (nothingPossible) {
if (optional) {
complain(
`Incorrect configuration for rule "${ruleName}". Rule should have "possible" values for options validation`,
);
return;
}
complain(`Unexpected option value "${String(actual)}" for rule "${ruleName}"`);
return;
}
if (typeof possible === 'function') {
if (!possible(actual)) {
complain(`Invalid option "${JSON.stringify(actual)}" for rule ${ruleName}`);
}
return;
}
// If `possible` is an array instead of an object ...
if (Array.isArray(possible)) {
for (const a of [actual].flat()) {
if (isValid(possible, a)) {
continue;
}
complain(`Invalid option value "${String(a)}" for rule "${ruleName}"`);
}
return;
}
// If actual is NOT an object ...
if (!isPlainObject(actual) || typeof actual !== 'object' || actual == null) {
complain(
`Invalid option value ${JSON.stringify(actual)} for rule "${ruleName}": should be an object`,
);
return;
}
for (const [optionName, optionValue] of Object.entries(actual)) {
if (IGNORED_OPTIONS.has(optionName)) {
continue;
}
const possibleValue = possible && possible[optionName];
if (!possibleValue) {
complain(`Invalid option name "${optionName}" for rule "${ruleName}"`);
continue;
}
for (const a of [optionValue].flat()) {
if (isValid(possibleValue, a)) {
continue;
}
complain(`Invalid value "${a}" for option "${optionName}" of rule "${ruleName}"`);
}
}
}
/**
* @param {Possible | Possible[]} possible
* @param {unknown} actual
* @returns {boolean}
*/
function isValid(possible, actual) {
for (const possibility of [possible].flat()) {
if (typeof possibility === 'function' && possibility(actual)) {
return true;
}
if (actual === possibility) {
return true;
}
}
return false;
}