Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const normalizeRuleSettings = require('../normalizeRuleSettings'); |
| 4 | const Result = require('postcss/lib/result'); |
| 5 | const rules = require('../rules'); |
| 6 | |
| 7 | /** |
| 8 | * Useful for third-party code (e.g. plugins) to run a PostCSS Root |
| 9 | * against a specific rule and do something with the warnings |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 10 | * @template T |
| 11 | * @template {Object} O |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 12 | * @param {{ |
| 13 | ruleName: string, |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 14 | ruleSettings: import('stylelint').ConfigRuleSettings<T, O>, |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 15 | root: import('postcss').Root, |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 16 | }} options |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 17 | * @param {(warning: import('postcss').Warning) => void} callback |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 18 | * @returns {void} |
| 19 | */ |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 20 | function checkAgainstRule(options, callback) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 21 | if (!options) |
| 22 | throw new Error( |
| 23 | "checkAgainstRule requires an options object with 'ruleName', 'ruleSettings', and 'root' properties", |
| 24 | ); |
| 25 | |
| 26 | if (!callback) throw new Error('checkAgainstRule requires a callback'); |
| 27 | |
| 28 | if (!options.ruleName) throw new Error("checkAgainstRule requires a 'ruleName' option"); |
| 29 | |
| 30 | if (!Object.keys(rules).includes(options.ruleName)) |
| 31 | throw new Error(`Rule '${options.ruleName}' does not exist`); |
| 32 | |
| 33 | if (!options.ruleSettings) throw new Error("checkAgainstRule requires a 'ruleSettings' option"); |
| 34 | |
| 35 | if (!options.root) throw new Error("checkAgainstRule requires a 'root' option"); |
| 36 | |
| 37 | const settings = normalizeRuleSettings(options.ruleSettings, options.ruleName); |
| 38 | |
| 39 | if (!settings) { |
| 40 | return; |
| 41 | } |
| 42 | |
Tim van der Lippe | cc71b28 | 2021-02-12 15:51:14 +0000 | [diff] [blame] | 43 | // @ts-expect-error - this error should not occur with PostCSS 8 |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 44 | const tmpPostcssResult = new Result(); |
| 45 | |
Tim van der Lippe | 0a9b84d | 2021-03-24 11:53:15 +0000 | [diff] [blame] | 46 | rules[options.ruleName]( |
| 47 | settings[0], |
| 48 | /** @type {O} */ (settings[1]), |
| 49 | {}, |
| 50 | )(options.root, tmpPostcssResult); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 51 | tmpPostcssResult.warnings().forEach(callback); |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame^] | 52 | } |
| 53 | |
| 54 | module.exports = /** @type {typeof import('stylelint').utils.checkAgainstRule} */ ( |
| 55 | checkAgainstRule |
| 56 | ); |