This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathindex.js
296 lines (261 loc) · 7.64 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use strict";
//var fs = require("fs");
var assign = require("object-assign");
var loaderUtils = require("loader-utils");
var objectHash = require("object-hash");
var createCache = require("loader-fs-cache");
var pkg = require("./package.json");
var cache = createCache("eslint-loader");
var engines = {};
/**
* Class representing an ESLintError.
* @extends Error
*/
class ESLintError extends Error {
/**
* Create an ESLintError.
* @param {string} messages - Formatted eslint errors.
*/
constructor(messages) {
super();
this.name = "ESLintError";
this.message = messages;
this.stack = "";
}
/**
* Returns a stringified representation of our error. This method is called
* when an error is consumed by console methods
* ex: console.error(new ESLintError(formattedMessage))
* @return {string} error - A stringified representation of the error.
*/
inspect() {
return this.message;
}
}
/**
* printLinterOutput
*
* @param {Object} eslint.executeOnText return value
* @param {Object} config eslint configuration
* @param {Object} webpack webpack instance
* @return {void}
*/
function printLinterOutput(res, config, webpack) {
// skip ignored file warning
if (
!(
res.warningCount === 1 &&
res.results[0].messages[0] &&
res.results[0].messages[0].message &&
res.results[0].messages[0].message.indexOf("ignore") > 1
)
) {
// quiet filter done now
// eslint allow rules to be specified in the input between comments
// so we can found warnings defined in the input itself
if (res.warningCount && config.quiet) {
res.warningCount = 0;
res.results[0].warningCount = 0;
res.results[0].messages = res.results[0].messages.filter(function(
message
) {
return message.severity !== 1;
});
}
// if enabled, use eslint auto-fixing where possible
if (
config.fix &&
(res.results[0].output !== res.src ||
res.results[0].fixableErrorCount > 0 ||
res.results[0].fixableWarningCount > 0)
) {
var eslint = require(config.eslintPath);
eslint.CLIEngine.outputFixes(res);
}
if (res.errorCount || res.warningCount) {
// add filename for each results so formatter can have relevant filename
res.results.forEach(function(r) {
r.filePath = webpack.resourcePath;
});
var messages = config.formatter(res.results);
if (config.outputReport && config.outputReport.filePath) {
var reportOutput;
// if a different formatter is passed in as an option use that
if (config.outputReport.formatter) {
reportOutput = config.outputReport.formatter(res.results);
} else {
reportOutput = messages;
}
var filePath = loaderUtils.interpolateName(
webpack,
config.outputReport.filePath,
{
content: res.results
.map(function(r) {
return r.source;
})
.join("\n")
}
);
webpack.emitFile(filePath, reportOutput);
}
// default behavior: emit error only if we have errors
var emitter = res.errorCount ? webpack.emitError : webpack.emitWarning;
// force emitError or emitWarning if user want this
if (config.emitError) {
emitter = webpack.emitError;
} else if (config.emitWarning) {
emitter = webpack.emitWarning;
}
if (emitter) {
if (config.failOnError && res.errorCount) {
throw new ESLintError(
"Module failed because of a eslint error.\n" + messages
);
} else if (config.failOnWarning && res.warningCount) {
throw new ESLintError(
"Module failed because of a eslint warning.\n" + messages
);
}
emitter(new ESLintError(messages));
} else {
throw new Error(
"Your module system doesn't support emitWarning. " +
"Update available? \n" +
messages
);
}
}
}
}
/**
* webpack loader
*
* @param {String|Buffer} input JavaScript string
* @param {Object} map input source map
* @return {void}
*/
module.exports = function(input, map) {
var webpack = this;
var userOptions = assign(
// user defaults
(webpack.options && webpack.options.eslint) || webpack.query || {},
// loader query string
loaderUtils.getOptions(webpack)
);
var eslintPkgPath = "eslint/package.json";
var userEslintPath = eslintPkgPath;
if (userOptions.eslintPath) {
userEslintPath = userOptions.eslintPath + "/package.json";
}
var eslintVersion;
try {
eslintVersion = require(require.resolve(userEslintPath)).version;
} catch (_) {
// ignored
}
if (!eslintVersion) {
try {
eslintVersion = require(require.resolve(eslintPkgPath)).version;
} catch (_) {
// ignored
}
}
var config = assign(
// loader defaults
{
cacheIdentifier: JSON.stringify({
"eslint-loader": pkg.version,
eslint: eslintVersion || "unknown version"
}),
eslintPath: "eslint"
},
userOptions
);
var cacheDirectory = config.cache;
var cacheIdentifier = config.cacheIdentifier;
delete config.cacheIdentifier;
// Create the engine only once per config
var configHash = objectHash(config);
if (!engines[configHash]) {
var eslint = require(config.eslintPath);
engines[configHash] = new eslint.CLIEngine(config);
// Try to get oficial formatter
if (typeof config.formatter === "string") {
try {
config.formatter = engines[configHash].getFormatter(config.formatter);
} catch (_) {
try {
config.formatter = require(config.formatter);
if (
config.formatter &&
typeof config.formatter !== "function" &&
typeof config.formatter.default === "function"
) {
config.formatter = config.formatter.default;
}
} catch (_) {
// ignored
}
}
}
// Get default formatter `stylish` when not defined
if (config.formatter == null || typeof config.formatter !== "function") {
config.formatter = engines[configHash].getFormatter("stylish");
}
}
webpack.cacheable();
var emitter = config.emitError ? webpack.emitError : webpack.emitWarning;
var engine = engines[configHash];
var resourcePath = webpack.resourcePath;
var cwd = process.cwd();
// remove cwd from resource path in case webpack has been started from project
// root, to allow having relative paths in .eslintignore
if (resourcePath.indexOf(cwd) === 0) {
resourcePath = resourcePath.substr(cwd.length + 1);
}
// return early if cached
if (config.cache) {
var callback = webpack.async();
return cache(
{
directory: cacheDirectory,
identifier: cacheIdentifier,
options: config,
source: input,
transform: function() {
return lint(engine, input, resourcePath, emitter);
}
},
function(err, res) {
if (err) {
return callback(err);
}
try {
printLinterOutput(
assign({}, res || {}, { src: input }),
config,
webpack
);
} catch (e) {
err = e;
}
return callback(err, input, map);
}
);
}
printLinterOutput(
lint(engine, input, resourcePath, emitter),
config,
webpack
);
webpack.callback(null, input, map);
};
function lint(engine, input, resourcePath, emitter) {
try {
return engine.executeOnText(input, resourcePath, true);
} catch (_) {
if (emitter) emitter(_);
return { src: input };
}
}