1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 |
|
5 | const ignoredOptions = ['severity', 'message', 'reportDisables'];
|
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 | module.exports = function (result, ruleName, ...optionDescriptions) {
|
31 | let noErrors = true;
|
32 |
|
33 | optionDescriptions.forEach((optionDescription) => {
|
34 | validate(optionDescription, ruleName, complain);
|
35 | });
|
36 |
|
37 | |
38 |
|
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 |
|
53 |
|
54 |
|
55 |
|
56 | function 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 |
|
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 |
|
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 |
|
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 |
|
152 |
|
153 |
|
154 |
|
155 | function isValid(possible, actual) {
|
156 | const possibleList = ([]).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 | }
|