Skip to content

Commit

Permalink
Use new PostCSS API
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 15, 2020
1 parent fd6cba4 commit 44057c3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 52 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var postcss = require('postcss');
var reporter = require('./lib/reporter');

module.exports = postcss.plugin('postcss-reporter', reporter);
module.exports = reporter;
module.exports.postcss = true
73 changes: 38 additions & 35 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,54 @@ module.exports = function(opts = {}) {

var messageFilter = opts.filter || (message => message.type === 'warning');

return (css, result) => {
var messagesToLog = result.messages
.filter(pluginFilter)
.filter(messageFilter);
return {
postcssPlugin: 'postcss-reporter',
RootExit (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 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 sourceGroupedMessages = _.groupBy(messagesToLog, message => {
return util.getLocation(message).file || resultSource;
});

var report = '';
_.forOwn(sourceGroupedMessages, function(messages, source) {
report += formatter({
messages: messages,
source: source,
var report = '';
_.forOwn(sourceGroupedMessages, function(messages, source) {
report += formatter({
messages: messages,
source: source,
});
});
});

if (opts.clearReportedMessages) {
result.messages = _.difference(result.messages, messagesToLog);
}
if (opts.clearReportedMessages) {
result.messages = _.difference(result.messages, messagesToLog);
}

if (opts.clearAllMessages) {
var messagesToClear = result.messages.filter(pluginFilter);
result.messages = _.difference(result.messages, messagesToClear);
}
if (opts.clearAllMessages) {
var messagesToClear = result.messages.filter(pluginFilter);
result.messages = _.difference(result.messages, messagesToClear);
}


if (!report) return;
if (!report) return;

console.log(report);
console.log(report);

if (opts.throwError && shouldThrowError()) {
throw new Error(chalk.red.bold('\n** postcss-reporter: warnings or errors were found **'));
}
if (opts.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(message => {
return message.type === 'warning' || message.type === 'error';
})
);
}
},
};
};
1 change: 0 additions & 1 deletion test/formatter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var test = require('tape');
var formatter = require('../lib/formatter');
var chalk = require('chalk');
var symbols = require('log-symbols');
var path = require('path');
var sourceMap = require('source-map');
Expand Down
30 changes: 16 additions & 14 deletions test/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('reporter with simple mock result', function(t) {
formatter: mockFormatter(tracker),
});
t.doesNotThrow(function() {
testReporter(null, mockSimpleResult);
testReporter.RootExit(null, { result: mockSimpleResult });
});
t.deepEqual(tracker.messages, mockSimpleResult.messages);
t.equal(tracker.source, '<input css 1>');
Expand Down Expand Up @@ -86,7 +86,9 @@ test('reporter with simple mock result containing non warning typed message', fu
formatter: mockFormatter(tracker),
});
t.doesNotThrow(function() {
testReporter(null, mockResultContainingNonWarningMessage);
testReporter.RootExit(null, {
result: mockResultContainingNonWarningMessage,
});
});
t.deepEqual(tracker.messages, expectedMessages);
t.equal(tracker.source, '<input css 1>');
Expand All @@ -99,7 +101,7 @@ test('reporter with simple mock result and whitelisted plugins', function(t) {
formatter: mockFormatter(tracker),
plugins: ['foo', 'bar'],
});
testReporter(null, mockSimpleResult);
testReporter.RootExit(null, { result: mockSimpleResult });
t.deepEqual(
tracker.messages,
[
Expand All @@ -125,7 +127,7 @@ test('reporter with simple mock result and blacklisted plugins', function(t) {
formatter: mockFormatter(tracker),
plugins: ['!foo', '!baz'],
});
testReporter(null, mockSimpleResult);
testReporter.RootExit(null, { result: mockSimpleResult });
t.deepEqual(
tracker.messages,
[
Expand Down Expand Up @@ -153,7 +155,7 @@ test('reporter with simple mock result and function-filtered plugins', function(
formatter: mockFormatter(tracker),
filter: function(message) { return message.type === 'error'; },
});
testReporter(null, cloneResult);
testReporter.RootExit(null, { result: cloneResult });
t.deepEqual(
tracker.messages,
[
Expand All @@ -173,7 +175,7 @@ test('reporter with simple mock result and empty plugins', function(t) {
formatter: mockFormatter(tracker),
plugins: [],
});
testReporter(null, mockSimpleResult);
testReporter.RootExit(null, { result: mockSimpleResult });
t.deepEqual(
tracker.messages,
mockSimpleResult.messages
Expand All @@ -188,7 +190,7 @@ test('reporter with simple mock result and clearReportedMessages', function(t) {
formatter: mockFormatter(tracker),
clearReportedMessages: true,
});
testReporter(null, cloneResult);
testReporter.RootExit(null, { result: cloneResult });
t.deepEqual(cloneResult.messages, []);
t.end();
});
Expand All @@ -201,7 +203,7 @@ test('reporter with simple mock result, whitelisted plugins and clearReportedMes
plugins: ['baz', 'foo'],
clearReportedMessages: true,
});
testReporter(null, cloneResult);
testReporter.RootExit(null, { result: cloneResult });
t.deepEqual(
cloneResult.messages,
[
Expand All @@ -222,7 +224,7 @@ test('reporter with simple mock result and clearAllMessages', function(t) {
formatter: mockFormatter(tracker),
clearAllMessages: true,
});
testReporter(null, cloneResult);
testReporter.RootExit(null, { result: cloneResult });
t.deepEqual(cloneResult.messages, []);
t.end();
});
Expand All @@ -235,7 +237,7 @@ test('reporter with simple mock result, clearAllMessages and whitelisted plugins
plugins: ['foo'],
clearAllMessages: true,
});
testReporter(null, cloneResult);
testReporter.RootExit(null, { result: cloneResult });
t.deepEqual(
cloneResult.messages,
[
Expand Down Expand Up @@ -267,7 +269,7 @@ test('reporter with simple mock result and throwError', function(t) {
throwError: true,
});
t.throws(function() {
testReporter(null, cloneResult);
testReporter.RootExit(null, { result: cloneResult });
});
t.end();
});
Expand Down Expand Up @@ -302,7 +304,7 @@ test('reporter with mock containing file source', function(t) {
var testReporter = reporter({
formatter: mockFormatter(tracker),
});
testReporter(null, mockResultFromFile);
testReporter.RootExit(null, { result: mockResultFromFile });
t.equal(tracker.source, '/path/to/file.css');
t.end();
})
Expand All @@ -323,7 +325,7 @@ test('reporter with mock containing no source', function(t) {
var testReporter = reporter({
formatter: mockFormatter(tracker),
});
testReporter(null, mockResultNoSource);
testReporter.RootExit(null, { result: mockResultNoSource });
t.equal(tracker.source, '');
t.end();
})
Expand Down Expand Up @@ -417,7 +419,7 @@ test('reporter with warnings that messages that each have nodes', function(t) {
var testReporter = reporter({
formatter: mockMultiSourceFormatter(tracker),
});
testReporter(null, mockWarningNodeResult);
testReporter.RootExit(null, { result: mockWarningNodeResult });
t.deepEqual(tracker, [
{
source: 'foo.css',
Expand Down

0 comments on commit 44057c3

Please sign in to comment.