forked from postcss/postcss-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.js
More file actions
103 lines (81 loc) · 2.45 KB
/
formatter.js
File metadata and controls
103 lines (81 loc) · 2.45 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var pico = require('picocolors');
var path = require('path');
var util = require('./util');
function sortBy(cb) {
return (v1, v2) => {
v1 = cb(v1);
v2 = cb(v2);
return v1 < v2 ? -1 : v1 > v2 ? 1 : 0;
};
}
function createSortFunction(positionless, sortByPosition) {
var positionValue = 0
if (positionless === 'any') { positionValue = 1; }
if (positionless === 'first') { positionValue = 2; }
if (positionless === 'last') { positionValue = 0; }
var sortFunction = sortBy((m) => {
if (!m.line) return 1;
return positionValue;
})
if (sortByPosition) {
return (v1, v2) => (
sortFunction(v1, v2) ||
sortBy((m) => m.line || "")(v1, v2) ||
sortBy((m) => m.column || "")(v1, v2));
}
return sortFunction;
}
module.exports = function (opts) {
var options = opts || {};
var sortByPosition =
typeof options.sortByPosition !== 'undefined'
? options.sortByPosition
: true;
var positionless = options.positionless || 'first';
var sortFunction = createSortFunction(positionless, sortByPosition);
return function (input) {
var messages = input.messages.filter(function (message) {
return typeof message.text === 'string';
});
var source = input.source;
if (!messages.length) return '';
var orderedMessages = messages.sort(sortFunction);
var output = '\n';
if (source) {
output += pico.bold(pico.underline(logFrom(source))) + '\n';
}
orderedMessages.forEach(function (w) {
output += messageToString(w) + '\n';
});
return output;
function messageToString(message) {
var location = util.getLocation(message);
var str = '';
if (location.line) {
str += pico.bold(location.line);
}
if (location.column) {
str += pico.bold(':' + location.column);
}
if (location.line || location.column) {
str += '\t';
}
if (!options.noIcon) {
if (message.type === 'warning') {
str += pico.yellow(util.warningSymbol + ' ');
} else if (message.type === 'error') {
str += pico.red(util.errorSymbol + ' ');
}
}
str += message.text;
if (!options.noPlugin) {
str += pico.yellow(' [' + message.plugin + ']');
}
return str;
}
function logFrom(fromValue) {
if (fromValue.charAt(0) === '<') return fromValue;
return path.relative(process.cwd(), fromValue).split(path.sep).join('/');
}
};
};