Skip to content

Commit 32838e7

Browse files
committed
Merge pull request #17 from SimenB/auto-find-csslintrc
Automatically lookup .ccslintrc
2 parents 2bc33a8 + 57df54f commit 32838e7

File tree

9 files changed

+77
-31
lines changed

9 files changed

+77
-31
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 2
8+
trim_trailing_whitespace = true
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/node_modules
22
/*.log
3+
.idea/
4+
*.iml

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ gulp.task('css', function() {
2828
#### ruleConfiguration
2929
Type: `Object`
3030

31+
If you pass `lookup: false`, the local .csslintrc is not looked up automatically.
32+
3133
You can pass rule configuration as an object. See the [list of rules by ID on the CSSLint wiki](https://github.com/stubbornella/csslint/wiki/Rules-by-ID) for valid rule IDs.
3234

35+
Any properties passed wil be in _addition_ to (or overwriting) the ones in .csslintrc (unless `lookup: false` is passed).
36+
3337
```javascript
3438
gulp.src('client/css/*.css')
3539
.pipe(csslint({

index.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
var gutil = require('gulp-util');
66
var c = gutil.colors;
7+
var error = gutil.PluginError;
78
var es = require('event-stream');
89
var fs = require('fs');
910
var csslint = require('csslint').CSSLint;
11+
var RcLoader = require('rcloader');
1012

1113
var formatOutput = function(report, file, options) {
1214
if (!report.messages.length) {
@@ -40,41 +42,39 @@ var cssLintPlugin = function(options) {
4042

4143
var ruleset = {};
4244

43-
// Read CSSLint options from a specified csslintrc file.
44-
if (typeof options === 'string') {
45-
// Don't catch readFile errors, let them bubble up
46-
var externalOptions = fs.readFileSync('./'+options);
47-
48-
try {
49-
options = JSON.parse(externalOptions);
50-
}
51-
catch(err) {
52-
throw new Error('Error parsing csslintrc: '+err);
53-
}
54-
}
45+
var rcLoader = new RcLoader('.csslintrc', options, { loader: 'async' });
5546

5647
// Build a list of all available rules
5748
csslint.getRules().forEach(function(rule) {
5849
ruleset[rule.id] = 1;
5950
});
6051

61-
for (var rule in options) {
62-
if (!options[rule]) {
63-
// Remove rules that are turned off
64-
delete ruleset[rule];
65-
}
66-
else {
67-
ruleset[rule] = options[rule];
68-
}
69-
}
70-
7152
return es.map(function(file, cb) {
72-
var report = csslint.verify(String(file.contents), ruleset);
53+
if (file.isNull()) return cb(null, file); // pass along
54+
if (file.isStream()) return cb(new error('gulp-csslint: Streaming not supported'));
55+
56+
rcLoader.for(file.path, function (err, opts) {
57+
if (err) return cb(err);
58+
59+
var str = file.contents.toString('utf8');
60+
61+
for (var rule in opts) {
62+
if (!opts[rule]) {
63+
// Remove rules that are turned off
64+
delete ruleset[rule];
65+
}
66+
else {
67+
ruleset[rule] = opts[rule];
68+
}
69+
}
70+
71+
var report = csslint.verify(str, ruleset);
7372

74-
// send status down-stream
75-
file.csslint = formatOutput(report, file, options);
73+
// send status down-stream
74+
file.csslint = formatOutput(report, file, ruleset);
7675

77-
cb(null, file);
76+
cb(null, file);
77+
});
7878
});
7979
};
8080

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"dependencies": {
77
"csslint": "^0.10.0",
88
"event-stream": "^3.3.0",
9-
"gulp-util": "^3.0.4"
9+
"gulp-util": "^3.0.4",
10+
"rcloader": "^0.1.4"
1011
},
1112
"devDependencies": {
1213
"mocha": "^2.2.1",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"vendor-prefix": false
3-
}
3+
}

test/fixtures/usingImportant.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* properties using important */
2+
.mybox {
3+
border: 1px solid black !important;
4+
}

test/main.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ describe('gulp-csslint', function() {
109109
it('should support options', function(done) {
110110
var a = 0;
111111

112-
var file = getFile('fixtures/missingPrefixes.css');
112+
var file = getFile('fixtures/usingImportant.css');
113113

114114
var stream = cssLintPlugin({
115-
'vendor-prefix': false
115+
important: false
116116
});
117117
stream.on('data', function(newFile) {
118118
++a;
@@ -135,7 +135,29 @@ describe('gulp-csslint', function() {
135135

136136
var file = getFile('fixtures/missingPrefixes.css');
137137

138-
var stream = cssLintPlugin('test/csslintrc.json');
138+
var stream = cssLintPlugin('test/.csslintrc');
139+
stream.on('data', function(newFile) {
140+
++a;
141+
should.exist(newFile.csslint.success);
142+
newFile.csslint.success.should.equal(true);
143+
should.not.exist(newFile.csslint.results);
144+
should.not.exist(newFile.csslint.opt);
145+
});
146+
stream.once('end', function() {
147+
a.should.equal(1);
148+
done();
149+
});
150+
151+
stream.write(file);
152+
stream.end();
153+
});
154+
155+
it('should find csslintrc automatically', function(done) {
156+
var a = 0;
157+
158+
var file = getFile('fixtures/missingPrefixes.css');
159+
160+
var stream = cssLintPlugin();
139161
stream.on('data', function(newFile) {
140162
++a;
141163
should.exist(newFile.csslint.success);

0 commit comments

Comments
 (0)