1 |
|
2 | "use strict";
|
3 |
|
4 | const _ = require("lodash");
|
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 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | module.exports = function(
|
76 | stylelint /*: stylelint$internalApi*/,
|
77 | postcssResult /*: ?postcssResultType*/,
|
78 | filePath /*: ?string*/,
|
79 | cssSyntaxError /*: ?stylelint$cssSyntaxError*/
|
80 | ) /*: Promise<stylelint$result>*/ {
|
81 | let stylelintResult;
|
82 | let source;
|
83 |
|
84 | if (postcssResult) {
|
85 | source = !postcssResult.root.source
|
86 | ? undefined
|
87 | : postcssResult.root.source.input.file ||
|
88 | postcssResult.root.source.input.id;
|
89 |
|
90 |
|
91 | const deprecationMessages = _.remove(postcssResult.messages, {
|
92 | stylelintType: "deprecation"
|
93 | });
|
94 | const deprecations = deprecationMessages.map((
|
95 | deprecationMessage
|
96 | ) => {
|
97 | return {
|
98 | text: deprecationMessage.text,
|
99 | reference: deprecationMessage.stylelintReference
|
100 | };
|
101 | });
|
102 |
|
103 |
|
104 | const invalidOptionMessages = _.remove(postcssResult.messages, {
|
105 | stylelintType: "invalidOption"
|
106 | });
|
107 | const invalidOptionWarnings = invalidOptionMessages.map((
|
108 | invalidOptionMessage
|
109 | ) => {
|
110 | return {
|
111 | text: invalidOptionMessage.text
|
112 | };
|
113 | });
|
114 |
|
115 | const parseErrors = _.remove(postcssResult.messages, {
|
116 | stylelintType: "parseError"
|
117 | });
|
118 |
|
119 |
|
120 | stylelintResult = {
|
121 | source,
|
122 | deprecations,
|
123 | invalidOptionWarnings,
|
124 | parseErrors,
|
125 | errored: postcssResult.stylelint.stylelintError,
|
126 | warnings: postcssResult.messages.map((message /*: messageType*/) => {
|
127 | return {
|
128 | line: message.line,
|
129 | column: message.column,
|
130 | rule: message.rule,
|
131 | severity: message.severity,
|
132 | text: message.text
|
133 | };
|
134 | }),
|
135 | ignored: postcssResult.stylelint.ignored,
|
136 | _postcssResult: postcssResult
|
137 | };
|
138 | } else if (cssSyntaxError) {
|
139 | if (cssSyntaxError.name !== "CssSyntaxError") {
|
140 | throw cssSyntaxError;
|
141 | }
|
142 |
|
143 | stylelintResult = {
|
144 | source: cssSyntaxError.file || "<input css 1>",
|
145 | deprecations: [],
|
146 | invalidOptionWarnings: [],
|
147 | parseErrors: [],
|
148 | errored: true,
|
149 | warnings: [
|
150 | {
|
151 | line: cssSyntaxError.line,
|
152 | column: cssSyntaxError.column,
|
153 | rule: cssSyntaxError.name,
|
154 | severity: "error",
|
155 | text: cssSyntaxError.reason + " (" + cssSyntaxError.name + ")"
|
156 | }
|
157 | ]
|
158 | };
|
159 | } else {
|
160 | throw new Error(
|
161 | "createStylelintResult must be called with either postcssResult or CssSyntaxError"
|
162 | );
|
163 | }
|
164 |
|
165 | return stylelint.getConfigForFile(filePath).then((
|
166 | configForFile
|
167 | ) => {
|
168 | const config = configForFile.config;
|
169 | const file = source || (cssSyntaxError && cssSyntaxError.file);
|
170 |
|
171 | if (config.resultProcessors) {
|
172 | config.resultProcessors.forEach(resultProcessor => {
|
173 |
|
174 |
|
175 | const returned = resultProcessor(stylelintResult, file);
|
176 |
|
177 | if (returned) {
|
178 | stylelintResult = returned;
|
179 | }
|
180 | });
|
181 | }
|
182 |
|
183 | return stylelintResult;
|
184 | });
|
185 | };
|