.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / utils / validateOptions.js
1 /* @flow */
2 "use strict";
3
4 const _ = require("lodash");
5
6 const ignoredOptions = ["severity", "message"];
7
8 /**
9  * Validate a rule's options.
10  *
11  * See existing rules for examples.
12  *
13  * @param {Result} result - postcss result
14  * @param {string} ruleName
15  * @param {...object} ...optionDescriptions - Each optionDescription can
16  *   have the following properties:
17  *      - `actual` (required): the actual passed option value or object.
18  *      - `possible` (required): a schema representation of what values are
19  *      valid for those options. `possible` should be an object if the
20  *      options are an object, with corresponding keys; if the options are not an
21  *      object, `possible` isn't, either. All `possible` value representations
22  *      should be **arrays of either values or functions**. Values are === checked
23  *      against `actual`. Functions are fed `actual` as an argument and their
24  *      return value is interpreted: truthy = valid, falsy = invalid.
25  *    - `optional` (optional): If this is `true`, `actual` can be undefined.
26  * @return {boolean} Whether or not the options are valid (true = valid)
27  */
28 module.exports = function(
29   result /*: Object*/,
30   ruleName /*: string*/
31 ) /*: boolean*/ {
32   let noErrors = true;
33
34   const optionDescriptions = Array.from(arguments).slice(2);
35
36   optionDescriptions.forEach(optionDescription => {
37     validate(optionDescription, ruleName, complain);
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 function validate(opts, ruleName, complain) {
52   const possible = opts.possible;
53   const actual = opts.actual;
54   const optional = opts.optional;
55
56   if (actual === null || _.isEqual(actual, [null])) {
57     return;
58   }
59
60   const nothingPossible =
61     possible === undefined ||
62     (Array.isArray(possible) && possible.length === 0);
63
64   if (nothingPossible && actual === true) {
65     return;
66   }
67
68   if (actual === undefined) {
69     if (nothingPossible || optional) {
70       return;
71     }
72     complain(`Expected option value for rule "${ruleName}"`);
73     return;
74   } else if (nothingPossible) {
75     if (optional) {
76       complain(
77         `Incorrect configuration for rule "${
78           ruleName
79         }". Rule should have "possible" values for options validation`
80       );
81
82       return;
83     }
84
85     complain(`Unexpected option value "${actual}" for rule "${ruleName}"`);
86     return;
87   }
88
89   // If `possible` is a function ...
90   if (_.isFunction(possible)) {
91     if (!possible(actual)) {
92       complain(
93         `Invalid option "${JSON.stringify(actual)}" for rule ${ruleName}`
94       );
95     }
96     return;
97   }
98
99   // If `possible` is an array instead of an object ...
100   if (!_.isPlainObject(possible)) {
101     [].concat(actual).forEach(a => {
102       if (isValid(possible, a)) {
103         return;
104       }
105       complain(`Invalid option value "${a}" for rule "${ruleName}"`);
106     });
107     return;
108   }
109
110   // If possible is an object ...
111   if (!_.isPlainObject(actual)) {
112     complain(
113       `Invalid option value ${JSON.stringify(actual)} for rule "${
114         ruleName
115       }": ` + "should be an object"
116     );
117     return;
118   }
119
120   Object.keys(actual).forEach(optionName => {
121     if (ignoredOptions.indexOf(optionName) !== -1) {
122       return;
123     }
124
125     if (!possible[optionName]) {
126       complain(`Invalid option name "${optionName}" for rule "${ruleName}"`);
127       return;
128     }
129
130     const actualOptionValue = actual[optionName];
131     [].concat(actualOptionValue).forEach(a => {
132       if (isValid(possible[optionName], a)) {
133         return;
134       }
135       complain(
136         `Invalid value "${a}" for option "${optionName}" of rule "${ruleName}"`
137       );
138     });
139   });
140 }
141
142 function isValid(possible, actual) {
143   const possibleList = [].concat(possible);
144   for (let i = 0, l = possibleList.length; i < l; i++) {
145     const possibility = possibleList[i];
146     if (typeof possibility === "function" && possibility(actual)) {
147       return true;
148     }
149     if (actual === possibility) {
150       return true;
151     }
152   }
153 }