Skip to content

Commit 5337daf

Browse files
committed
Add maxWarnings option. Closes #17
1 parent 6027195 commit 5337daf

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ gulp.task('lint', () => {
2424
## Options
2525
* `configPath`
2626
* Pass a path to a valid configuration file and stop lesshint from looking for a `.lesshintrc` file.
27+
* `maxWarnings`
28+
* The maximum number of warnings to allow, before failing the task. Omit this option to always allow the task to pass.
2729

2830
## API
2931
* `lesshint.failOnError()`

index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ const lesshintPlugin = (options) => {
88
const lesshint = new Lesshint();
99

1010
options = options || {};
11-
options = lesshint.getConfig(options.configPath);
1211

13-
lesshint.configure(options);
12+
const config = lesshint.getConfig(options.configPath);
1413

14+
lesshint.configure(config);
15+
16+
let warningCount = 0;
17+
let maxWarnings;
1518
let error;
1619

20+
if (options.maxWarnings) {
21+
maxWarnings = parseInt(options.maxWarnings) || 0;
22+
}
23+
1724
return through.obj(function (file, enc, cb) {
1825
if (file.isStream()) {
1926
return cb(new PluginError('gulp-lesshint', 'Streaming not supported'));
@@ -27,6 +34,10 @@ const lesshintPlugin = (options) => {
2734
const contents = file.contents.toString();
2835
const results = lesshint.checkString(contents, file.path);
2936

37+
warningCount = results.reduce((sum, result) => {
38+
return sum + (result.severity === 'warning' ? 1 : 0);
39+
}, warningCount);
40+
3041
file.lesshint = {
3142
resultCount: results.length,
3243
results,
@@ -44,6 +55,13 @@ const lesshintPlugin = (options) => {
4455
this.emit('error', new PluginError('gulp-lesshint', error, {
4556
showStack: false,
4657
}));
58+
} else if (warningCount > maxWarnings) {
59+
const count = (warningCount === 1 ? 'warning' : 'warnings');
60+
const message = `Failed with ${ warningCount } ${ count }. Maximum allowed is ${ options.maxWarnings }.`;
61+
62+
this.emit('error', new PluginError('gulp-lesshint', message, {
63+
name: 'LesshintError'
64+
}));
4765
}
4866

4967
return cb();

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ describe('gulp-lesshint', () => {
175175
lintStream.end();
176176
});
177177

178+
it('should fail the task when the maximum number of warnings is exceeded.', (cb) => {
179+
const stream = lesshint({
180+
maxWarnings: 1
181+
});
182+
183+
stream.on('error', (error) => {
184+
assert.equal(error.message, 'Failed with 3 warnings. Maximum allowed is 1.');
185+
186+
cb();
187+
});
188+
189+
stream.on('end', cb);
190+
191+
stream.write(new File({
192+
base: __dirname,
193+
path: __dirname + '/fixture.less',
194+
contents: new Buffer(`
195+
.foo {
196+
color: red !important;
197+
}
198+
`)
199+
}));
200+
201+
stream.end();
202+
});
203+
178204
it('should ignore null files', () => {
179205
const stream = lesshint();
180206

0 commit comments

Comments
 (0)