Skip to content

Commit 43c00ba

Browse files
justinsajwilsson
authored andcommitted
Add failOnWarning support (#53)
1 parent cb9576b commit 43c00ba

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ gulp.task('lint', () => {
1717
// Options
1818
}))
1919
.pipe(lesshint.reporter('reporter-name')) // Leave empty to use the default, "stylish"
20-
.pipe(lesshint.failOnError()); // Use this to fail the task on lint errors
20+
.pipe(lesshint.failOnError()) // Use this to fail the task on lint errors
21+
.pipe(lesshint.failOnWarning()); // Use this to fail the task on lint warnings
2122
});
2223
```
2324

@@ -30,6 +31,8 @@ gulp.task('lint', () => {
3031
## API
3132
* `lesshint.failOnError()`
3233
* Use this to fail the task when there are at least one lint result with a severity of `error`.
34+
* `lesshint.failOnWarning()`
35+
* Use this to fail the task when there are at least one lint result with a severity of `warning`.**NOTE**: this does not respect the `maxWarnings` option.
3336

3437
## Reporters
3538
If no reporter name is passed, the default `lesshint-reporter-stylish` will be used which just prints everything with different colors.

index.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const PluginError = require('plugin-error');
44
const Lesshint = require('lesshint').Lesshint;
55
const through = require('through2');
66

7-
const { getSeverityCount, isError, isExcluded, isWarning, pluralize } = require('./utils');
7+
const { isError, isExcluded, isWarning, pluralize, severityCount } = require('./utils');
88

99
const lesshintPlugin = (options = {}) => {
1010
const lesshint = new Lesshint();
@@ -33,7 +33,7 @@ const lesshintPlugin = (options = {}) => {
3333
const contents = file.contents.toString();
3434
const results = lesshint.checkString(contents, file.path);
3535

36-
warningCount += getSeverityCount(results, isWarning);
36+
warningCount += severityCount(results, isWarning);
3737

3838
file.lesshint = {
3939
resultCount: results.length,
@@ -89,7 +89,7 @@ lesshintPlugin.failOnError = () => {
8989

9090
return through.obj((file, enc, cb) => {
9191
if (file.lesshint) {
92-
errorCount += getSeverityCount(file.lesshint.results, isError);
92+
errorCount += severityCount(file.lesshint.results, isError);
9393
}
9494

9595
return cb(null, file);
@@ -108,4 +108,28 @@ lesshintPlugin.failOnError = () => {
108108
});
109109
};
110110

111+
lesshintPlugin.failOnWarning = () => {
112+
let warningCount = 0;
113+
114+
return through.obj((file, enc, cb) => {
115+
if (file.lesshint) {
116+
warningCount += severityCount(file.lesshint.results, isWarning);
117+
}
118+
119+
return cb(null, file);
120+
}, function (cb) {
121+
if (!warningCount) {
122+
return cb();
123+
}
124+
125+
const message = `Failed with ${ warningCount } ${ pluralize('warning', warningCount) }`;
126+
127+
this.emit('error', new PluginError('gulp-lesshint', message, {
128+
name: 'LesshintError',
129+
}));
130+
131+
return cb();
132+
});
133+
};
134+
111135
module.exports = lesshintPlugin;

test/test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe('gulp-lesshint', () => {
140140
stream.end();
141141
});
142142

143-
it('should emit errors when asked to', (cb) => {
143+
it('should fail on errors when asked to', (cb) => {
144144
const lintStream = lesshint({
145145
configPath: './test/config.json'
146146
});
@@ -175,6 +175,41 @@ describe('gulp-lesshint', () => {
175175
lintStream.end();
176176
});
177177

178+
it('should fail on warnings when asked to', (cb) => {
179+
const lintStream = lesshint({
180+
configPath: './test/config.json'
181+
});
182+
const failStream = lesshint.failOnWarning();
183+
184+
lintStream.on('data', (file) => {
185+
failStream.write(file);
186+
});
187+
188+
lintStream.once('end', () => {
189+
failStream.end();
190+
});
191+
192+
failStream.on('data', () => {});
193+
194+
failStream.on('error', (error) => {
195+
assert.equal(error.name, 'LesshintError');
196+
197+
cb();
198+
});
199+
200+
lintStream.write(new File({
201+
base: __dirname,
202+
path: __dirname + '/fixture.less',
203+
contents: Buffer.from(`
204+
.foo {
205+
color: red !important;
206+
}
207+
`)
208+
}));
209+
210+
lintStream.end();
211+
});
212+
178213
it('should fail the task when the maximum number of warnings is exceeded.', (cb) => {
179214
const stream = lesshint({
180215
maxWarnings: 1

utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
const minimatch = require('minimatch');
44

55
module.exports = {
6-
getSeverityCount (results, isSeverityFn) {
7-
return results.reduce((sum, result) => {
8-
return sum + (isSeverityFn(result.severity) ? 1 : 0);
9-
}, 0);
10-
},
11-
126
isError (severity) {
137
return severity === 'error';
148
},
@@ -30,4 +24,10 @@ module.exports = {
3024
pluralize (singular, count) {
3125
return count === 1 ? singular : `${ singular }s`;
3226
},
27+
28+
severityCount (results, isSeverityFn) {
29+
return results.reduce((sum, result) => {
30+
return sum + (isSeverityFn(result.severity) ? 1 : 0);
31+
}, 0);
32+
},
3333
};

0 commit comments

Comments
 (0)