-
Notifications
You must be signed in to change notification settings - Fork 12
/
lint_reporter.js
85 lines (72 loc) · 2.15 KB
/
lint_reporter.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
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
76
77
78
79
80
81
82
83
84
85
const util = require("postcss-reporter/lib/util");
const formatter = require("stylelint/lib/formatters/stringFormatter");
function hasError(messages) {
return messages.some(message => {
if (message.rule) {
// stylelint
return message.severity === "error";
} else {
return message.type === "warning" || message.type === "error";
}
});
}
function shouldThrowError(sources) {
return sources.length && sources.some(entry => entry.errored);
}
module.exports = opts => {
const options = opts || {};
let shouldThrow = false;
const completeReport = [];
return {
postcssPlugin: "stylelint-reporter",
OnceExit(root, { result }) {
const messagesToLog = result.messages;
const resultSource = result.root.source
? result.root.source.input.file || result.root.source.input.id
: "";
const sourceGroupedMessages = messagesToLog.reduce(
(innerResult, message) => {
const key = util.getLocation(message).file || resultSource;
if (!message.severity) {
message.severity = message.type || "warning";
}
if (hasOwnProperty.call(innerResult, key)) {
innerResult[key].push(message);
} else {
innerResult[key] = [message];
}
return innerResult;
},
{}
);
const prepared = [];
Object.keys(sourceGroupedMessages).forEach(source => {
const messages = sourceGroupedMessages[source];
prepared.push({
warnings: messages,
source,
deprecations: [],
invalidOptionWarnings: [],
errored: hasError(messages)
});
});
if (options.clearReportedMessages) {
result.messages = [];
}
const report = formatter(prepared).trim();
if (report !== "") {
completeReport.push(report);
}
if (options.throwError && shouldThrowError(prepared)) {
shouldThrow = true;
}
},
report() {
if (completeReport.length) {
console.log(`\n${completeReport.join("\n\n")}\n`);
}
return !shouldThrow;
}
};
};
module.exports.postcss = true;