Skip to content
This repository has been archived by the owner on Feb 27, 2018. It is now read-only.

Commit

Permalink
Automatically lookup .ccslintrc
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 31, 2015
1 parent d77f4c7 commit 57df54f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 31 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules
/*.log
.idea/
*.iml
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ gulp.task('css', function() {
#### ruleConfiguration
Type: `Object`

If you pass `lookup: false`, the local .csslintrc is not looked up automatically.

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.

Any properties passed wil be in _addition_ to (or overwriting) the ones in .csslintrc (unless `lookup: false` is passed).

```javascript
gulp.src('client/css/*.css')
.pipe(csslint({
Expand Down
52 changes: 26 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

var gutil = require('gulp-util');
var c = gutil.colors;
var error = gutil.PluginError;
var es = require('event-stream');
var fs = require('fs');
var csslint = require('csslint').CSSLint;
var RcLoader = require('rcloader');

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

var ruleset = {};

// Read CSSLint options from a specified csslintrc file.
if (typeof options === 'string') {
// Don't catch readFile errors, let them bubble up
var externalOptions = fs.readFileSync('./'+options);

try {
options = JSON.parse(externalOptions);
}
catch(err) {
throw new Error('Error parsing csslintrc: '+err);
}
}
var rcLoader = new RcLoader('.csslintrc', options, { loader: 'async' });

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

for (var rule in options) {
if (!options[rule]) {
// Remove rules that are turned off
delete ruleset[rule];
}
else {
ruleset[rule] = options[rule];
}
}

return es.map(function(file, cb) {
var report = csslint.verify(String(file.contents), ruleset);
if (file.isNull()) return cb(null, file); // pass along
if (file.isStream()) return cb(new error('gulp-csslint: Streaming not supported'));

rcLoader.for(file.path, function (err, opts) {
if (err) return cb(err);

var str = file.contents.toString('utf8');

for (var rule in opts) {
if (!opts[rule]) {
// Remove rules that are turned off
delete ruleset[rule];
}
else {
ruleset[rule] = opts[rule];
}
}

var report = csslint.verify(str, ruleset);

// send status down-stream
file.csslint = formatOutput(report, file, options);
// send status down-stream
file.csslint = formatOutput(report, file, ruleset);

cb(null, file);
cb(null, file);
});
});
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"csslint": "^0.10.0",
"event-stream": "^3.3.0",
"gulp-util": "^3.0.4"
"gulp-util": "^3.0.4",
"rcloader": "^0.1.4"
},
"devDependencies": {
"mocha": "^2.2.1",
Expand Down
2 changes: 1 addition & 1 deletion test/csslintrc.json → test/.csslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"vendor-prefix": false
}
}
4 changes: 4 additions & 0 deletions test/fixtures/usingImportant.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* properties using important */
.mybox {
border: 1px solid black !important;
}
28 changes: 25 additions & 3 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ describe('gulp-csslint', function() {
it('should support options', function(done) {
var a = 0;

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

var stream = cssLintPlugin({
'vendor-prefix': false
important: false
});
stream.on('data', function(newFile) {
++a;
Expand All @@ -135,7 +135,29 @@ describe('gulp-csslint', function() {

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

var stream = cssLintPlugin('test/csslintrc.json');
var stream = cssLintPlugin('test/.csslintrc');
stream.on('data', function(newFile) {
++a;
should.exist(newFile.csslint.success);
newFile.csslint.success.should.equal(true);
should.not.exist(newFile.csslint.results);
should.not.exist(newFile.csslint.opt);
});
stream.once('end', function() {
a.should.equal(1);
done();
});

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

it('should find csslintrc automatically', function(done) {
var a = 0;

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

var stream = cssLintPlugin();
stream.on('data', function(newFile) {
++a;
should.exist(newFile.csslint.success);
Expand Down

0 comments on commit 57df54f

Please sign in to comment.