gulp-csslint
Advanced tools
Comparing version 0.3.1 to 1.0.0
91
index.js
@@ -8,28 +8,8 @@ 'use strict'; | ||
var formatOutput = function(report, file, options) { | ||
if (!report.messages.length) { | ||
return { | ||
success: true | ||
}; | ||
function validateFormatter(formatter) { | ||
if (typeof formatter !== 'object' || !formatter.id || !formatter.startFormat || !formatter.endFormat || !formatter.endFormat || !formatter.formatResults) { | ||
throw new Error('Invalid formatter: formatters need to be objects, and contain "id", "name", "startFormat", "endFormat" and "formatResults"'); | ||
} | ||
} | ||
var filePath = (file.path || 'stdin'); | ||
// Handle errors | ||
var results = report.messages.map(function(err) { | ||
if (!err) return; | ||
return { file: filePath, error: err }; | ||
}).filter(function(err) { | ||
return err; | ||
}); | ||
return { | ||
originalReport: report, | ||
errorCount: results.length, | ||
success: false, | ||
results: results, | ||
options: options | ||
}; | ||
}; | ||
var cssLintPlugin = function(options) { | ||
@@ -68,3 +48,3 @@ options = options || {}; | ||
// send status down-stream | ||
file.csslint = formatOutput(report, file, ruleset); | ||
file.csslint = { report: report, success: !report.messages.length }; | ||
@@ -76,5 +56,5 @@ cb(null, file); | ||
cssLintPlugin.reporter = function(customReporter, options) { | ||
var reporter = csslint.getFormatter('text'); | ||
var builtInReporter = true; | ||
cssLintPlugin.formatter = function(customFormatter, options) { | ||
var formatter = csslint.getFormatter('text'); | ||
var builtInFormatter = true; | ||
var output; | ||
@@ -85,21 +65,29 @@ | ||
var logger = options.logger || process.stdout.write.bind(process.stdout); | ||
if (typeof customReporter === 'function') { | ||
reporter = customReporter; | ||
builtInReporter = false; | ||
} | ||
else if (typeof customReporter === 'string') { | ||
if (customReporter === 'fail') { | ||
return cssLintPlugin.failReporter(); | ||
if (customFormatter) { | ||
if (typeof customFormatter === 'function') { | ||
formatter = customFormatter; | ||
builtInFormatter = false; | ||
} | ||
else if (typeof customFormatter === 'string') { | ||
if (customFormatter === 'fail') { | ||
return cssLintPlugin.failFormatter(); | ||
} | ||
reporter = csslint.getFormatter(customReporter); | ||
} | ||
formatter = csslint.getFormatter(customFormatter); | ||
if (typeof reporter === 'undefined') { | ||
throw new Error('Invalid reporter'); | ||
if (typeof formatter === 'undefined') { | ||
throw new Error('Invalid reporter: ' + customFormatter); | ||
} | ||
} | ||
else if (typeof customFormatter === 'object') { | ||
validateFormatter(customFormatter); | ||
formatter = customFormatter; | ||
} | ||
else { | ||
throw new Error('Invalid custom formatter passed, please pass `null` or `undefined` if you want to pass options while using default formatter.'); | ||
} | ||
} | ||
if (builtInReporter) { | ||
output = [reporter.startFormat()]; | ||
if (builtInFormatter) { | ||
output = [formatter.startFormat()]; | ||
} | ||
@@ -111,7 +99,7 @@ | ||
if (file.csslint && !file.csslint.success) { | ||
if (builtInReporter) { | ||
output.push(reporter.formatResults(file.csslint.originalReport, file.path, options)); | ||
if (builtInFormatter) { | ||
output.push(formatter.formatResults(file.csslint.report, file.path, options)); | ||
} | ||
else { | ||
reporter(file); | ||
formatter(file.csslint.report, file.path, options); | ||
} | ||
@@ -123,4 +111,4 @@ } | ||
function(cb) { | ||
if (builtInReporter) { | ||
output.push(reporter.endFormat()); | ||
if (builtInFormatter) { | ||
output.push(formatter.endFormat()); | ||
@@ -146,3 +134,10 @@ output | ||
cssLintPlugin.failReporter = function() { | ||
cssLintPlugin.addFormatter = function(formatter) { | ||
formatter = typeof formatter === 'string' ? require(formatter) : formatter; | ||
validateFormatter(formatter); | ||
csslint.addFormatter(formatter); | ||
}; | ||
cssLintPlugin.failFormatter = function() { | ||
return through.obj(function(file, enc, cb) { | ||
@@ -149,0 +144,0 @@ // Nothing to report or no errors |
{ | ||
"name": "gulp-csslint", | ||
"version": "0.3.1", | ||
"version": "1.0.0", | ||
"description": "CSSLint plugin for gulp", | ||
@@ -10,23 +10,27 @@ "main": "index.js", | ||
"dependencies": { | ||
"csslint": "^0.10.0", | ||
"gulp-util": "^3.0.4", | ||
"rcloader": "^0.1.4", | ||
"through2": "^2.0.0" | ||
"csslint": "^1.0.2", | ||
"gulp-util": "^3.0.7", | ||
"rcloader": "^0.2.1", | ||
"through2": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"eslint": "^1.1.0", | ||
"eslint-config-semistandard": "^5.0.0", | ||
"eslint-config-standard": "^4.1.0", | ||
"eslint-plugin-standard": "^1.2.0", | ||
"istanbul": "^0.3.14", | ||
"mocha": "^2.2.5", | ||
"rimraf": "^2.3.4", | ||
"should": "^7.0.3", | ||
"sinon": "^1.15.4" | ||
"coveralls": "^2.11.12", | ||
"csslint-stylish": "0.0.4", | ||
"eslint": "^3.2.2", | ||
"eslint-config-semistandard": "^7.0.0-beta.0", | ||
"eslint-config-standard": "^6.0.0-beta.2", | ||
"eslint-plugin-promise": "^2.0.1", | ||
"eslint-plugin-standard": "^2.0.0", | ||
"mocha": "^3.0.2", | ||
"node-version-check": "^2.1.0", | ||
"nyc": "^7.1.0", | ||
"rimraf": "^2.5.4", | ||
"should": "^11.0.0", | ||
"sinon": "^1.17.5" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf coverage/", | ||
"cover": "istanbul cover _mocha", | ||
"lint": "eslint index.js test/main.js", | ||
"clean": "rimraf coverage/ .nyc_output/", | ||
"cover": "nyc mocha", | ||
"postcover": "nyc report --reporter lcov", | ||
"lint": "node-version-gte-4 && eslint index.js test/main.js || node-version-lt-4", | ||
"precover": "npm run lint && npm run clean", | ||
@@ -33,0 +37,0 @@ "pretest": "npm run lint", |
@@ -20,3 +20,3 @@ # 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] | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter()); | ||
.pipe(csslint.formatter()); | ||
}); | ||
@@ -43,3 +43,3 @@ ``` | ||
})) | ||
.pipe(csslint.reporter()); | ||
.pipe(csslint.formatter()); | ||
``` | ||
@@ -57,3 +57,3 @@ | ||
.pipe(csslint('csslintrc.json')) | ||
.pipe(csslint.reporter()); | ||
.pipe(csslint.formatter()); | ||
``` | ||
@@ -67,12 +67,10 @@ | ||
file.csslint.success = true; // or false | ||
file.csslint.errorCount = 0; // number of errors returned by CSSLint | ||
file.csslint.results = []; // CSSLint errors | ||
file.csslint.opt = {}; // The options you passed to CSSLint | ||
file.csslint.report = {}; // The report from CSSLint after linting the file | ||
``` | ||
## Using reporters | ||
## Using formatters | ||
Several reporters come built-in to css-lint. To use one of these reporters, pass the name to `csslint.reporter`. | ||
Several formatters come built-in to CSSLint. To use one of these formatters, pass the name to `csslint.formatter`. | ||
For a list of all reporters supported by `csslint`, see the [csslint wiki](https://github.com/CSSLint/csslint/wiki/Command-line-interface#--format). | ||
For a list of all formatters supported by `csslint`, see the [csslint wiki](https://github.com/CSSLint/csslint/wiki/Command-line-interface#--format). | ||
@@ -83,36 +81,54 @@ ```js | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter('junit-xml')); | ||
.pipe(csslint.formatter('junit-xml')); | ||
``` | ||
### Custom reporters | ||
### Custom formatters | ||
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. | ||
Custom formatters can be provided by first adding a valid CSSLint-formatter, such as `csslint-stylish`, then using it: | ||
```js | ||
var csslint = require('gulp-csslint'); | ||
var gutil = require('gulp-util'); | ||
var customReporter = function(file) { | ||
gutil.log(gutil.colors.cyan(file.csslint.errorCount)+' errors in '+gutil.colors.magenta(file.path)); | ||
csslint.addFormatter('csslint-stylish'); | ||
file.csslint.results.forEach(function(result) { | ||
gutil.log(result.error.message+' on line '+result.error.line); | ||
}); | ||
}; | ||
gulp.task('lint', function() { | ||
gulp.src('lib/*.css') | ||
.pipe(csslint()) | ||
.pipe(csslint.formatter('stylish')) | ||
}); | ||
``` | ||
You can provide the formatter by requiring it directly as well: | ||
```js | ||
var csslint = require('gulp-csslint'); | ||
gulp.task('lint', function() { | ||
gulp.src('lib/*.css') | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter(customReporter)); | ||
.pipe(csslint.formatter(require('csslint-stylish'))) | ||
}); | ||
``` | ||
### Reporter options | ||
You can also pass options to the built-in formatter, by passing a second option to `reporter`. | ||
You can also provide an object with the following contract to implement your own formatter: | ||
```js | ||
{ | ||
id: 'string', // Name passed to csslint.formatter | ||
startFormat: function() {}, // Called before parsing any files, should return a string | ||
startFormat: function() {}, // Called after parsing all files, should return a string | ||
formatResult: function (results, filename, options) {} // Called with a results-object per file linted. Optionally called with a filename, and options passed to csslint.formatter(*formatter*, *options*) | ||
} | ||
``` | ||
You can also provide a function, which is called for each file linted with the same arguments as `formatResults`. | ||
### Formatter options | ||
You can also pass options to the built-in formatter, by passing a second option to `formatter`. | ||
```js | ||
gulp.task('lint', function() { | ||
gulp.src('lib/*.css') | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter('junit-xml', options)); | ||
.pipe(csslint.formatter('junit-xml', options)); | ||
}); | ||
@@ -130,3 +146,3 @@ ``` | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter('junit-xml', {logger: console.log.bind(console)})); | ||
.pipe(csslint.formatter('junit-xml', {logger: console.log.bind(console)})); | ||
}); | ||
@@ -139,7 +155,7 @@ ``` | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter('junit-xml', {logger: gutil.log.bind(null, 'gulp-csslint:')})); | ||
.pipe(csslint.formatter('junit-xml', {logger: gutil.log.bind(null, 'gulp-csslint:')})); | ||
}); | ||
``` | ||
`logger` is called once for the starting format of the reporter, then once for each file containing violations, then | ||
`logger` is called once for the starting format of the formatter, then once for each file containing violations, then | ||
lastly once for the ending format. Instead of writing to `stdout`, you can write to file using this option. | ||
@@ -154,3 +170,3 @@ | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter('junit-xml', {logger: function(str) { output += str; }})) | ||
.pipe(csslint.formatter('junit-xml', {logger: function(str) { output += str; }})) | ||
.on('end', function(err) { | ||
@@ -164,3 +180,3 @@ if (err) return cb(err); | ||
This functionality is only available when not using custom reporters. | ||
This functionality is only available when not using a custom formatting function. | ||
@@ -181,3 +197,3 @@ ## Custom rules | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter()) | ||
.pipe(csslint.formatter()) | ||
}); | ||
@@ -188,3 +204,3 @@ ``` | ||
Pipe the file stream to `csslint.failReporter()` to fail on errors. | ||
Pipe the file stream to `csslint.failFormatter()` to fail on errors. | ||
@@ -197,4 +213,4 @@ ```js | ||
.pipe(csslint()) | ||
.pipe(csslint.reporter()) // Display errors | ||
.pipe(csslint.reporter('fail')); // Fail on error (or csslint.failReporter()) | ||
.pipe(csslint.formatter()) // Display errors | ||
.pipe(csslint.formatter('fail')); // Fail on error (or csslint.failFormatter()) | ||
}); | ||
@@ -201,0 +217,0 @@ ``` |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12801
1
216
13
119
1
+ Addedclone@2.1.2(transitive)
+ Addedcsslint@1.0.5(transitive)
+ Addedlodash.assign@4.2.0(transitive)
+ Addedlodash.isobject@3.0.2(transitive)
+ Addedlodash.merge@4.6.2(transitive)
+ Addedparserlib@1.1.1(transitive)
+ Addedrcloader@0.2.2(transitive)
- Removedcsslint@0.10.0(transitive)
- Removedlodash@3.10.1(transitive)
- Removedparserlib@0.2.5(transitive)
- Removedrcloader@0.1.4(transitive)
Updatedcsslint@^1.0.2
Updatedgulp-util@^3.0.7
Updatedrcloader@^0.2.1
Updatedthrough2@^2.0.1