gulp-eslint
Advanced tools
Comparing version
52
index.js
@@ -5,3 +5,4 @@ 'use strict'; | ||
PluginError = require('gulp-util').PluginError, | ||
EsLint = require('eslint').CLIEngine, | ||
eslint = require('eslint').linter, | ||
CLIEngine = require('eslint').CLIEngine, | ||
util = require('./util'); | ||
@@ -13,9 +14,12 @@ | ||
function gulpEslint(options) { | ||
var linter = new EsLint(util.migrateOptions(options)); | ||
options = util.migrateOptions(options); | ||
var linter = new CLIEngine(options); | ||
function verify(filePath, contents) { | ||
var result = linter.executeOnText(contents).results[0]; | ||
var config = linter.getConfigForFile(filePath); | ||
var messages = eslint.verify(contents, config, filePath); | ||
//eslint.reset(); | ||
return { | ||
filePath: filePath, | ||
messages: result && result.messages || [] | ||
messages: messages || [] | ||
}; | ||
@@ -25,4 +29,4 @@ } | ||
return map(function (file, output) { | ||
if (linter.isPathIgnored(file.path) || file.isNull()) { | ||
// remove base path from file path before calling isPathIgnored | ||
if (util.isPathIgnored(file, linter.options) || file.isNull()) { | ||
output(null, file); | ||
@@ -57,8 +61,3 @@ | ||
messages.some(function (message) { | ||
var level = message.fatal ? 2 : message.severity; | ||
if (Array.isArray(level)) { | ||
level = level[0]; | ||
} | ||
if (level > 1) { | ||
if (util.isErrorMessage(message)) { | ||
error = new PluginError( | ||
@@ -82,2 +81,31 @@ 'gulp-eslint', | ||
/** | ||
* Fail when the stream ends if any eslint error(s) occurred | ||
*/ | ||
gulpEslint.failAfterError = function () { | ||
var errorCount = 0; | ||
return map(function (file, output) { | ||
var messages = file.eslint && file.eslint.messages || []; | ||
messages.forEach(function (message) { | ||
if (util.isErrorMessage(message)) { | ||
errorCount++; | ||
} | ||
}); | ||
output(null, file); | ||
}).once('end', function () { | ||
// Only format results if files has been lint'd | ||
if (errorCount > 0) { | ||
this.emit('error', new PluginError( | ||
'gulp-eslint', | ||
{ | ||
name: 'ESLintError', | ||
message: 'Failed with ' + errorCount + (errorCount === 1 ? ' error' : ' errors') | ||
} | ||
)); | ||
} | ||
}); | ||
}; | ||
/** | ||
* Wait until all files have been linted and format all results at once. | ||
@@ -84,0 +112,0 @@ */ |
{ | ||
"name": "gulp-eslint", | ||
"version": "0.2.0", | ||
"version": "0.2.2", | ||
"description": "A gulp plugin for processing files with eslint", | ||
@@ -18,3 +18,5 @@ "main": "index.js", | ||
"test": "mocha", | ||
"gulp": "gulp" | ||
"gulp": "gulp", | ||
"throw-on-error": "gulp throw-on-error", | ||
"throw-after-error": "gulp throw-after-error" | ||
}, | ||
@@ -47,3 +49,3 @@ "keywords": [ | ||
"dependencies": { | ||
"eslint": "^0.9.2", | ||
"eslint": "^0.13.0", | ||
"gulp-util": "^3.0.1", | ||
@@ -50,0 +52,0 @@ "map-stream": "^0.1.0", |
# gulp-eslint [](https://travis-ci.org/adametry/gulp-eslint) | ||
> A [Gulp](https://github.com/wearefractal/gulp) plugin for [eslint](https://github.com/wearefractal/gulp). | ||
> A [Gulp](https://github.com/wearefractal/gulp) plugin for [eslint](http://eslint.org/). | ||
@@ -4,0 +4,0 @@ ## Usage |
41
util.js
@@ -6,4 +6,8 @@ 'use strict'; | ||
through = require('through'), | ||
Config = require('eslint/lib/config'); | ||
EsLint = require('eslint').CLIEngine, | ||
IgnoredPaths = require('eslint/lib/ignored-paths'), | ||
FileFinder = require('eslint/lib/file-finder'); | ||
var ignoreFileFinder = new FileFinder('.eslintignore'); | ||
/** | ||
@@ -35,2 +39,21 @@ * Optional import, if not found, returns null. | ||
/** | ||
* Mimic the CLIEngine.isPathIgnored, but resolve .eslintignore based on file's directory rather than process.cwd() | ||
*/ | ||
exports.isPathIgnored = function (file, options) { | ||
var filePath; | ||
if (!options.ignore) { | ||
return false; | ||
} | ||
if (typeof options.ignorePath !== 'string') { | ||
options = { | ||
ignore: true, | ||
ignorePath: ignoreFileFinder.findInDirectoryOrParents(path.dirname(file.path || '')) | ||
}; | ||
} | ||
// set file path relative to the .eslintignore directory or cwd | ||
filePath = path.relative(path.dirname(options.ignorePath || '') || process.cwd(), file.path); | ||
return IgnoredPaths.load(options).contains(filePath); | ||
}; | ||
/** | ||
* Create config helper to merge various config sources | ||
@@ -92,2 +115,13 @@ */ | ||
/** | ||
* Resolve writable | ||
*/ | ||
exports.isErrorMessage = function (message) { | ||
var level = message.fatal ? 2 : message.severity; | ||
if (Array.isArray(level)) { | ||
level = level[0]; | ||
} | ||
return (level > 1); | ||
}; | ||
/** | ||
* Resolve formatter from unknown type (accepts string or function) | ||
@@ -103,6 +137,5 @@ * @exception TypeError thrown if unable to resolve the formatter type | ||
if (typeof formatter === 'string') { | ||
// load formatter (module, relative to cwd, eslint formatter) | ||
formatter = optional(formatter) | ||
|| optional(path.resolve(process.cwd(), formatter)) | ||
|| optional('eslint/lib/formatters/' + formatter); | ||
formatter = (new EsLint()).getFormatter(formatter); | ||
@@ -109,0 +142,0 @@ if (typeof formatter === 'string') { |
18371
29.53%6
50%290
23.93%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated