aboutsummaryrefslogtreecommitdiff
path: root/node_modules/postcss-reporter/lib
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/postcss-reporter/lib')
-rw-r--r--node_modules/postcss-reporter/lib/formatter.js80
-rw-r--r--node_modules/postcss-reporter/lib/reporter.js81
-rw-r--r--node_modules/postcss-reporter/lib/util.js20
3 files changed, 181 insertions, 0 deletions
diff --git a/node_modules/postcss-reporter/lib/formatter.js b/node_modules/postcss-reporter/lib/formatter.js
new file mode 100644
index 0000000..b5719b8
--- /dev/null
+++ b/node_modules/postcss-reporter/lib/formatter.js
@@ -0,0 +1,80 @@
+var chalk = require('chalk');
+var path = require('path');
+var symbols = require('log-symbols');
+var _ = require('lodash');
+var util = require('./util');
+
+module.exports = function(opts) {
+ var options = opts || {};
+ var sortByPosition = (typeof options.sortByPosition !== 'undefined') ? options.sortByPosition : true;
+ var positionless = options.positionless || 'first';
+
+ return function(input) {
+ var messages = input.messages;
+ var source = input.source;
+
+ if (!messages.length) return '';
+
+ 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 += chalk.bold.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 += chalk.bold(location.line);
+ }
+
+ if (location.column) {
+ str += chalk.bold(':' + location.column)
+ }
+
+ if (location.line || location.column) {
+ str += '\t';
+ }
+
+ if (!options.noIcon && message.type === 'warning') {
+ str += chalk.yellow(symbols.warning + ' ');
+ }
+
+ str += message.text;
+ if (!options.noPlugin) {
+ str += chalk.yellow(' [' + message.plugin + ']');
+ }
+ return str;
+ }
+
+ function logFrom(fromValue) {
+ if (fromValue.charAt(0) === '<') return fromValue;
+ return path.relative(process.cwd(), fromValue).split(path.sep).join('/');
+ }
+ };
+};
diff --git a/node_modules/postcss-reporter/lib/reporter.js b/node_modules/postcss-reporter/lib/reporter.js
new file mode 100644
index 0000000..70781fb
--- /dev/null
+++ b/node_modules/postcss-reporter/lib/reporter.js
@@ -0,0 +1,81 @@
+var chalk = require('chalk');
+var _ = require('lodash');
+var defaultFormatter = require('./formatter');
+var util = require('./util');
+
+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 (!options.plugins) {
+ // Every plugin
+ 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 {
+ // Whitelist
+ pluginFilter = function(message) {
+ return options.plugins.indexOf(message.plugin) !== -1;
+ };
+ }
+
+ var messageFilter = options.filter || function(message) { return (message.type === 'warning'); };
+
+ return function(css, result) {
+ var messagesToLog = result.messages
+ .filter(pluginFilter)
+ .filter(messageFilter);
+
+ var resultSource = (!result.root.source) ? ''
+ : result.root.source.input.file || result.root.source.input.id
+
+ var sourceGroupedMessages = _.groupBy(messagesToLog, function(message) {
+ return util.getLocation(message).file || resultSource;
+ });
+
+ var report = '';
+ _.forOwn(sourceGroupedMessages, function(messages, source) {
+ report += formatter({
+ messages: messages,
+ source: source,
+ });
+ });
+
+ if (options.clearReportedMessages) {
+ result.messages = _.difference(result.messages, messagesToLog);
+ }
+
+ if (options.clearAllMessages) {
+ var messagesToClear = result.messages.filter(pluginFilter);
+ result.messages = _.difference(result.messages, messagesToClear);
+ }
+
+
+ if (!report) return;
+
+ console.log(report);
+
+ 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(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
new file mode 100644
index 0000000..76c649b
--- /dev/null
+++ b/node_modules/postcss-reporter/lib/util.js
@@ -0,0 +1,20 @@
+var _ = require('lodash');
+
+exports.getLocation = function(message) {
+ var messageNode = message.node;
+
+ var location = {
+ line: message.line,
+ column: message.column,
+ };
+
+ var messageInput = _.get(messageNode, 'source.input');
+
+ if (!messageInput) return location;
+
+ var originLocation = messageInput.origin && messageInput.origin(message.line, message.column)
+ if (originLocation) return originLocation
+
+ location.file = messageInput.file || messageInput.id;
+ return location;
+};