diff options
Diffstat (limited to 'node_modules/postcss-reporter/lib')
| -rw-r--r-- | node_modules/postcss-reporter/lib/formatter.js | 75 | ||||
| -rw-r--r-- | node_modules/postcss-reporter/lib/reporter.js | 134 | ||||
| -rw-r--r-- | node_modules/postcss-reporter/lib/util.js | 11 |
3 files changed, 93 insertions, 127 deletions
diff --git a/node_modules/postcss-reporter/lib/formatter.js b/node_modules/postcss-reporter/lib/formatter.js index 7358cc61..b5719b8f 100644 --- a/node_modules/postcss-reporter/lib/formatter.js +++ b/node_modules/postcss-reporter/lib/formatter.js @@ -1,60 +1,45 @@ -var pico = require('picocolors'); +var chalk = require('chalk'); var path = require('path'); -var firstBy = require('thenby'); +var symbols = require('log-symbols'); +var _ = require('lodash'); var util = require('./util'); -var supportsLargeCharset = - process.platform !== 'win32' || - process.env.CI || - process.env.TERM === 'xterm-256color'; -var warningSymbol = supportsLargeCharset ? '⚠' : '!!'; - -function createSortFunction(positionless, sortByPosition) { - var positionValue = 0 - - if (positionless === 'any') { positionValue = 1; } - if (positionless === 'first') { positionValue = 2; } - if (positionless === 'last') { positionValue = 0; } - - var sortFunction = firstBy((m) => { - if (!m.line) return 1; - return positionValue; - }) - - if (sortByPosition) { - sortFunction = sortFunction.thenBy('line').thenBy('column'); - } - - return sortFunction; -} - -module.exports = function (opts) { +module.exports = function(opts) { var options = opts || {}; - var sortByPosition = - typeof options.sortByPosition !== 'undefined' - ? options.sortByPosition - : true; + 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'; - }); + return function(input) { + var messages = input.messages; var source = input.source; if (!messages.length) return ''; - var orderedMessages = messages.sort(sortFunction); + var orderedMessages = _.sortBy( + messages, + function(m) { + if (!m.line) return 1; + if (positionless === 'any') return 1; + if (positionless === 'first') return 2; + if (positionless === 'last') return 0; + }, + function(m) { + if (!sortByPosition) return 1; + return m.line; + }, + function(m) { + if (!sortByPosition) return 1; + return m.column; + } + ); var output = '\n'; if (source) { - output += pico.bold(pico.underline(logFrom(source))) + '\n'; + output += chalk.bold.underline(logFrom(source)) + '\n'; } - orderedMessages.forEach(function (w) { + orderedMessages.forEach(function(w) { output += messageToString(w) + '\n'; }); @@ -65,11 +50,11 @@ module.exports = function (opts) { var str = ''; if (location.line) { - str += pico.bold(location.line); + str += chalk.bold(location.line); } if (location.column) { - str += pico.bold(':' + location.column); + str += chalk.bold(':' + location.column) } if (location.line || location.column) { @@ -77,12 +62,12 @@ module.exports = function (opts) { } if (!options.noIcon && message.type === 'warning') { - str += pico.yellow(warningSymbol + ' '); + str += chalk.yellow(symbols.warning + ' '); } str += message.text; if (!options.noPlugin) { - str += pico.yellow(' [' + message.plugin + ']'); + str += chalk.yellow(' [' + message.plugin + ']'); } return str; } diff --git a/node_modules/postcss-reporter/lib/reporter.js b/node_modules/postcss-reporter/lib/reporter.js index 71872a97..70781fba 100644 --- a/node_modules/postcss-reporter/lib/reporter.js +++ b/node_modules/postcss-reporter/lib/reporter.js @@ -1,101 +1,81 @@ +var chalk = require('chalk'); +var _ = require('lodash'); var defaultFormatter = require('./formatter'); -var pico = require('picocolors'); var util = require('./util'); -module.exports = function (opts = {}) { - var formatter = - opts.formatter || - defaultFormatter({ - noIcon: opts.noIcon, - noPlugin: opts.noPlugin, - }); +module.exports = function(opts) { + var options = opts || {}; + + var formatter = options.formatter || defaultFormatter({ + sortByPosition: (typeof options.sortByPosition !== 'undefined') ? options.sortByPosition : true, + positionless: options.positionless || 'first', + noIcon: options.noIcon, + noPlugin: options.noPlugin, + }); var pluginFilter; - if (!opts.plugins) { + if (!options.plugins) { // Every plugin - pluginFilter = function () { - return true; - }; - } else if ( - opts.plugins.every(function (plugin) { - return plugin[0] === '!'; - }) - ) { - // Deny list - pluginFilter = function (message) { - return opts.plugins.indexOf('!' + message.plugin) === -1; + pluginFilter = function() { return true; }; + } else if (options.plugins.every(function(plugin) { return plugin[0] === '!'; })) { + // Blacklist + pluginFilter = function(message) { + return options.plugins.indexOf('!' + message.plugin) === -1; }; } else { - // Allow list - pluginFilter = function (message) { - return opts.plugins.indexOf(message.plugin) !== -1; + // Whitelist + pluginFilter = function(message) { + return options.plugins.indexOf(message.plugin) !== -1; }; } - var messageFilter = opts.filter || ((message) => message.type === 'warning'); - - return { - postcssPlugin: 'postcss-reporter', - OnceExit(css, { result }) { - var messagesToLog = result.messages - .filter(pluginFilter) - .filter(messageFilter); + var messageFilter = options.filter || function(message) { return (message.type === 'warning'); }; - var resultSource = !result.root.source - ? '' - : result.root.source.input.file || result.root.source.input.id; + return function(css, result) { + var messagesToLog = result.messages + .filter(pluginFilter) + .filter(messageFilter); - var sourceGroupedMessages = messagesToLog.reduce((grouped, message) => { - const key = util.getLocation(message).file || resultSource; + var resultSource = (!result.root.source) ? '' + : result.root.source.input.file || result.root.source.input.id - if (!grouped.hasOwnProperty(key)) { - grouped[key] = []; - } - - grouped[key].push(message); + var sourceGroupedMessages = _.groupBy(messagesToLog, function(message) { + return util.getLocation(message).file || resultSource; + }); - return grouped; - }, {}); + var report = ''; + _.forOwn(sourceGroupedMessages, function(messages, source) { + report += formatter({ + messages: messages, + source: source, + }); + }); - var report = ''; - for (const source in sourceGroupedMessages) { - if (sourceGroupedMessages.hasOwnProperty(source)) { - report += formatter({ - messages: sourceGroupedMessages[source], - source: source, - }); - } - } + if (options.clearReportedMessages) { + result.messages = _.difference(result.messages, messagesToLog); + } - if (opts.clearReportedMessages) { - result.messages = result.messages.filter(message => !messagesToLog.includes(message)); - } + if (options.clearAllMessages) { + var messagesToClear = result.messages.filter(pluginFilter); + result.messages = _.difference(result.messages, messagesToClear); + } - if (opts.clearAllMessages) { - var messagesToClear = result.messages.filter(pluginFilter); - result.messages = result.messages.filter(message => !messagesToClear.includes(message)); - } - if (!report) return; + if (!report) return; - console.log(report); + console.log(report); - if (opts.throwError && shouldThrowError()) { - throw new Error( - pico.red( - pico.bold('\n** postcss-reporter: warnings or errors were found **') - ) - ); - } + if (options.throwError && shouldThrowError()) { + throw new Error(chalk.red.bold('\n** postcss-reporter: warnings or errors were found **')); + } - function shouldThrowError() { - return ( - messagesToLog.length && - messagesToLog.some((message) => { - return message.type === 'warning' || message.type === 'error'; - }) - ); - } - }, + function shouldThrowError() { + return ( + messagesToLog.length + && messagesToLog.some(function(message) { + return message.type === 'warning' || message.type === 'error'; + }) + ); + } }; }; diff --git a/node_modules/postcss-reporter/lib/util.js b/node_modules/postcss-reporter/lib/util.js index 094b3acc..76c649bc 100644 --- a/node_modules/postcss-reporter/lib/util.js +++ b/node_modules/postcss-reporter/lib/util.js @@ -1,4 +1,6 @@ -exports.getLocation = function (message) { +var _ = require('lodash'); + +exports.getLocation = function(message) { var messageNode = message.node; var location = { @@ -6,13 +8,12 @@ exports.getLocation = function (message) { column: message.column, }; - var messageInput = messageNode && messageNode.source && messageNode.source.input; + var messageInput = _.get(messageNode, 'source.input'); if (!messageInput) return location; - var originLocation = - messageInput.origin && messageInput.origin(message.line, message.column); - if (originLocation) return originLocation; + var originLocation = messageInput.origin && messageInput.origin(message.line, message.column) + if (originLocation) return originLocation location.file = messageInput.file || messageInput.id; return location; |
