.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / utils / checkAgainstRule.js
1 /* @flow */
2 "use strict";
3
4 const normalizeRuleSettings = require("../normalizeRuleSettings");
5 const Result = require("postcss/lib/result");
6 const rules = require("../rules");
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
10 module.exports = function(
11   options /*: {
12     ruleName: string,
13     ruleSettings: stylelint$configRuleSettings,
14     root: Object,
15   }*/,
16   callback /*: Function*/
17 ) {
18   if (!options)
19     throw new Error(
20       "checkAgainstRule requires an options object with 'ruleName', 'ruleSettings', and 'root' properties"
21     );
22   if (!callback) throw new Error("checkAgainstRule requires a callback");
23   if (!options.ruleName)
24     throw new Error("checkAgainstRule requires a 'ruleName' option");
25   if (!rules[options.ruleName])
26     throw new Error(`Rule '${options.ruleName}' does not exist`);
27   if (!options.ruleSettings)
28     throw new Error("checkAgainstRule requires a 'ruleSettings' option");
29   if (!options.root)
30     throw new Error("checkAgainstRule requires a 'root' option");
31
32   const settings = normalizeRuleSettings(
33     options.ruleSettings,
34     options.ruleName
35   );
36   if (!settings) {
37     return;
38   }
39
40   const tmpPostcssResult = new Result();
41   rules[options.ruleName](settings[0], settings[1], {})(
42     options.root,
43     tmpPostcssResult
44   );
45   tmpPostcssResult.warnings().forEach(callback);
46 };