Skip to content

Commit

Permalink
use destructuring assignment to simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Jul 5, 2018
1 parent aed571b commit 72c3599
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## 5.0.0-0
## 5.0.0

* Bump `eslint` dependency to ^5.0.0 <https://eslint.org/blog/2018/06/eslint-v5.0.0-released>
* Use destructuring assignment to simplify the code

## 4.0.2

Expand Down
50 changes: 31 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
'use strict';

const PluginError = require('plugin-error');
const CLIEngine = require('eslint').CLIEngine;
const util = require('./util');
const path = require('path');
const {CLIEngine} = require('eslint');
const {
createIgnoreResult,
filterResult,
firstResultMessage,
handleCallback,
isErrorMessage,
migrateOptions,
resolveFormatter,
resolveWritable,
transform,
tryResultAction,
writeResults
} = require('./util');
const {relative} = require('path');

/**
* Append ESLint result to each file
Expand All @@ -12,11 +24,11 @@ const path = require('path');
* @returns {stream} gulp file stream
*/
function gulpEslint(options) {
options = util.migrateOptions(options) || {};
options = migrateOptions(options) || {};
const linter = new CLIEngine(options);

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

if (file.isNull()) {
cb(null, file);
Expand All @@ -38,7 +50,7 @@ function gulpEslint(options) {

if (linter.options.ignore && options.warnFileIgnored) {
// Warn that gulp.src is needlessly reading files that ESLint ignores
file.eslint = util.createIgnoreResult(file);
file.eslint = createIgnoreResult(file);
}
cb(null, file);
return;
Expand All @@ -57,7 +69,7 @@ function gulpEslint(options) {

if (options.quiet) {
// ignore warnings
file.eslint = util.filterResult(result, options.quiet);
file.eslint = filterResult(result, options.quiet);
} else {
file.eslint = result;
}
Expand All @@ -82,9 +94,9 @@ gulpEslint.result = action => {
throw new Error('Expected callable argument');
}

return util.transform((file, enc, done) => {
return transform((file, enc, done) => {
if (file.eslint) {
util.tryResultAction(action, file.eslint, util.handleCallback(done, file));
tryResultAction(action, file.eslint, handleCallback(done, file));
} else {
done(null, file);
}
Expand All @@ -106,7 +118,7 @@ gulpEslint.results = function(action) {
results.errorCount = 0;
results.warningCount = 0;

return util.transform((file, enc, done) => {
return transform((file, enc, done) => {
if (file.eslint) {
results.push(file.eslint);
// collect total error/warning count
Expand All @@ -116,7 +128,7 @@ gulpEslint.results = function(action) {
done(null, file);

}, done => {
util.tryResultAction(action, results, util.handleCallback(done));
tryResultAction(action, results, handleCallback(done));
});
};

Expand All @@ -127,7 +139,7 @@ gulpEslint.results = function(action) {
*/
gulpEslint.failOnError = () => {
return gulpEslint.result(result => {
const error = util.firstResultMessage(result, util.isErrorMessage);
const error = firstResultMessage(result, isErrorMessage);
if (!error) {
return;
}
Expand Down Expand Up @@ -168,10 +180,10 @@ gulpEslint.failAfterError = () => {
* @returns {stream} gulp file stream
*/
gulpEslint.formatEach = (formatter, writable) => {
formatter = util.resolveFormatter(formatter);
writable = util.resolveWritable(writable);
formatter = resolveFormatter(formatter);
writable = resolveWritable(writable);

return gulpEslint.result(result => util.writeResults([result], formatter, writable));
return gulpEslint.result(result => writeResults([result], formatter, writable));
};

/**
Expand All @@ -182,13 +194,13 @@ gulpEslint.formatEach = (formatter, writable) => {
* @returns {stream} gulp file stream
*/
gulpEslint.format = (formatter, writable) => {
formatter = util.resolveFormatter(formatter);
writable = util.resolveWritable(writable);
formatter = resolveFormatter(formatter);
writable = resolveWritable(writable);

return gulpEslint.results(results => {
// Only format results if files has been lint'd
if (results.length) {
util.writeResults(results, formatter, writable);
writeResults(results, formatter, writable);
}
});
};
Expand Down
10 changes: 5 additions & 5 deletions util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const Transform = require('stream').Transform;
const {Transform} = require('stream');
const PluginError = require('plugin-error');
const fancyLog = require('fancy-log');
const CLIEngine = require('eslint').CLIEngine;
const {CLIEngine} = require('eslint');

/**
* Convenience method for creating a transform stream in object mode
Expand Down Expand Up @@ -40,9 +40,9 @@ exports.createIgnoreResult = file => {
messages: [{
fatal: false,
severity: 1,
message: file.path.indexOf('node_modules/') < 0 ?
'File ignored because of .eslintignore file' :
'File ignored because it has a node_modules/** path'
message: file.path.includes('node_modules/') ?
'File ignored because it has a node_modules/** path' :
'File ignored because of .eslintignore file'
}],
errorCount: 0,
warningCount: 1
Expand Down

0 comments on commit 72c3599

Please sign in to comment.