Skip to content

Commit 39d9cee

Browse files
UltCombosindresorhus
authored andcommitted
Close PR #66: Add gulp-jshint-like reporters - fixes #21
1 parent 1123102 commit 39d9cee

File tree

6 files changed

+275
-73
lines changed

6 files changed

+275
-73
lines changed

index.js

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ module.exports = function (options) {
1616

1717
options = assign({esnext: false}, options);
1818

19-
var out = [];
2019
var checker = new Checker({esnext: !!options.esnext});
2120

2221
checker.registerDefaultRules();
@@ -59,48 +58,34 @@ module.exports = function (options) {
5958
return;
6059
}
6160

62-
try {
63-
var fixResults;
64-
var errors;
65-
var contents = file.contents.toString();
66-
67-
if (shouldFix) {
68-
fixResults = checker.fixString(contents, file.relative);
69-
errors = fixResults.errors;
70-
file.contents = new Buffer(fixResults.output);
71-
} else {
72-
errors = checker.checkString(contents, file.relative);
73-
}
74-
75-
var errorList = errors.getErrorList();
76-
77-
file.jscs = {
78-
success: true,
79-
errorCount: 0,
80-
errors: []
81-
};
82-
83-
if (errorList.length > 0) {
84-
file.jscs.success = false;
85-
file.jscs.errorCount = errorList.length;
86-
file.jscs.errors = errorList;
87-
}
88-
89-
errorList.forEach(function (err) {
90-
out.push(errors.explainError(err, true));
91-
});
92-
} catch (err) {
93-
out.push(err.stack.replace('null:', file.relative + ':'));
61+
var fixResults;
62+
var errors;
63+
var contents = file.contents.toString();
64+
65+
if (shouldFix) {
66+
fixResults = checker.fixString(contents, file.relative);
67+
errors = fixResults.errors;
68+
file.contents = new Buffer(fixResults.output);
69+
} else {
70+
errors = checker.checkString(contents, file.relative);
9471
}
9572

96-
cb(null, file);
97-
}, function (cb) {
98-
if (out.length > 0) {
99-
this.emit('error', new gutil.PluginError('gulp-jscs', out.join('\n\n'), {
100-
showStack: false
101-
}));
73+
var errorList = errors.getErrorList();
74+
75+
file.jscs = {
76+
success: true,
77+
errorCount: 0,
78+
errors: []
79+
};
80+
81+
if (errorList.length > 0) {
82+
file.jscs.success = false;
83+
file.jscs.errorCount = errorList.length;
84+
file.jscs.errors = errors;
10285
}
10386

104-
cb();
87+
cb(null, file);
10588
});
10689
};
90+
91+
module.exports.reporter = require('./reporters');

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=0.10.0"
1414
},
1515
"scripts": {
16-
"pretest": "jscs *.js",
16+
"pretest": "jscs *.js reporters",
1717
"test": "mocha"
1818
},
1919
"files": [
@@ -41,6 +41,7 @@
4141
"tildify": "^1.0.0"
4242
},
4343
"devDependencies": {
44-
"mocha": "*"
44+
"mocha": "*",
45+
"stream-assert": "^2.0.2"
4546
}
4647
}

reporters/fail.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
var PluginError = require('gulp-util').PluginError;
3+
var through = require('through2');
4+
5+
module.exports = function (failImmediately) {
6+
// paths to files that failed JSCS
7+
var fails = false;
8+
9+
return through.obj(function (file, enc, cb) {
10+
var error = null;
11+
12+
// check for failure
13+
if (file.jscs && !file.jscs.success) {
14+
if (failImmediately) {
15+
error = new PluginError('gulp-jscs', {
16+
message: 'JSCS failed for: ' + file.path,
17+
showStack: false
18+
});
19+
} else {
20+
(fails = fails || []).push(file.path);
21+
}
22+
}
23+
24+
cb(error, file);
25+
}, function (cb) {
26+
if (!failImmediately && fails) {
27+
// calling `cb(err)` would not emit the `end` event,
28+
// so emit the error explicitly and call `cb()` afterwards.
29+
this.emit('error', new PluginError('gulp-jscs', {
30+
message: 'JSCS failed for: ' + fails.join(', '),
31+
showStack: false
32+
}));
33+
}
34+
35+
cb();
36+
});
37+
};

reporters/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
var PluginError = require('gulp-util').PluginError;
3+
var through = require('through2');
4+
var loadReporter = require('./loadReporter');
5+
var failReporter = require('./fail');
6+
7+
module.exports = function (reporter) {
8+
if (reporter === 'fail' || reporter === 'failImmediately') {
9+
return failReporter(reporter === 'failImmediately');
10+
}
11+
12+
var rpt = loadReporter(reporter);
13+
14+
if (typeof rpt !== 'function') {
15+
throw new PluginError('gulp-jscs', 'Invalid reporter');
16+
}
17+
18+
// return stream that reports stuff
19+
return through.obj(function (file, enc, cb) {
20+
if (file.jscs && !file.jscs.success) {
21+
rpt([file.jscs.errors]);
22+
}
23+
24+
cb(null, file);
25+
});
26+
};

reporters/loadReporter.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
var getReporter = require('jscs/lib/cli-config').getReporter;
3+
4+
module.exports = function (reporter) {
5+
// we want the function
6+
if (typeof reporter === 'function') return reporter;
7+
8+
// load JSCS built-in or absolute path or module reporters
9+
return getReporter(reporter).writer;
10+
};

0 commit comments

Comments
 (0)