Skip to content

Commit

Permalink
Merge cfbdf20 into c8e3464
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed May 28, 2015
2 parents c8e3464 + cfbdf20 commit 5350e7e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 38 deletions.
88 changes: 52 additions & 36 deletions index.js
@@ -1,11 +1,9 @@
/*jshint node:true */

'use strict';

var gutil = require('gulp-util');
var c = gutil.colors;
var error = gutil.PluginError;
var es = require('event-stream');
var through = require('through2');
var fs = require('fs');
var csslint = require('csslint').CSSLint;
var RcLoader = require('rcloader');

Expand All @@ -26,14 +24,13 @@ var formatOutput = function(report, file, options) {
return err;
});

var output = {
return {
originalReport: report,
errorCount: results.length,
success: false,
results: results,
options: options
};

return output;
};

var cssLintPlugin = function(options) {
Expand All @@ -48,9 +45,9 @@ var cssLintPlugin = function(options) {
ruleset[rule.id] = 1;
});

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

rcLoader.for(file.path, function(err, opts) {
if (err) return cb(err);
Expand All @@ -77,47 +74,56 @@ var cssLintPlugin = function(options) {
});
};

var defaultReporter = function(file) {
var errorCount = file.csslint.errorCount;
var plural = errorCount === 1 ? '' : 's';

gutil.log(c.cyan(errorCount)+' error'+plural+' found in '+c.magenta(file.path));

file.csslint.results.forEach(function(result) {
var message = result.error;
gutil.log(
c.red('[') +
(
typeof message.line !== 'undefined' ?
c.yellow( 'L' + message.line ) +
c.red(':') +
c.yellow( 'C' + message.col )
:
c.yellow('GENERAL')
) +
c.red('] ') +
message.message + ' ' + message.rule.desc + ' (' + message.rule.id + ')');
});
};
cssLintPlugin.reporter = function(customReporter, toFile, formatterOptions) {
var reporter = csslint.getFormatter('text'), builtInReporter = true, output;

cssLintPlugin.reporter = function(customReporter) {
var reporter = defaultReporter;
if (typeof toFile === 'object') {
formatterOptions = toFile;
toFile = undefined;
}

if (typeof customReporter === 'function') {
reporter = customReporter;
builtInReporter = false;
} else if (typeof customReporter === 'string') {
if (customReporter === 'fail') {
return cssLintPlugin.failReporter();
}

reporter = csslint.getFormatter(customReporter);
}

if (typeof reporter === 'undefined') {
throw new Error('Invalid reporter');
}

return es.map(function(file, cb) {
if (builtInReporter) {
output = reporter.startFormat();
}

return through.obj(function(file, enc, cb) {
// Only report if CSSLint was ran and errors were found
if (file.csslint && !file.csslint.success) {
reporter(file);
if (builtInReporter) {
output += reporter.formatResults(file.csslint.originalReport, file.path, formatterOptions);
} else {
reporter(file);
}
}

return cb(null, file);
}, function(cb) {
if (builtInReporter) {
output += reporter.endFormat();

if (toFile) {
return fs.writeFile(toFile, output, cb);
} else {
gutil.log(output);
}
}

return cb();
});
};

Expand All @@ -128,8 +134,18 @@ cssLintPlugin.addRule = function(rule) {
csslint.addRule(rule);
};

cssLintPlugin.addFormatter = function(formatter) {
if (typeof formatter !== 'object' || !formatter.id || !formatter.startFormat || !formatter.endFormat ||
!formatter.endFormat || !formatter.formatResults) {
throw new Error('Invalid formatter: formatters need to be objects, and contain "id", "name", "startFormat", ' +
'"endFormat" and "formatResults"');
}

csslint.addFormatter(formatter);
};

cssLintPlugin.failReporter = function() {
return es.map(function(file, cb) {
return through.obj(function(file, enc, cb) {
// Nothing to report or no errors
if (!file.csslint || file.csslint.success) {
return cb(null, file);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -8,12 +8,13 @@
],
"dependencies": {
"csslint": "^0.10.0",
"event-stream": "^3.3.1",
"gulp-util": "^3.0.4",
"rcloader": "^0.1.4"
"rcloader": "^0.1.4",
"through2": "^0.6.5"
},
"devDependencies": {
"coveralls": "^2.11.2",
"intercept-stdout": "0.1.1",
"istanbul": "^0.3.14",
"jscs": "^1.13.1",
"jshint": "^2.7.0",
Expand Down
37 changes: 37 additions & 0 deletions test/main.js
Expand Up @@ -3,6 +3,7 @@ var should = require('should');
var gutil = require('gulp-util');
var fs = require('fs');
var path = require('path');
var intercept = require('intercept-stdout');
require('mocha');

var getFile = function(filePath) {
Expand Down Expand Up @@ -223,5 +224,41 @@ describe('gulp-csslint', function() {
stream.write(file);
stream.end();
});

it('should use built-in formatter', function(done) {
var a = 0;

var file = getFile('fixtures/usingImportant.css');

var lintStream = cssLintPlugin();
var reporterStream = cssLintPlugin.reporter('checkstyle-xml');
var stdoutOutput = '';

var unhookStdout = intercept(function intercept(string) {
stdoutOutput += string;
});

reporterStream.on('data', function() {
++a;
});
lintStream.on('data', function(file) {
reporterStream.write(file);
});
lintStream.once('end', function() {
reporterStream.end();
});

reporterStream.once('end', function() {
unhookStdout();

a.should.equal(1);
stdoutOutput.indexOf('checkstyle').should.be.above(-1);

done();
});

lintStream.write(file);
lintStream.end();
});
});
});

0 comments on commit 5350e7e

Please sign in to comment.