.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / createStylelintResult.js
1 /* @flow */
2 "use strict";
3 const _ = require("lodash");
4
5 /*:: type messageType = {
6   type: string,
7   text: string,
8   line: number,
9   column: number,
10   severity: string,
11   rule: string,
12   node: Object,
13   index: number,
14   stylelintType?: string,
15   stylelintReference?: string,
16 }
17 */
18
19 /*:: type postcssResultType = {
20   processor: {
21     version: string,
22     plugins: Array<any>,
23   },
24   messages: Array<messageType>,
25   root: {
26     raws: {
27       semicolon: boolean,
28       after: string,
29     },
30     type: string,
31     nodes: Array<Object>,
32     source: {
33       input: Object,
34       start: Object,
35     },
36     lastEach: number,
37     indexes: Object,
38   },
39   opts: {
40     from: ?string,
41     syntax: ?{
42       parse: Function,
43       stringify: Function,
44     },
45   },
46   css: string,
47   map: ?any,
48   stylelint: {
49     ruleSeverities: Object,
50     customMessages: Object,
51     quiet: ?any,
52     disabledRanges: {
53       all: Array<any>,
54     },
55     ignored?: boolean,
56     stylelintError?: boolean,
57   },
58 }
59 */
60
61 /*:: type resultType = {
62   config: {
63     codeProcessors?: Array<Function>,
64     ignorePatterns?: string,
65     processors?: Array<any>,
66     quiet?: boolean,
67     resultProcessors?: Array<Function>,
68     rules: Object,
69   },
70   filepath: string,
71 }
72 */
73
74 module.exports = function(
75   stylelint /*: stylelint$internalApi*/,
76   postcssResult /*: postcssResultType*/,
77   filePath /*:: ?: string*/
78 ) /*: Promise<stylelint$result>*/ {
79   const source = !postcssResult.root.source
80     ? undefined
81     : postcssResult.root.source.input.file ||
82       postcssResult.root.source.input.id;
83
84   // Strip out deprecation warnings from the messages
85   const deprecationMessages = _.remove(postcssResult.messages, {
86     stylelintType: "deprecation"
87   });
88   const deprecations = deprecationMessages.map((
89     deprecationMessage /*: messageType*/
90   ) => {
91     return {
92       text: deprecationMessage.text,
93       reference: deprecationMessage.stylelintReference
94     };
95   });
96
97   // Also strip out invalid options
98   const invalidOptionMessages = _.remove(postcssResult.messages, {
99     stylelintType: "invalidOption"
100   });
101   const invalidOptionWarnings = invalidOptionMessages.map((
102     invalidOptionMessage /*: messageType*/
103   ) => {
104     return {
105       text: invalidOptionMessage.text
106     };
107   });
108
109   const parseErrors = _.remove(postcssResult.messages, {
110     stylelintType: "parseError"
111   });
112
113   // This defines the stylelint result object that formatters receive
114   let stylelintResult = {
115     source,
116     deprecations,
117     invalidOptionWarnings,
118     parseErrors,
119     errored: postcssResult.stylelint.stylelintError,
120     warnings: postcssResult.messages.map((message /*: messageType*/) => {
121       return {
122         line: message.line,
123         column: message.column,
124         rule: message.rule,
125         severity: message.severity,
126         text: message.text
127       };
128     }),
129     ignored: postcssResult.stylelint.ignored,
130     _postcssResult: postcssResult
131   };
132
133   return stylelint.getConfigForFile(filePath).then((
134     result /*: resultType*/
135   ) => {
136     const config = result.config;
137
138     if (config.resultProcessors) {
139       config.resultProcessors.forEach(resultProcessor => {
140         // Result processors might just mutate the result object,
141         // or might return a new one
142         const returned = resultProcessor(stylelintResult, source);
143         if (returned) {
144           stylelintResult = returned;
145         }
146       });
147     }
148
149     return stylelintResult;
150   });
151 };