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