.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / utils / report.js
1 /* @flow */
2 "use strict";
3
4 const _ = require("lodash");
5
6 /**
7  * Report a violation.
8  *
9  * This function accounts for `disabledRanges` attached to the result.
10  * That is, if the reported violation is within a disabledRange,
11  * it is ignored. Otherwise, it is attached to the result as a
12  * postcss warning.
13  *
14  * It also accounts for the rule's severity.
15  *
16  * You *must* pass *either* a node or a line number.
17  */
18 module.exports = function(
19   violation /*: {
20   ruleName: string,
21   result: Object,
22   message: string,
23   node: Object,
24   index?: number,
25   word?: string,
26   line?: number
27 }*/
28 ) {
29   const ruleName = violation.ruleName;
30   const result = violation.result;
31   const message = violation.message;
32   const line = violation.line;
33   const node = violation.node;
34   const index = violation.index;
35   const word = violation.word;
36
37   result.stylelint = result.stylelint || {};
38
39   // In quiet mode, mere warnings are ignored
40   if (
41     result.stylelint.quiet &&
42     result.stylelint.ruleSeverities[ruleName] !== "error"
43   ) {
44     return;
45   }
46
47   // If a line is not passed, use the node.positionBy method to get the
48   // line number that the complaint pertains to
49   const startLine = line || node.positionBy({ index }).line;
50
51   if (result.stylelint.disabledRanges && !result.stylelint.ignoreDisables) {
52     const ranges =
53       result.stylelint.disabledRanges[ruleName] ||
54       result.stylelint.disabledRanges.all;
55     for (const range of ranges) {
56       if (
57         // If the violation is within a disabledRange,
58         // and that disabledRange's rules include this one,
59         // do not register a warning
60         range.start <= startLine &&
61         (range.end >= startLine || range.end === undefined) &&
62         (!range.rules || range.rules.indexOf(ruleName) !== -1)
63       ) {
64         return;
65       }
66     }
67   }
68
69   const severity = _.get(
70     result.stylelint,
71     ["ruleSeverities", ruleName],
72     "ignore"
73   );
74
75   if (!result.stylelint.stylelintError && severity === "error") {
76     result.stylelint.stylelintError = true;
77   }
78
79   const warningProperties /*: Object*/ = {
80     severity,
81     rule: ruleName
82   };
83   if (node) {
84     warningProperties.node = node;
85   }
86   if (index) {
87     warningProperties.index = index;
88   }
89   if (word) {
90     warningProperties.word = word;
91   }
92
93   const warningMessage = _.get(
94     result.stylelint,
95     ["customMessages", ruleName],
96     message
97   );
98   result.warn(warningMessage, warningProperties);
99 };