Socket
Socket
Sign inDemoInstall

gulp-csslint

Package Overview
Dependencies
62
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.5 to 0.2.0

139

index.js

@@ -1,10 +0,8 @@

/*jshint node:true */
'use strict';
var gutil = require('gulp-util');
var c = gutil.colors;
var es = require('event-stream');
var fs = require('fs');
var error = gutil.PluginError;
var through = require('through2');
var csslint = require('csslint').CSSLint;
var RcLoader = require('rcloader');

@@ -28,3 +26,4 @@ var formatOutput = function(report, file, options) {

var output = {
return {
originalReport: report,
errorCount: results.length,

@@ -35,24 +34,11 @@ success: false,

};
return output;
};
var cssLintPlugin = function(options) {
if (!options) options = {};
options = 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);
var rcLoader = new RcLoader('.csslintrc', options, { loader: 'async' });
try {
options = JSON.parse(externalOptions);
}
catch(err) {
throw new Error('Error parsing csslintrc: '+err);
}
}
// Build a list of all available rules

@@ -63,42 +49,30 @@ csslint.getRules().forEach(function(rule) {

for (var rule in options) {
if (!options[rule]) {
// Remove rules that are turned off
delete ruleset[rule];
}
else {
ruleset[rule] = options[rule];
}
}
return through.obj(function(file, enc, cb) {
if (file.isNull()) return cb(null, file); // pass along
if (file.isStream()) return cb(new error('gulp-csslint: Streaming not supported'), file);
return es.map(function(file, cb) {
var report = csslint.verify(String(file.contents), ruleset);
var content = file.contents.toString(enc);
// send status down-stream
file.csslint = formatOutput(report, file, options);
if (!content) return cb(null, file); // pass along
cb(null, file);
});
};
rcLoader.for(file.path, function(err, opts) {
if (err) return cb(err);
var defaultReporter = function(file) {
var errorCount = file.csslint.errorCount;
var plural = errorCount === 1 ? '' : 's';
for (var rule in opts) {
if (!opts[rule]) {
// Remove rules that are turned off
delete ruleset[rule];
}
else {
ruleset[rule] = opts[rule];
}
}
gutil.log(c.cyan(errorCount)+' error'+plural+' found in '+c.magenta(file.path));
var report = csslint.verify(content, ruleset);
file.csslint.results.forEach(function(result) {
var message = result.error;
gutil.log(
c.red('[') +
(
typeof message.line !== 'undefined' ?
c.yellow( 'L' + message.line ) +
c.red(':') +
c.yellow( 'C' + message.col )
:
c.yellow('GENERAL')
) +
c.red('] ') +
message.message + ' ' + message.rule.desc + ' (' + message.rule.id + ')');
// send status down-stream
file.csslint = formatOutput(report, file, ruleset);
cb(null, file);
});
});

@@ -108,8 +82,18 @@ };

cssLintPlugin.reporter = function(customReporter) {
var reporter = defaultReporter;
var reporter = csslint.getFormatter('text');
var builtInReporter = true;
var output;
if (typeof customReporter === 'function') {
reporter = customReporter;
builtInReporter = false;
}
else if (typeof customReporter === 'string') {
if (customReporter === 'fail') {
return cssLintPlugin.failReporter();
}
reporter = csslint.getFormatter(customReporter);
}
if (typeof reporter === 'undefined') {

@@ -119,14 +103,41 @@ throw new Error('Invalid reporter');

return es.map(function(file, cb) {
// Only report if CSSLint was ran and errors were found
if (file.csslint && !file.csslint.success) {
reporter(file);
if (builtInReporter) {
output = reporter.startFormat();
}
return through.obj(
function(file, enc, cb) {
// Only report if CSSLint was ran and errors were found
if (file.csslint && !file.csslint.success) {
if (builtInReporter) {
output += reporter.formatResults(file.csslint.originalReport, file.path);
}
else {
reporter(file);
}
}
return cb(null, file);
},
function(cb) {
if (builtInReporter) {
output += reporter.endFormat();
gutil.log(output);
}
return cb();
}
);
};
return cb(null, file);
});
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) {
cssLintPlugin.failReporter = function() {
return through.obj(function(file, enc, cb) {
// Nothing to report or no errors

@@ -133,0 +144,0 @@ if (!file.csslint || file.csslint.success) {

{
"name": "gulp-csslint",
"version": "0.1.5",
"version": "0.2.0",
"description": "CSSLint plugin for gulp",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"csslint": "^0.10.0",
"event-stream": "^3.1.2",
"gulp-util": "^3.0.0"
"gulp-util": "^3.0.4",
"rcloader": "^0.1.4",
"through2": "^2.0.0"
},
"devDependencies": {
"mocha": "^1.18.2",
"should": "^3.3.1"
"coveralls": "^2.11.2",
"istanbul": "^0.3.14",
"jscs": "^1.13.1",
"jshint": "^2.7.0",
"mocha": "^2.2.5",
"rimraf": "^2.3.4",
"should": "^7.0.3",
"sinon": "^1.15.4"
},
"scripts": {
"test": "./node_modules/.bin/mocha"
"clean": "rimraf coverage/",
"lint": "jscs index.js test/ && jshint index.js test/",
"pretest": "npm run lint",
"test": "mocha",
"precover": "npm run lint && npm run clean",
"cover": "istanbul cover _mocha"
},

@@ -18,0 +33,0 @@ "repository": {

@@ -1,2 +0,2 @@

# gulp-csslint [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url]
# gulp-csslint [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![Dependency status][david-image]][david-url]
> CSSLint plugin for gulp 3

@@ -14,3 +14,3 @@

```javascript
```js
var csslint = require('gulp-csslint');

@@ -32,5 +32,9 @@

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.
```javascript
Any properties passed wil be in _addition_ to (or overwriting) the ones in .csslintrc (unless `lookup: false` is passed).
```js
gulp.src('client/css/*.css')

@@ -50,3 +54,3 @@ .pipe(csslint({

```javascript
```js
gulp.src('client/css/*.css')

@@ -61,3 +65,3 @@ .pipe(csslint('csslintrc.json'))

```javascript
```js
file.csslint.success = true; // or false

@@ -69,7 +73,20 @@ file.csslint.errorCount = 0; // number of errors returned by CSSLint

## Custom Reporters
## Using reporters
Custom reporter functions can be passed as `cssline.reporter(reporterFunc)`. The reporter function will be called for each linted file and passed the file object as described above.
Several reporters come built-in to css-lint. To use one of these reporters, pass the name to `csslint.reporter`.
```javascript
For a list of all reporters supported by `csslint`, see the [csslint wiki](https://github.com/CSSLint/csslint/wiki/Command-line-interface#--format).
```js
gulp.task('lint', function() {
gulp.files('lib/*.css')
.pipe(csslint())
.pipe(csslint.reporter('junit-xml'));
```
### Custom reporters
Custom reporter functions can be passed as `csslint.reporter(reporterFunc)`. The reporter function will be called for each linted file and passed the file object as described above.
```js
var csslint = require('gulp-csslint');

@@ -93,5 +110,43 @@ var gutil = require('gulp-util');

## Custom rules
Use the `csslint.addRule(rule)` method to define custom rules that run in addition to the rules defined in the csslintrc file. See [Working with Rules](https://github.com/CSSLint/csslint/wiki/Working-with-Rules) for details.
```js
var csslint = require('gulp-csslint');
csslint.addRule({
// rule information
});
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.
```js
var csslint = require('gulp-csslint');
gulp.task('lint', function() {
gulp.files('lib/*.css')
.pipe(csslint())
.pipe(csslint.reporter()) // Display errors
.pipe(csslint.reporter('fail')); // Fail on error (or csslint.failReporter())
});
```
[travis-url]: http://travis-ci.org/lazd/gulp-csslint
[travis-image]: https://secure.travis-ci.org/lazd/gulp-csslint.png?branch=master
[travis-image]: https://img.shields.io/travis/lazd/gulp-csslint.svg
[npm-url]: https://npmjs.org/package/gulp-csslint
[npm-image]: https://badge.fury.io/js/gulp-csslint.png
[npm-image]: https://img.shields.io/npm/v/gulp-csslint.svg
[david-url]: https://david-dm.org/lazd/gulp-csslint
[david-image]: https://img.shields.io/david/lazd/gulp-csslint.svg
[coveralls-url]: https://coveralls.io/r/lazd/gulp-csslint
[coveralls-image]: https://img.shields.io/coveralls/lazd/gulp-csslint.svg
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc