Skip to content

Commit 33542eb

Browse files
committed
Merge pull request #14 from suitcss/fix_reporter_options
Fix issue with no options being passed to bemLint and reporter
2 parents fb90664 + 1275d8e commit 33542eb

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

lib/index.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ var reporter = require('postcss-reporter');
1616

1717
module.exports = preprocessor;
1818

19+
/**
20+
* Default options to PostCSS plugins
21+
*/
22+
1923
var defaults = {
2024
minify: undefined,
2125
use: [
@@ -27,9 +31,6 @@ var defaults = {
2731
'postcss-reporter'
2832
],
2933
'postcss-import': {
30-
transform: function (css, filename) {
31-
return postcss([bemLinter, reporter]).process(css, {from: filename}).css;
32-
},
3334
root: undefined,
3435
onImport: function(imported) {
3536
// Update the watch task with the list of imported files
@@ -59,7 +60,7 @@ var defaults = {
5960
* Process CSS
6061
*
6162
* @param {String} css
62-
* @return {String}
63+
* @returns {String}
6364
*/
6465

6566
function preprocessor(css, options) {
@@ -85,11 +86,11 @@ function preprocessor(css, options) {
8586
return processor.process(css);
8687
}
8788

88-
8989
/**
9090
* Merge options with defaults and set root
9191
*
9292
* @param {Object} options
93+
* @returns {Object} Merged options object
9394
*/
9495

9596
function mergeOptions(options) {
@@ -98,8 +99,10 @@ function mergeOptions(options) {
9899

99100
var merged = assign({}, defaults, options.config);
100101

102+
// Set some core options
101103
merged.minify = options.minify;
102104
merged['postcss-import'].root = options.root;
105+
merged['postcss-import'].transform = lintImportedFiles(merged);
103106

104107
// Ensure postcss-reporter is always the final plugin
105108
if (last(merged.use) !== 'postcss-reporter') {
@@ -110,3 +113,20 @@ function mergeOptions(options) {
110113

111114
return merged;
112115
}
116+
117+
/**
118+
* Returns a function to be used by postcss-import
119+
* Lint each imported component with postcss-bem-linter
120+
*
121+
* @param {Object} options
122+
* @returns {Function} Used by postcss-import transform
123+
*/
124+
function lintImportedFiles(options) {
125+
return function (css, filename) {
126+
return postcss([
127+
bemLinter(options['postcss-bem-linter']),
128+
reporter(options['postcss-reporter'])
129+
]).process(css, {from: filename}).css;
130+
};
131+
}
132+

test/error.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"postcss-reporter": {
3+
"throwError": true,
4+
"plugins": ["postcss-bem-linter"]
5+
}
6+
}

test/fixtures/error.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/** @define Component **/
2+
3+
.component {}

test/fixtures/import-error.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "./error.css"

test/test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ describe('suitcss', function () {
2929

3030
beforeEach(function() {
3131
mergeOptions = suitcss.__get__('mergeOptions');
32-
defaults = suitcss.__get__('defaults');
3332
});
3433

3534
it('should use default options when nothing is passed', function() {
36-
expect(mergeOptions({})).to.eql(defaults);
37-
expect(mergeOptions()).to.eql(defaults);
35+
var keys = [
36+
'minify',
37+
'use',
38+
'postcss-import',
39+
'postcss-reporter',
40+
'cssnano'
41+
];
42+
expect(mergeOptions({})).to.have.keys(keys);
43+
expect(mergeOptions()).to.have.keys(keys);
3844
});
3945

4046
it('should allow an import root to be set', function() {
@@ -67,7 +73,6 @@ describe('suitcss', function () {
6773
'postcss-reporter'
6874
]);
6975
expect(opts.autoprefixer).to.eql(autoprefixer);
70-
expect(opts['postcss-reporter']).to.eql(defaults['postcss-reporter']);
7176
expect(opts['postcss-import'].root).to.equal('test/root');
7277
});
7378
});
@@ -167,6 +172,15 @@ describe('cli', function () {
167172
});
168173
});
169174

175+
it('should output an error to stderr on conformance failure when throwError is set', function(done) {
176+
exec('bin/suitcss -i test/fixtures -c test/error.config.json test/fixtures/import-error.css test/fixtures/cli/output.css', function (err, stdout, stderr) {
177+
expect(err).to.be.an('error');
178+
expect(err.code).to.equal(1);
179+
expect(stderr).to.contain('postcss-reporter: warnings or errors were found');
180+
done();
181+
});
182+
});
183+
170184
it('should log on non-existant file', function (done) {
171185
exec('bin/suitcss test/fixtures/cli/non-existant.css', function (err, stdout, stderr) {
172186
expect(err).to.be.an('error');

0 commit comments

Comments
 (0)