Skip to content

Commit

Permalink
emit an error when it fails to load an ESLint plugin
Browse files Browse the repository at this point in the history
fix #185
  • Loading branch information
shinnn committed Jun 12, 2017
1 parent b8bf261 commit 18a4299
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* Use [`Buffer.from(<string>)`](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding) instead of the deprecated [`new Buffer(<string>)`](https://nodejs.org/dist/latest-v8.x/docs/api/buffer.html#buffer_new_buffer_string_encoding)
* Note that `Buffer.from` is only available on Node.js >= [4.5.0](https://nodejs.org/en/blog/release/v4.5.0/).
* Bump [`eslint`](https://github.com/eslint/eslint) dependency to [`^4.0.0`](http://eslint.org/blog/2017/06/eslint-v4.0.0-released)
* Emit a [`PluginError`](https://github.com/gulpjs/gulp-util#new-pluginerrorpluginname-message-options) when it fails to load an [ESLint plugin](http://eslint.org/docs/user-guide/configuring#configuring-plugins)

## 3.0.1

Expand Down
32 changes: 18 additions & 14 deletions index.js
Expand Up @@ -15,19 +15,6 @@ function gulpEslint(options) {
options = util.migrateOptions(options) || {};
const linter = new CLIEngine(options);

function verify(str, filePath) {
const result = linter.executeOnText(str, filePath).results[0];
// Note: Fixes are applied as part of "executeOnText".
// Any applied fix messages have been removed from the result.

if (options.quiet) {
// ignore warnings
return util.filterResult(result, options.quiet);
}

return result;
}

return util.transform((file, enc, cb) => {
const filePath = path.relative(process.cwd(), file.path);

Expand Down Expand Up @@ -57,7 +44,24 @@ function gulpEslint(options) {
return;
}

file.eslint = verify(file.contents.toString(), filePath);
let result;

try {
result = linter.executeOnText(file.contents.toString(), filePath).results[0];
} catch (e) {
cb(new PluginError('gulp-eslint', e));
return;
}
// Note: Fixes are applied as part of "executeOnText".
// Any applied fix messages have been removed from the result.

if (options.quiet) {
// ignore warnings
file.eslint = util.filterResult(result, options.quiet);
} else {
file.eslint = result;
}

// Update the fixed output; otherwise, fixable messages are simply ignored.
if (file.eslint.hasOwnProperty('output')) {
file.contents = Buffer.from(file.eslint.output);
Expand Down
18 changes: 18 additions & 0 deletions test/linting.js
Expand Up @@ -106,6 +106,7 @@ describe('gulp-eslint plugin', () => {
it('should emit an error when it takes a steam content', done => {
eslint({useEslintrc: false, rules: {'strict': 'error'}})
.on('error', err => {
err.plugin.should.equal('gulp-eslint');
err.message.should.equal('gulp-eslint doesn\'t support vinyl files with Stream contents.');
done();
})
Expand All @@ -115,6 +116,23 @@ describe('gulp-eslint plugin', () => {
}));
});

it('should emit an error when it fails to load a plugin', done => {
const pluginName = 'this-is-unknown-plugin';
eslint({plugins: [pluginName]})
.on('error', err => {
err.plugin.should.equal('gulp-eslint');
err.message.should.equal(`Failed to load plugin this-is-unknown-plugin: Cannot find module 'eslint-plugin-${
pluginName
}'`);

done();
})
.end(new File({
path: 'test/fixtures/file.js',
contents: Buffer.from('')
}));
});

describe('"warnFileIgnored" option', () => {

it('when true, should warn when a file is ignored by .eslintignore', done => {
Expand Down

0 comments on commit 18a4299

Please sign in to comment.