From 57df54fe8a500227582c6b27e685509e7914a8d5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 23 Feb 2015 22:49:41 +0100 Subject: [PATCH] Automatically lookup .ccslintrc Fix #3 #13 Close #14 --- .editorconfig | 11 ++++++ .gitattributes | 2 ++ .gitignore | 2 ++ README.md | 4 +++ index.js | 52 ++++++++++++++--------------- package.json | 3 +- test/{csslintrc.json => .csslintrc} | 2 +- test/fixtures/usingImportant.css | 4 +++ test/main.js | 28 ++++++++++++++-- 9 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes rename test/{csslintrc.json => .csslintrc} (92%) create mode 100644 test/fixtures/usingImportant.css diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..421e692 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore index 3849d2e..16f80c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /node_modules /*.log +.idea/ +*.iml diff --git a/README.md b/README.md index c99ed9a..81fcfaa 100644 --- a/README.md +++ b/README.md @@ -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({ diff --git a/index.js b/index.js index c62b5b7..818fbb4 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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); + }); }); }; diff --git a/package.json b/package.json index 4e8d21a..c3c3927 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/csslintrc.json b/test/.csslintrc similarity index 92% rename from test/csslintrc.json rename to test/.csslintrc index 070a345..22135fd 100644 --- a/test/csslintrc.json +++ b/test/.csslintrc @@ -1,3 +1,3 @@ { "vendor-prefix": false -} \ No newline at end of file +} diff --git a/test/fixtures/usingImportant.css b/test/fixtures/usingImportant.css new file mode 100644 index 0000000..2dc8d1a --- /dev/null +++ b/test/fixtures/usingImportant.css @@ -0,0 +1,4 @@ +/* properties using important */ +.mybox { + border: 1px solid black !important; +} diff --git a/test/main.js b/test/main.js index 5d7118b..2c2a5fa 100644 --- a/test/main.js +++ b/test/main.js @@ -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; @@ -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);