Skip to content

Commit 825ff28

Browse files
committed
Merge pull request #25 from lukasoppermann/patch-1
Adding addRule function
2 parents 2a616d2 + 1b07d90 commit 825ff28

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ gulp.task('lint', function() {
8989
});
9090
```
9191

92+
## Custom Rules
93+
94+
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.
95+
96+
```javascript
97+
var csslint = require(‘gulp-csslint’);
98+
99+
csslint.addRule({
100+
// rule object
101+
});
102+
103+
gulp.task(‘lint’, function() {
104+
gulp.files('lib/*.css')
105+
.pipe(csslint())
106+
.pipe(csslint.reporter())
107+
});
108+
```
109+
92110
## Fail on errors
93111

94112
Pipe the file stream to `csslint.failReporter()` to fail on errors.

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ cssLintPlugin.reporter = function(customReporter) {
122122
});
123123
};
124124

125+
cssLintPlugin.addRule = function(rule) {
126+
if(typeof rule !== 'object') {
127+
throw new Error('Invalid rule: rules need to be objects.');
128+
}
129+
csslint.addRule(rule);
130+
};
131+
125132
cssLintPlugin.failReporter = function() {
126133
return es.map(function(file, cb) {
127134
// Nothing to report or no errors

test/fixtures/addRule.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: red;
3+
}

test/main.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,55 @@ describe('gulp-csslint', function() {
173173
stream.write(file);
174174
stream.end();
175175
});
176+
177+
it('should report added rule linting', function(done) {
178+
var a = 0;
179+
180+
var file = getFile('fixtures/addRule.css');
181+
cssLintPlugin.addRule({
182+
id: 'oocss',
183+
name: 'OOCSS',
184+
desc: 'Class names must follow pattern',
185+
browsers: 'All',
186+
187+
//initialization
188+
init: function(parser, reporter) {
189+
'use strict';
190+
var rule = this;
191+
parser.addListener('startrule', function(event) {
192+
var line = event.line,
193+
col = event.col;
194+
195+
for (var i=0,len=event.selectors.length; i < len; i++) {
196+
var selectors = event.selectors[i].text.split(/(?=\.)/);
197+
for (var s=0,l=selectors.length; s < l; s++){
198+
var selector = selectors[s].trim();
199+
if(selector.charAt(0) !== '.'){
200+
return;
201+
}
202+
if(!selector.match(/^\.(_)?(o|c|u|is|has|js|qa)-[a-z0-9]+$/)){
203+
reporter.warn('Bad naming: '+selector, line, col, rule);
204+
}
205+
}
206+
}
207+
});
208+
}
209+
});
210+
211+
var stream = cssLintPlugin();
212+
stream.on('data', function(newFile) {
213+
++a;
214+
should.exist(newFile.csslint.success);
215+
newFile.csslint.success.should.equal(false);
216+
should.exist(newFile.csslint.results);
217+
});
218+
stream.once('end', function() {
219+
a.should.equal(1);
220+
done();
221+
});
222+
223+
stream.write(file);
224+
stream.end();
225+
});
176226
});
177227
});

0 commit comments

Comments
 (0)