.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / postcss-reporter / lib / formatter.js
1 var chalk = require('chalk');
2 var path = require('path');
3 var symbols = require('log-symbols');
4 var _ = require('lodash');
5 var util = require('./util');
6
7 module.exports = function(opts) {
8   var options = opts || {};
9   var sortByPosition = (typeof options.sortByPosition !== 'undefined') ? options.sortByPosition : true;
10   var positionless = options.positionless || 'first';
11
12   return function(input) {
13     var messages = input.messages;
14     var source = input.source;
15
16     if (!messages.length) return '';
17
18     var orderedMessages = _.sortBy(
19       messages,
20       function(m) {
21         if (!m.line) return 1;
22         if (positionless === 'any') return 1;
23         if (positionless === 'first') return 2;
24         if (positionless === 'last') return 0;
25       },
26       function(m) {
27         if (!sortByPosition) return 1;
28         return m.line;
29       },
30       function(m) {
31         if (!sortByPosition) return 1;
32         return m.column;
33       }
34     );
35
36     var output = '\n';
37
38     if (source) {
39       output += chalk.bold.underline(logFrom(source)) + '\n';
40     }
41
42     orderedMessages.forEach(function(w) {
43       output += messageToString(w) + '\n';
44     });
45
46     return output;
47
48     function messageToString(message) {
49       var location = util.getLocation(message);
50       var str = '';
51
52       if (location.line) {
53         str += chalk.bold(location.line);
54       }
55
56       if (location.column) {
57         str += chalk.bold(':' + location.column)
58       }
59
60       if (location.line || location.column) {
61         str += '\t';
62       }
63
64       if (!options.noIcon && message.type === 'warning') {
65         str += chalk.yellow(symbols.warning + '  ');
66       }
67
68       str += message.text;
69       if (!options.noPlugin) {
70         str += chalk.yellow(' [' + message.plugin + ']');
71       }
72       return str;
73     }
74
75     function logFrom(fromValue) {
76       if (fromValue.charAt(0) === '<') return fromValue;
77       return path.relative(process.cwd(), fromValue).split(path.sep).join('/');
78     }
79   };
80 };