Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ gulp.task('lint', function() {
});
```

## Custom Rules

The plugin exposes the csslint `addRule` method which allows you to define custom rules that are run in addition to the included rules. [Creating your own rules](https://github.com/CSSLint/csslint/wiki/Working-with-Rules) works exactly like when using csslint directly.

```javascript
var csslint = require(‘gulp-csslint’);

csslint.addRule({
// rule object
});

gulp.task(‘lint’, function() {
gulp.files('lib/*.css')
.pipe(csslint())
.pipe(csslint.reporter())
});
```

## Fail on errors

Pipe the file stream to `csslint.failReporter()` to fail on errors.
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ cssLintPlugin.reporter = function(customReporter) {
});
};

cssLintPlugin.addRule = function(rule) {
if(typeof rule !== 'object') {
throw new Error('Invalid rule: rules need to be objects.');
}
csslint.addRule(rule);
};

cssLintPlugin.failReporter = function() {
return es.map(function(file, cb) {
// Nothing to report or no errors
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/addRule.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: red;
}
50 changes: 50 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,55 @@ describe('gulp-csslint', function() {
stream.write(file);
stream.end();
});

it('should report added rule linting', function(done) {
var a = 0;

var file = getFile('fixtures/addRule.css');
cssLintPlugin.addRule({
id: 'oocss',
name: 'OOCSS',
desc: 'Class names must follow pattern',
browsers: 'All',

//initialization
init: function(parser, reporter) {
'use strict';
var rule = this;
parser.addListener('startrule', function(event) {
var line = event.line,
col = event.col;

for (var i=0,len=event.selectors.length; i < len; i++) {
var selectors = event.selectors[i].text.split(/(?=\.)/);
for (var s=0,l=selectors.length; s < l; s++){
var selector = selectors[s].trim();
if(selector.charAt(0) !== '.'){
return;
}
if(!selector.match(/^\.(_)?(o|c|u|is|has|js|qa)-[a-z0-9]+$/)){
reporter.warn('Bad naming: '+selector, line, col, rule);
}
}
}
});
}
});

var stream = cssLintPlugin();
stream.on('data', function(newFile) {
++a;
should.exist(newFile.csslint.success);
newFile.csslint.success.should.equal(false);
should.exist(newFile.csslint.results);
});
stream.once('end', function() {
a.should.equal(1);
done();
});

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