/
checkAgainstRule.js
42 lines (27 loc) · 1.28 KB
/
checkAgainstRule.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
'use strict';
const normalizeRuleSettings = require('../normalizeRuleSettings');
const Result = require('postcss/lib/result');
const { isPlainObject } = require('./validateTypes');
const getStylelintRule = require('./getStylelintRule');
/**
* @type {import('stylelint').Utils['checkAgainstRule']}
*/
function checkAgainstRule(options, callback) {
if (!isPlainObject(options)) throw new Error('Expected an options object');
if (!callback) throw new Error('Expected a callback function');
const { ruleName, ruleSettings, root, result, context = {} } = options;
if (!ruleName) throw new Error('Expected a "ruleName" option');
const rule = getStylelintRule(ruleName, result && result.stylelint.config);
if (!rule) throw new Error(`Rule "${ruleName}" does not exist`);
if (!ruleSettings) throw new Error('Expected a "ruleSettings" option');
if (!root) throw new Error('Expected a "root" option');
const settings = normalizeRuleSettings(ruleSettings, rule);
if (!settings) {
return;
}
// @ts-expect-error - this error should not occur with PostCSS 8
const tmpPostcssResult = new Result();
rule(settings[0], /** @type {Object} */ (settings[1]), context)(root, tmpPostcssResult);
for (const warning of tmpPostcssResult.warnings()) callback(warning);
}
module.exports = checkAgainstRule;