Skip to content

Commit f6dc009

Browse files
committed
Add ability to use built in formatters
Also remove the built in one Fixes #1
1 parent b365adf commit f6dc009

File tree

4 files changed

+91
-43
lines changed

4 files changed

+91
-43
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,20 @@ file.csslint.results = []; // CSSLint errors
6666
file.csslint.opt = {}; // The options you passed to CSSLint
6767
```
6868

69-
## Custom Reporters
69+
## Using reporters
70+
71+
Several reporters come built-in to css-lint. To use one of these reporters, pass the name to `csslint.reporter`.
72+
73+
For a list of all reporters supported by `csslint`, see the [csslint wiki](https://github.com/CSSLint/csslint/wiki/Command-line-interface#--format).
74+
75+
```js
76+
gulp.task('lint', function() {
77+
gulp.files('lib/*.css')
78+
.pipe(csslint())
79+
.pipe(csslint.reporter('junit-xml'));
80+
```
81+
82+
### Custom Reporters
7083
7184
Custom reporter functions can be passed as `csslint.reporter(reporterFunc)`. The reporter function will be called for each linted file and passed the file object as described above.
7285
@@ -118,7 +131,7 @@ gulp.task('lint', function() {
118131
gulp.files('lib/*.css')
119132
.pipe(csslint())
120133
.pipe(csslint.reporter()) // Display errors
121-
.pipe(csslint.failReporter()); // Fail on error
134+
.pipe(csslint.reporter('fail')); // Fail on error (or csslint.failReporter())
122135
});
123136
```
124137

index.js

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
/*jshint node:true */
2-
31
'use strict';
42

53
var gutil = require('gulp-util');
6-
var c = gutil.colors;
74
var error = gutil.PluginError;
8-
var es = require('event-stream');
5+
var through = require('through2');
96
var csslint = require('csslint').CSSLint;
107
var RcLoader = require('rcloader');
118

@@ -26,14 +23,13 @@ var formatOutput = function(report, file, options) {
2623
return err;
2724
});
2825

29-
var output = {
26+
return {
27+
originalReport: report,
3028
errorCount: results.length,
3129
success: false,
3230
results: results,
3331
options: options
3432
};
35-
36-
return output;
3733
};
3834

3935
var cssLintPlugin = function(options) {
@@ -48,11 +44,11 @@ var cssLintPlugin = function(options) {
4844
ruleset[rule.id] = 1;
4945
});
5046

51-
return es.map(function(file, cb) {
47+
return through.obj(function(file, enc, cb) {
5248
if (file.isNull()) return cb(null, file); // pass along
53-
if (file.isStream()) return cb(new error('gulp-csslint: Streaming not supported'));
49+
if (file.isStream()) return cb(new error('gulp-csslint: Streaming not supported'), file);
5450

55-
var content = file.contents.toString('utf8');
51+
var content = file.contents.toString(enc);
5652

5753
if (!content) return cb(null, file); // pass along
5854

@@ -79,47 +75,47 @@ var cssLintPlugin = function(options) {
7975
});
8076
};
8177

82-
var defaultReporter = function(file) {
83-
var errorCount = file.csslint.errorCount;
84-
var plural = errorCount === 1 ? '' : 's';
85-
86-
gutil.log(c.cyan(errorCount)+' error'+plural+' found in '+c.magenta(file.path));
87-
88-
file.csslint.results.forEach(function(result) {
89-
var message = result.error;
90-
gutil.log(
91-
c.red('[') +
92-
(
93-
typeof message.line !== 'undefined' ?
94-
c.yellow( 'L' + message.line ) +
95-
c.red(':') +
96-
c.yellow( 'C' + message.col )
97-
:
98-
c.yellow('GENERAL')
99-
) +
100-
c.red('] ') +
101-
message.message + ' ' + message.rule.desc + ' (' + message.rule.id + ')');
102-
});
103-
};
104-
10578
cssLintPlugin.reporter = function(customReporter) {
106-
var reporter = defaultReporter;
79+
var reporter = csslint.getFormatter('text'), builtInReporter = true, output;
10780

10881
if (typeof customReporter === 'function') {
10982
reporter = customReporter;
83+
builtInReporter = false;
84+
} else if (typeof customReporter === 'string') {
85+
if (customReporter === 'fail') {
86+
return cssLintPlugin.failReporter();
87+
}
88+
89+
reporter = csslint.getFormatter(customReporter);
11090
}
11191

11292
if (typeof reporter === 'undefined') {
11393
throw new Error('Invalid reporter');
11494
}
11595

116-
return es.map(function(file, cb) {
96+
if (builtInReporter) {
97+
output = reporter.startFormat();
98+
}
99+
100+
return through.obj(function(file, enc, cb) {
117101
// Only report if CSSLint was ran and errors were found
118102
if (file.csslint && !file.csslint.success) {
119-
reporter(file);
103+
if (builtInReporter) {
104+
output += reporter.formatResults(file.csslint.originalReport, file.path);
105+
} else {
106+
reporter(file);
107+
}
120108
}
121109

122110
return cb(null, file);
111+
}, function(cb) {
112+
if (builtInReporter) {
113+
output += reporter.endFormat();
114+
115+
gutil.log(output);
116+
}
117+
118+
return cb();
123119
});
124120
};
125121

@@ -131,7 +127,7 @@ cssLintPlugin.addRule = function(rule) {
131127
};
132128

133129
cssLintPlugin.failReporter = function() {
134-
return es.map(function(file, cb) {
130+
return through.obj(function(file, enc, cb) {
135131
// Nothing to report or no errors
136132
if (!file.csslint || file.csslint.success) {
137133
return cb(null, file);

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
],
99
"dependencies": {
1010
"csslint": "^0.10.0",
11-
"event-stream": "^3.3.1",
1211
"gulp-util": "^3.0.4",
13-
"rcloader": "^0.1.4"
12+
"rcloader": "^0.1.4",
13+
"through2": "^2.0.0"
1414
},
1515
"devDependencies": {
1616
"coveralls": "^2.11.2",
@@ -19,7 +19,8 @@
1919
"jshint": "^2.7.0",
2020
"mocha": "^2.2.5",
2121
"rimraf": "^2.3.4",
22-
"should": "^6.0.3"
22+
"should": "^7.0.3",
23+
"sinon": "^1.15.4"
2324
},
2425
"scripts": {
2526
"clean": "rimraf coverage/",

test/main.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
/*jshint expr: true*/
2+
13
var cssLintPlugin = require('../');
24
var should = require('should');
35
var gutil = require('gulp-util');
46
var fs = require('fs');
57
var path = require('path');
6-
require('mocha');
8+
var sinon = require('sinon');
79

810
var getFile = function(filePath) {
911
filePath = 'test/'+filePath;
@@ -243,5 +245,41 @@ describe('gulp-csslint', function() {
243245
stream.write(file);
244246
stream.end();
245247
});
248+
249+
it('should use built-in formatter', sinon.test(function(done) {
250+
var a = 0;
251+
252+
var file = getFile('fixtures/usingImportant.css');
253+
254+
var lintStream = cssLintPlugin();
255+
var reporterStream = cssLintPlugin.reporter('checkstyle-xml');
256+
257+
sinon.stub(gutil, 'log');
258+
259+
reporterStream.on('data', function() {
260+
++a;
261+
});
262+
lintStream.on('data', function(file) {
263+
reporterStream.write(file);
264+
});
265+
lintStream.once('end', function() {
266+
reporterStream.end();
267+
});
268+
269+
reporterStream.once('end', function() {
270+
a.should.equal(1);
271+
sinon.assert.calledOnce(gutil.log, 'asdasd');
272+
sinon.assert.calledWith(gutil.log, '<?xml version="1.0" encoding="utf-8"?><checkstyle><file ' +
273+
'name="test/fixtures/usingImportant.css"><error line="2" column="1" severity="warning" ' +
274+
'message="Bad naming: .mybox" source="net.csslint.OOCSS"/><error line="3" column="3" severity="warning" ' +
275+
'message="Use of !important" source="net.csslint.Disallow!important"/></file></checkstyle>');
276+
277+
gutil.log.restore();
278+
done();
279+
});
280+
281+
lintStream.write(file);
282+
lintStream.end();
283+
}));
246284
});
247285
});

0 commit comments

Comments
 (0)