forked from adametry/gulp-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
196 lines (170 loc) · 5.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const PluginError = require('plugin-error');
const CLIEngine = require('eslint').CLIEngine;
const util = require('./util');
const path = require('path');
/**
* Append ESLint result to each file
*
* @param {(Object|String)} [options] - Configure rules, env, global, and other options for running ESLint
* @returns {stream} gulp file stream
*/
function gulpEslint(options) {
options = util.migrateOptions(options) || {};
const linter = new CLIEngine(options);
return util.transform((file, enc, cb) => {
const filePath = path.relative(process.cwd(), file.path);
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError('gulp-eslint', 'gulp-eslint doesn\'t support vinyl files with Stream contents.'));
return;
}
if (linter.isPathIgnored(filePath)) {
// Note:
// Vinyl files can have an independently defined cwd, but ESLint works relative to `process.cwd()`.
// (https://github.com/gulpjs/gulp/blob/master/docs/recipes/specifying-a-cwd.md)
// Also, ESLint doesn't adjust file paths relative to an ancestory .eslintignore path.
// E.g., If ../.eslintignore has "foo/*.js", ESLint will ignore ./foo/*.js, instead of ../foo/*.js.
// Eslint rolls this into `CLIEngine.executeOnText`. So, gulp-eslint must account for this limitation.
if (linter.options.ignore && options.warnFileIgnored) {
// Warn that gulp.src is needlessly reading files that ESLint ignores
file.eslint = util.createIgnoreResult(file);
}
cb(null, file);
return;
}
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);
file.eslint.fixed = true;
}
cb(null, file);
});
}
/**
* Handle each ESLint result as it passes through the stream.
*
* @param {Function} action - A function to handle each ESLint result
* @returns {stream} gulp file stream
*/
gulpEslint.result = action => {
if (typeof action !== 'function') {
throw new Error('Expected callable argument');
}
return util.transform((file, enc, done) => {
if (file.eslint) {
util.tryResultAction(action, file.eslint, util.handleCallback(done, file));
} else {
done(null, file);
}
});
};
/**
* Handle all ESLint results at the end of the stream.
*
* @param {Function} action - A function to handle all ESLint results
* @returns {stream} gulp file stream
*/
gulpEslint.results = function(action) {
if (typeof action !== 'function') {
throw new Error('Expected callable argument');
}
const results = [];
results.errorCount = 0;
results.warningCount = 0;
return util.transform((file, enc, done) => {
if (file.eslint) {
results.push(file.eslint);
// collect total error/warning count
results.errorCount += file.eslint.errorCount;
results.warningCount += file.eslint.warningCount;
}
done(null, file);
}, done => {
util.tryResultAction(action, results, util.handleCallback(done));
});
};
/**
* Fail when an ESLint error is found in ESLint results.
*
* @returns {stream} gulp file stream
*/
gulpEslint.failOnError = () => {
return gulpEslint.result(result => {
const error = util.firstResultMessage(result, util.isErrorMessage);
if (!error) {
return;
}
throw new PluginError('gulp-eslint', {
name: 'ESLintError',
fileName: result.filePath,
message: error.message,
lineNumber: error.line
});
});
};
/**
* Fail when the stream ends if any ESLint error(s) occurred
*
* @returns {stream} gulp file stream
*/
gulpEslint.failAfterError = () => {
return gulpEslint.results(results => {
const count = results.errorCount;
if (!count) {
return;
}
throw new PluginError('gulp-eslint', {
name: 'ESLintError',
message: 'Failed with ' + count + (count === 1 ? ' error' : ' errors')
});
});
};
/**
* Format the results of each file individually.
*
* @param {(String|Function)} [formatter=stylish] - The name or function for a ESLint result formatter
* @param {(Function|Stream)} [writable=fancy-log] - A funtion or stream to write the formatted ESLint results.
* @returns {stream} gulp file stream
*/
gulpEslint.formatEach = (formatter, writable) => {
formatter = util.resolveFormatter(formatter);
writable = util.resolveWritable(writable);
return gulpEslint.result(result => util.writeResults([result], formatter, writable));
};
/**
* Wait until all files have been linted and format all results at once.
*
* @param {(String|Function)} [formatter=stylish] - The name or function for a ESLint result formatter
* @param {(Function|stream)} [writable=fancy-log] - A funtion or stream to write the formatted ESLint results.
* @returns {stream} gulp file stream
*/
gulpEslint.format = (formatter, writable) => {
formatter = util.resolveFormatter(formatter);
writable = util.resolveWritable(writable);
return gulpEslint.results(results => {
// Only format results if files has been lint'd
if (results.length) {
util.writeResults(results, formatter, writable);
}
});
};
module.exports = gulpEslint;