From 10e14f5a01148e59f9d22efae8dd0bce9b0468a5 Mon Sep 17 00:00:00 2001 From: Lukas Oppermann Date: Fri, 17 Apr 2015 09:17:34 +0200 Subject: [PATCH 1/2] Adding addRule function --- README.md | 18 ++++++++++++++++++ index.js | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/README.md b/README.md index 9128194..a0bedf6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 8a2ceec..e4e0b2e 100644 --- a/index.js +++ b/index.js @@ -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 From 1b07d9030396202e8173881f0a6cb33511e03007 Mon Sep 17 00:00:00 2001 From: Lukas Oppermann Date: Tue, 28 Apr 2015 08:44:29 +0200 Subject: [PATCH 2/2] adding test --- test/fixtures/addRule.css | 3 +++ test/main.js | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/fixtures/addRule.css diff --git a/test/fixtures/addRule.css b/test/fixtures/addRule.css new file mode 100644 index 0000000..a15c877 --- /dev/null +++ b/test/fixtures/addRule.css @@ -0,0 +1,3 @@ +.foo { + color: red; +} diff --git a/test/main.js b/test/main.js index 2c2a5fa..bb0ec44 100644 --- a/test/main.js +++ b/test/main.js @@ -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(); + }); }); });